视图强类型化到视图模型在回发时不会将模型返回到控制器

发布于 2024-09-25 20:51:18 字数 3044 浏览 0 评论 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 model”已经丢失了它的一些值... 基本上,第一个屏幕应显示对象列表,当您单击“编辑”时,应打开一个新视图,显示您单击的 1 个对象以允许您对其进行编辑 - 但它会丢失值。 帮助!

I have a PartialView strongly typed to a view model

<%@ 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>

Heres the viewmodel

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; }

}

And heres the Controller method...

  [ActionName("Edit")]
    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Edit(string button, RateControlEditViewModel model)
    { // Do Stuff here 
    }

My problem is the object "RateControlEditViewModel model" passed into the controller has lost some of its values...
Basically screen one should show a list of objects, when you click "Edit" a new view should open displaying the 1 object you clicked on to allow you to edit it - but its losing the values.
Help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浮生面具三千个 2024-10-02 20:51:18

好的,
看来,如果您使用“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)%>

我认为很简单 - 希望它对其他人有帮助。

Ok,
It seems that if you are using "DisplayFor", the model attributes aren't populated.
Using my example below, the values I receive back to the controller are those in the "HiddenFor" objects, but not those in the "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>

To resolve this I just included "HiddenFor"s for the other 2 attributes :)

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

Simple I think - hope it helps someone else.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文