Link to home
Start Free TrialLog in
Avatar of mmsi
mmsi

asked on

MVC 3 Submit Button not working properly

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.
Avatar of kaufmed
kaufmed
Flag of United States of America image

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?
Avatar of mmsi
mmsi

ASKER

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?
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?
Avatar of mmsi

ASKER

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".
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.
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.
Avatar of mmsi

ASKER

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).

User generated image
User generated image
Is "Confirmation" both the name of the .cshtml file and the name of the action (i.e. method) within the Email controller?
Avatar of mmsi

ASKER

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?
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.
ASKER CERTIFIED SOLUTION
Avatar of mmsi
mmsi

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mmsi

ASKER

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.