Solved

MVC 3 Submit Button not working properly

Posted on 2012-12-25
12
1,091 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
  • 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
 

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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
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…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

930 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now