I am having problems getting my form to Post to my Save method in a controller. I am new to MVC, and have followed several examples to try to get this to work. Here is my html markup:
<form action='Edit/Save' method='post'> <fieldset> <legend>Personal Information</legend> <table class='editGrid'> <tr> <td><label for='txtFirstName'>First Name:</label></td> <td><input type='text' id='txtFirstName' value='<%=user.FirstName %>' name='FirstName' /></td> </tr> <tr> <td><label for='txtLastName'>Last Name:</label></td> <td><input type='text' id='txtLastName' value='<%=user.LastName %>' name='LastName' /></td> </tr> <tr> <td><label for='txtNtLogin'>NT Login:</label></td> <td><input type='text' id='txtNtLogin' value='<%=user.NtLogin %>' name='NtLogin' /></td> </tr> <tr> <td><label for='txtHireDate'>Hire Date:</label></td> <td><input type='text' id='txtHireDate' value='<%=string.Format('{0:d}',user.HireDate) %>' name='HireDate' /></td> </tr> </table> </fieldset> <fieldset> <legend>Job Information</legend> <table class='editGrid'> <tr> <td><label for='CostCenters'>Cost Center:</label></td> <td><%=Html.DropDownList('CostCenters')%></td> </tr> <tr> <td><label for='Managers'>Manager:</label></td> <td><%=Html.DropDownList('Managers')%></td> </tr> <tr> <td><label for='Responsibilities'>Responsibility:</label></td> <td><%=Html.DropDownList('Responsibilities')%></td> </tr> <tr> <td><label for='Departments'>Department:</label></td> <td><%=Html.DropDownList('Departments')%></td> </tr> <tr> <td><label for='Active'>Active:</label></td> <td><%=Html.CheckBox('Active',user.Active) %></td> </tr> <tr> <td><label for='txtHireDate'>Hire Date:</label></td> <td><%=Html.TextBox('txtHireDate',string.Format('{0:d}',user.HireDate)) %></td> </tr> <tr> <td><label for='txtReleaseDate'>Release Date:</label></td> <td><%=Html.TextBox('txtReleaseDate',string.Format('{0:d}',user.ReleaseDate)) %></td> </tr> </table> </fieldset> <input type='submit' value='Save Changes' /> </form>
This form routes to a Save method in my EditController. Here is the code for my EditController’s Save method:
public class EditController : Controller { public ActionResult Save() { //Save code goes here }
I have tried using the html form tag, and also the Html helper code:
using (Html.BeginForm('Save', 'Edit'))
Here is the entry from my RegisterRoutes method in the Global.asax file:
routes.MapRoute('EditSave', '{controller}/Save', new { controller = 'Edit', action = 'Save' });
No matter what I do, the submit button does not trigger the Save method. Yet, if I manually key in the Url, The code breaks right into the Save method.
Edit: Per Craig Stuntz’s comment, I checked the source of the page. The page actually contains 2 forms, although only 1 is coded on the page by myself: Here is the HTML that appears prior to my form tag:
<form name='aspnetForm' method='post' action='44' id='aspnetForm'> <div> <input type='hidden' name='__VIEWSTATE' id='__VIEWSTATE' value='/wEPDwUJNjM3OTAyNTUzZGQrHhVn9+t78aHxN0vHvKUJ8DQWlQ==' /> </div> <div id='nav'> <span id='navLinks'> <a href='#'>Placeholder Link</a> </span> <span id='userName'> <span id='ctl00_lblUserName' class='UserName'>Welcome, Test User</span> </span> </div> <div id='Content'> <div id='formContainer'> <form action='Edit/Save' method='post'>
I didn’t think MVC generated viewstate or additional form tags. I am pulling data and populating it into this form using another method from the same controller. Am I doing something wrong here?
Ok, got the answer. And thanks Craig for having me look at the HTML again! My master page had generated a form tag in it without my knowing it, so I essentially had nested forms on the same page. After I removed the form from the Master Page, everything worked perfectly.