我遇到的问题是当我将填充对象传递给不显示所有属性的视图时。未在视图中使用的MVC 2模型属性返回为空或空
public ActionResult Edit(Guid clientID)
{
Property property = PropertyModel.GetPropertyDetails(clientID);
return View("Edit", "Site", property);
}
检视:
<%@ Page Title="" Language="C#" Inherits="System.Web.Mvc.ViewPage<WebFrontend.ViewModels.Property>" %>
<% using (Html.BeginForm())
{%>
<%: Html.ValidationSummary(true, "Property update was unsuccessful. Please correct the errors and try again.") %>
<fieldset>
<legend>Edit Account: <%: Model.ClientAccountNo %></legend>
<div class="editor-label">
<%: Html.LabelFor(model => model.ClientName)%>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.ClientName)%>
<%: Html.ValidationMessageFor(model => model.ClientName)%>
</div>
<p>
<input type="submit" value="Update Property" />
</p>
</fieldset>
<% } %>
当提交的属性对象被传递给该控制器的方法,但所有的视图中未使用的属性是空值或空白包括Model.ClientAccountNo其是存在于提交前查看。
[HttpPost]
public ActionResult Edit(Property property)
{
if (ModelState.IsValid)
{
bool success = PropertyModel.UpdateProperty(property);
if (success)
{
// property has been updated, take them to the property details screen.
return RedirectToAction("Details", "Property", new { clientID = property.ClientID });
}
else
{
ModelState.AddModelError("", "Could not update property.");
return View("Activate", "Site", property);
}
}
// If we got this far, something failed, redisplay form
return View("Edit", "Site", property);
}
我在网上找不到任何类似的东西,任何解释为什么会发生这种情况,以及如何解决它将不胜感激。
好吧,我现在看到,我有一种感觉,它不能以这种方式工作。我想我会结束视图模型每个视图路线只是意味着很多左手 - 右手编码。 – WillMcKill 2011-03-16 16:35:02