我有一个强烈输入视图模型的PartialView
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<VATRateManager_MVC.Models.ViewModels.RateControlEditViewModel>" %>
<script src="../../Scripts/MicrosoftAjax.js" type="text/javascript"></script>
<script src="../../Scripts/MicrosoftMvcValidation.js" type="text/javascript"></script>
<% Html.EnableClientValidation(); %>
<% using (Html.BeginForm("Edit", "Rate", FormMethod.Post))
{ %>
<%=Html.ValidationSummary() %>
<%=Html.HiddenFor(Model => Model.ParentID)%>
<%=Html.HiddenFor(Model => Model.ParentTypeID)%>
<%=Html.HiddenFor(Model => Model.ParentTypeDescription)%>
<%=Html.HiddenFor(Model => Model.RateID)%>
<div >
<%=Html.LabelFor(Model => Model.RateValue)%>
<%=Html.DisplayFor(Model => Model.RateValue) %>
</div>
<div>
<%=Html.LabelFor(Model => Model.StartDate)%>
<%=Html.DisplayFor(Model => Model.StartDate, new { @class = "date" })%>
<%-- <%=Html.EditorFor(Model => Model.StartDate, new { @class = "date" })%>--%>
</div>
<div>
<input type="submit" name="button" value="Edit"/>
</div>
继承人视图模型
public class RateControlEditViewModel
{
/// <summary>The parent ID is the Rate ID for the Current record. </summary>
[HiddenInput(DisplayValue = false)]
public int ParentID { get; set; }
/// <summary>The parent Type ID is the Type ID for the Current record. </summary>
[HiddenInput (DisplayValue= false)]
public int ParentTypeID { get; set; }
[HiddenInput(DisplayValue = false)]
public string ParentTypeDescription { get; set; }
/// <summary>Rate ID for current record, may be null if adding new rate. </summary>
[HiddenInput(DisplayValue = false)]
public int? RateID { get; set; }
/// <summary>Percentage VAT Rate to 2 decimal places. </summary>
[DisplayName("VAT Rate (%)")]
[Required(ErrorMessage ="Please Supply VAT Rate as percentage value")]
[Range(typeof(Decimal), "0","100")]
public decimal RateValue { get; set; }
/// <summary>Start date from which this VAT Rate will be active, the start date will also be
/// the same value for the previous records End Date to ensure there are no gaps in the Rate.</summary>
[DisplayName("VAT Rate Start Date")]
[DataType(DataType.Date)]
[Required(ErrorMessage="Please Supply Start date from which this VAT rate will become active")]
public DateTime? StartDate { get; set; }
}
继承人控制器方法......
[ActionName("Edit")]
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(string button, RateControlEditViewModel model)
{ // Do Stuff here
}
我的问题是传递给控制器的对象“RateControlEditViewModel模型”丢失了一些值...... 基本上屏幕应显示对象列表,当您单击“编辑”时,应打开一个新视图,显示您单击的1个对象以允许您编辑它 - 但它会丢失值。 救命啊!
答案 0 :(得分:0)
确定, 似乎如果您使用“DisplayFor”,则不会填充模型属性。 使用下面的示例,我收到的控制器值是“HiddenFor”对象中的值,而不是“DisplayFor”中的值。
<%=Html.HiddenFor(Model => Model.ParentID)%>
<%=Html.HiddenFor(Model => Model.ParentTypeID)%>
<%=Html.HiddenFor(Model => Model.ParentTypeDescription)%>
<%=Html.HiddenFor(Model => Model.RateID)%>
<div >
<%=Html.LabelFor(Model => Model.RateValue)%>
<%=Html.DisplayFor(Model => Model.RateValue) %>
</div>
<div>
<%=Html.LabelFor(Model => Model.StartDate)%>
<%=Html.DisplayFor(Model => Model.StartDate, new { @class = "date" })%>
</div>
要解决此问题,我只为其他2个属性添加了“HiddenFor”:)
<%=Html.HiddenFor(Model => Model.RateValue)%>
<%=Html.HiddenFor(Model => Model.StartDate)%>
简单我认为 - 希望它可以帮助别人。