During a postback (lets assume validation errors) my text input fields do not display trimmed values even though I specify them to be so in my model (see simple model below). The values are indeed trimmed under my controller, debug.writeline() shows them so, but that isn't being reflected in the view.
How do I get that trimming to reflect in my view (within an Input Field) after a postback?
Simple model:
private string _name;
public string Name {
get { return this._name; }
set { this._name = (value == null) ? "" : value.Trim(); }
}
Simple controller:
public ActionResult Index() {
return View();
}
[HttpPost]
public ActionResult Index([Bind(Include="Name,City,State")] Model model) {
Debug.WriteLine("Name: " + model.Name); // trimmed!
return View(model);
}
Simple view:
@using(Html.BeginForm()) {
@Html.EditorFor(m => m.Name); // not trimmed!
@Html.ValidationMessageFor(m => m.Name);
}
UPDATE: In my Simple Controller, the HttpPost method, I'm passing my model to the view "return View(model)". In my view I can reference that object simply by doing "Model.Name" or "@Model.Name" and when I do so, I see that it is trimmed. The problem however still remains because I do not understand how to reference the passed in object (model) under @Html.LabelFor, @Html.EditorFor helpers? I did try using @Html.Label and @Html.Editor in some creative ways, but that didn't work either. If I understand the helper objects, then @Html.EditorFor(m => m.Name) is actually not referencing the passed in object (model) but instead creating a new reference to it.
Under View:
@{
Layout = null;
if (Model != null) {
Debug.WriteLine("From View: _" + Model.Name + "_"); // trimmed !
}
}