Solved

MVC 3 Submit Button not working properly

Posted on 2012-12-25
12
1,068 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 74

Expert Comment

by:käµfm³d 👽
Comment Utility
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
Comment Utility
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 74

Expert Comment

by:käµfm³d 👽
Comment Utility
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
Comment Utility
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 74

Expert Comment

by:käµfm³d 👽
Comment Utility
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 74

Expert Comment

by:käµfm³d 👽
Comment Utility
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:mmsi
Comment Utility
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 74

Expert Comment

by:käµfm³d 👽
Comment Utility
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
Comment Utility
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 74

Expert Comment

by:käµfm³d 👽
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
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…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

762 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

12 Experts available now in Live!

Get 1:1 Help Now