Solved

MVC 3 Submit Button not working properly

Posted on 2012-12-25
12
1,250 Views
Last Modified: 2013-01-01
I'm using mvc 3 (C#), I have a contact form used for sending emails.  However the submit button does not work properly.

//view/home/contact.cshtml

@using (Html.BeginForm())
   {
    <div id="divContact">      
   <table>
    <tr>
        <td>
            @Html.TextArea("txtComment", "")            
        </td>
    </tr>
    <tr>
        <td>
            <input id="btnContactSubmit" name="btnSubmit" type="submit" value="Send" />
        </td>
    </tr>
   </table>
   </div>
   }

//HomeController

 [HttpPost]
        public ActionResult Contact(string email, string subject, string body)
        {
            try
            {
                WebMail.SmtpServer = "smtp info goes here";
                WebMail.Send(email, subject, body, email);
                return RedirectToAction("Confirmation", "Email");
            }
            catch (Exception)
            {
                return RedirectToAction("SendError", "Email");
            }
        }


This works fine on my local host, however, when I upload it to godaddy, the submit does not seem to do anything when pressed.
0
Comment
Question by:mmsi
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 6
12 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 38720482
Your POST action expects three parameters, but I don't see in your view where you are submitting three fields. Is this view supposed to post back to this particular POST action?
0
 

Author Comment

by:mmsi
ID: 38720503
Yes, this view is posting back to the Post action.  I was thinking that during the post the form fields were being passed.  If this is wrong, why does it work on my local host?
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 38720521
When you say that it works on localhost, do you actually have values in the email, subject, and body variables when the page posts bac?
0
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 

Author Comment

by:mmsi
ID: 38720998
To answer your question Yes, however, on the local host,  my smpt server denies access because I don't have permission to send emails.

So, for testing purposes, i commented out the webmail code.   When I press the submit button on the local host it takes me to:
http://localhost:10016/Email/Confirmation

When I run it on the live server and press the submit button  it seams the page just reloads

If you goto www.jobstreampro.com and click on the contact button and press the submit button  you can see what it does on the live server.

Question?  Do I need to register a route (in the Global.asax page) for the EmailController?  The only route I see if for "Home".
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 38721102
If your RegisterRoutes method shows:

routes.MapRoute(
    "Default",                                              // Route name
    "{controller}/{action}/{id}",                           // URL with parameters
    new { controller = "Home", action = "Index", id = "" }  // Parameter defaults
);

Open in new window


...then you shouldn't. The reason you see "Home" is because it is being designated as the default controller if no controller is detected in the URL. So when people browse using just the domain (e.g. www.jobstreampro.com), no controller is detected, so Home is selected.

In your case, I assume you have the EmailController at the same level as HomeController. I gather that from what you posted above:


Because of this, the routing of MVC detects "Email" as the controller, and "Confirmation" as the action. This is what the placeholders "{controller}" and "{action}" represent in the above snippet.

Let me ask you this:  Why post back to the same page? Why not have the action of your form pointed to the EmailController directly? To me, it would seem that you would want email-related activities within the EmailController.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 38721125
Can you post a screenshot of your view hierarchy? That is, can display the relationship within Solution Explorer of Home and Email? I tried to hit your Email controller directly (e.g. http://www.jobstreampro.com/Email/Confirmation) and it's coming back as a non-existent page.
0
 

Author Comment

by:mmsi
ID: 38721155
I won't be able to post a view hierarchy of the project I have on my local host until later tonight.  However, the following is the screen shots from the live server (godaddy).

Controllers Screenshot
Views Screenshot
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 38721232
Is "Confirmation" both the name of the .cshtml file and the name of the action (i.e. method) within the Email controller?
0
 

Author Comment

by:mmsi
ID: 38722376
Yes, both the file and the name of the action have the same name.

public ActionResult Confirmation()
        {  
            return View();
        }


"Let me ask you this:  Why post back to the same page? Why not have the action of your form pointed to the EmailController directly? To me, it would seem that you would want email-related activities within the EmailController."

How could I post back to the EmailController from the contact view?
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 38722456
How could I post back to the EmailController from the contact view?
Specify that info in the BeginForm method:

@using (Html.BeginForm("Confirmation", "Email"))

Open in new window


You would want, I imagine, to move the emailing code to that page, though. This is all dependent on your architecture and how you would like to organize things.

It seems as though you have been working at this since my previous test. Now it appears you have errors in your view. I can hit the http://www.jobstreampro.com/Email/Confirmation page, but it's displaying the internal error (be sure to turn this functionality off when you go live). You have the Confirmation view pointed to the Global.asax for a code-behind. That is not correct.
0
 

Accepted Solution

by:
mmsi earned 0 total points
ID: 38723152
I'm not sure if I could reward points for the answer to this question.  However, I want to thank you for taking the time to help me, and I appreciate the advice.

I removed all files from the live server and then uploaded new files.  When I press the submit button it is now displaying the confirmation page.
0
 

Author Closing Comment

by:mmsi
ID: 38734196
The problem appears not to be code related, and no expert actually gave me the answer I needed to solve this problem.  I simply deleted my files off the live server and re-uploaded the files.  This fixed my problem and this is the reason why I'm accepting my own solution.
0

Featured Post

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

628 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question