I'm working on my very first ASP.NET MVC project, and I'm seeing strange behavior when returning data from an Edit View to my controller action.
Right now, I have 3 textboxes on the page, plus one hidden one for the PKey. All are being populated from the viewdata correctly, but when I submit the form, only 2 of the 3 fields show up in the returned model. However, all three fields are populated correctly in the Request Object.
I'm probably not explaining it very well, but here's some pertinent code snips to hopefully better explain:
public ActionResult Edit(System.Guid Id)
{
SetBase sb = setBaseRepository.Get(Id);
return View("Edit", sb );
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(SetBase setBase)
{
//if (setBase.Title.Trim().Length == 0)
//{
// ModelState.AddModelError("Title", "Title is required.");
//}
if (setBase.Year.Trim().Length == 0)
{
ModelState.AddModelError("Year", "Year is required.");
}
if (!ModelState.IsValid)
{
return View("Edit", setBase);
}
setBaseRepository.SaveOrUpdate(setBase);
return View();
}
And here's the 'meat' of the View Itself:
<viewdata model="CardTracker.Core.SetBase">
<content name="MainContent">
<% MvcForm form = Html.BeginForm("Edit", "SetBase", Model.Id); %>
<%=Html.ValidationSummary("Update was unsuccessful. Please correct the errors and try again.", new { class = "dataEntryError" })%>
<fieldset>
<legend class="dataEntry">Edit Set Base</legend>
<div>
!{Html.Hidden("Id")}
<label class="dataEntry" for="Title" >Title: </label> ${Html.TextBox("Title", Model.Title, new { class = "dataEntryLong" })}
<%=Html.ValidationMessage("Title", "***", new { class = "dataEntryError" })%>
</br>
<label class="dataEntry" for="Year">Year:</label>${Html.TextBox("Year", null, new { class = "dataEntryNumber" })}
<%=Html.ValidationMessage("Year", "***", new { class = "dataEntryError" })%>
</br>
</div>
<input type="submit" value="Update" class="button" />
</fieldset>
<% form.EndForm(); %>
</content>
The 'Id' and 'Year' fields are returned just fine, but 'Title' always comes back blank. I have verified that all of them are spelled correctly everywhere.
I'm sure I'm doing something obviously wrong, but I don't see it. The numerous examples I have studied aren't helping, and most of them show Add functionality instead of Edit.
Thanks in advance for any and all help.