When you have an ASP.Net MVC form created by Html.BeginForm(), how do fields inside it get populated? In the case of
<asp:Content ID="Main" ContentPlaceHolderID="MainContent" runat="server">
<h2>Edit Dinner</h2>
<%= Html.ValidationSummary("Please correct the errors and try again.") %>
<% using (Html.BeginForm()) { %>
<p>
<label for="Title">Dinner Title:</label>
<%= Html.TextBox("Title") %>
<%= Html.ValidationMessage("Title", "*") %>
</p>
Where does the value for Html.TextBox("Title") come from? I know, the model. And in this case the model does have a Title field. But I have cases where I have to create a view model so I end up having something like:
public class DinnerViewModel {
public Dinner Dinner {get; set;};
public SomethingElse SomethingElse {get; set;};
}
and use that as a model, the <%= Html.TextBox("Title") %> gets the right value. Or at least it does if the validation fails and the form is re-displayed.
I'm asking because I have a case of a form that is being submitted via AJAX (using Ajax.BeginForm()) and the form gets refreshed. I'm creating a new model object, empty, for it, but it's still loading the values of the last submission. When I submit without AJAX everything works fine.
Update
I'm watching the debugger in the template. Model.Dinner.Title equals "" yet Html.TextArea("Title", Model.Dinner.Title) puts the previous value that was submitted inside the text area.