2010-09-30 86 views
0

我有一個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模型」傳遞到控件ller已經失去了它的一些價值... 基本上屏幕應該顯示一個對象列表,當你點擊「編輯」一個新的視圖應該打開顯示你點擊的1個對象以允許你編輯它 - 但它會丟失值。 幫助!

回答

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> 

要解決此我只是包括 「HiddenFor」 S對其他2個屬性:)

<%=Html.HiddenFor(Model => Model.RateValue)%> 
<%=Html.HiddenFor(Model => Model.StartDate)%> 

簡單,我認爲 - 希望它可以幫助別人。