MVC 3 Problem, EditorFor() TextBoxFor()
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 the TextBoxFor
, the browser never sends that value back.
inputs that are disabled do not post back. it is part of html, you will never get the value for this. If you want it to post back try readonly.
Use TextBoxFor() method and as the disabled object does not post on server you can add css class for that TextBoxFor control as .disabled-control { display: none }
精彩评论