Getting up to speed on MVC 3,
Having a problem with an [HttpPost] ActionResult method seeing passed data, depending on whether I use EditorFor() or TextBoxFor() in my view.
I have some fields in my Edit View that I want to be read-only so I have used:
<div class="editor-field">
@Html.EditorFor(model => model.ModelNumber)
@*Html.TextBoxFor(model => model.ModelNumber, new { disabled = "disabled", @readonly = "readonly" })*@
@*Html.ValidationMessageFor(model => model.ModelNumber)*@
</div>
Here is the ActionResult controller methods:
public ActionResult Edit(int id)
{
var NPSProc = db.NPSProcesseds.SingleOrDefault(p => p.Id == id);
return View(NPSProc);
}
[HttpPost]
public ActionResult Edit(NPSProcessed Processed)
{
try
{
if (ModelState.IsValid)
{
db.Entry(Processed).State = EntityState.Modified;
db.SaveChanges();
return new RedirectResult("~/Home", false);
}
else
{
return View("Edit", Processed);
}
}
catch (DbEntityValidationException dbEx)
{
foreach (var validationErrors in dbEx.EntityValidationErrors)
{
foreach (var validationError in validationErrors.ValidationErrors)
{
Trace.TraceInformation("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage);
}
}
return View("Edit", Processed);
}
When I remove the comments on the EditorFor() method, the ModelNumber field contains a value ModelNumber in the Processed object passed to the HttpPost ActionResult Method.
If I comment the EditorFor() method and remove the comment on the TextBoxFor() method
the MethodNumber field contains a null.
It appears that EditorFor() Method is performing some additional work that TextBoxFor() method is not doing.
Could someone point me in the correct direction and point out the error that I am making.
Thanks
Joe
Disabled
<input>elements aren’t posted back to the server.Because you set
disabled="disabled"in theTextBoxFor, the browser never sends that value back.