我遇到的問題是當我將填充對象傳遞給不顯示所有屬性的視圖時。未在視圖中使用的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