I am working on my first MVC Webapplication (using Razor and C#) and I have run across a strange behaviour.
I am editing a "line" of data and using ajax calls to submit and redisplay data. Everything works fine as far as changing existing data and storing it goes. Also if I just redisplay the "line" that was submitted no problems.
However, I want to display a "new" line with some of the values from the old line retained and the rest blanked out.
However, when I submit the new line object to the Partial View, the "blanked" out values aren't being picked up by the @Html.... helpers. But if I display the property of the Model directly it has the correct (blank) value.
Here are the relevant sections of my code: Controller Method: [HttpPost] public ActionResult EditLineForm(SkuRequestLine ln) { SkuRequestLine newline = null;
try
{
if (ln.Store(true))
{
ViewData["prodcatdesc"] = DataConnection.GetProductCategory(ln.Category).description;
newline = new SkuRequestLine();
newline.Copy(ln);
newline.Line = DataConnection.NextSkuRequestLineNumber(ln.Request);
newline.Comments = "";
newline.Description = "";
newline.Vendorsku = "";
return PartialView("EditLineForm", newline); // this line is being executed.
}
else
{
return PartialView("EditLineForm", ln);
}
}
catch (Exception ex)
{
List<string> msgs = new List<string>();
while (ex != null)
{
msgs.Add(ex.Message);
ex = ex.InnerException;
}
return PartialView("EditLineForm", ln);
}
}
Razor Code:
@model Sku_Management.Models.SkuRequestLine
@using (Ajax.BeginForm("EditLineForm", "SkuRequest", new AjaxOptions { OnSuccess = "UpdateLineList" }))
{
.
.
.
<tr>
<td>
<span class="editor-label">
开发者_C百科 @Html.LabelFor(model => model.Description)
</span>
</td>
<td colspan="5">
<span class="editor-field">
@Html.TextBoxFor(model => model.Description, new { @class = "fortywide" }) // Displays the Description from the edited Line passed in. Not what what Model.Description is.
@Html.ValidationMessageFor(model => model.Description)
</span>
<span>|@Model.Description|</span> // Displays "||" which is what it should be since Model.Description is blank.
</td>
</tr>
The only thing I can think of is that model => model.Description is using a cached version of the Model not the new Model passed into the PartialView call.
I have spent the day searching for anything even similar on the web but I can't find anything that even begins to describe this behavior.
Has anyone else encountered this and knows what I am dong wrong?
Thanks
This is because the HTMLHelpers look to the ModelState for values before using the Model.
You'll have to clear the ModelState entries to get this to work.
精彩评论