Razor 将错误的值发送到隐藏字段中

Jor*_*dan 1 c# asp.net-mvc razor

我正在处理剃刀引擎中的一些异常行为。我的剃刀视图中有以下代码:

@using (Html.BeginForm("AddCabinetItem", "Builder"))
{
    @Html.AntiForgeryToken()

    @Html.HiddenFor(model => model.ID)
    @Html.HiddenFor(model => model.LibraryItemID)
    ...
Run Code Online (Sandbox Code Playgroud)

模型值如下:

Model = new CabinetItem {
    ID = 0,
    LibraryItemID = 155,
    ...
}
Run Code Online (Sandbox Code Playgroud)

我在 razor 视图本身的调试中验证了这些值是正确的。发出的 HTML 是错误的:

<input data-val="true" ... id="ID" name="ID" type="hidden" value="155">
<input data-val="true" ... id="LibraryItemID" name="LibraryItemID" type="hidden" value="155">
Run Code Online (Sandbox Code Playgroud)

Razor 向两个隐藏区域发射了 155。这里发生了什么?我没有用这些字段在 javascript 中做任何事情。这是一个错误还是我做错了什么?我正在 Visual Studio 2013 中使用 MVC 5。

这是CabinetItem的定义

public abstract class JobItem : IEquatable<JobItem>
{
    protected JobItem()
    {

    }

    protected JobItem(LibraryItem a_item)
    {
        #region Argument Validation

        if (a_item == null)
            throw new ArgumentNullException("a_item");

        #endregion

        LibraryItemID = a_item.WizardLibraryItemId;
        Name = a_item.Name;

        Quantity = 1;
    }

    protected JobItem(JobItem a_other)
    {
        #region Argument Validation

        if (a_other == null)
            throw new ArgumentNullException("a_other");

        #endregion

        ID = a_other.ID;
        LibraryItemID = a_other.LibraryItemID;
        Name = a_other.Name;

        Quantity = a_other.Quantity;
    }

    public int ID { get; set; }

    public int LibraryItemID { get; set; }

    public string Name { get; set; }

    [Required]
    [Display(ResourceType = typeof(CutReady.Web.Languages.Builder.Builder), Name = "QuantityLabel")]
    [Range(1, Int32.MaxValue, ErrorMessageResourceType = typeof(CutReady.Web.Languages.Builder.Builder), ErrorMessageResourceName = "QuantityRangeError")]
    public int Quantity { get; set; }

    public abstract JobItem Clone();

    public override bool Equals(object a_obj)
    {
        if (ReferenceEquals(null, a_obj)) 
            return false;

        if (ReferenceEquals(this, a_obj)) 
            return true;

        if (a_obj.GetType() != typeof (JobItem)) 
            return false;

        return Equals((JobItem) a_obj);
    }

    public virtual bool Equals(JobItem a_other)
    {
        if (ReferenceEquals(null, a_other)) 
            return false;

        if (ReferenceEquals(this, a_other)) 
            return true;

        return LibraryItemID == a_other.LibraryItemID;
    }

    public override int GetHashCode()
    {
        return LibraryItemID;
    }

}

public class CabinetItem : JobItem
{
    public CabinetItem()
    {

    }

    public CabinetItem(LibraryItem a_item)
        : base (a_item)
    {
        #region Argument Validation

        if (a_item == null)
            throw new ArgumentNullException("a_item");

        #endregion

        ItemWidth = a_item.ItemWidth;
        ItemDepth = a_item.ItemDepth;
        ItemHeight = a_item.ItemHeight;
        ItemWidthLock = new DimensionLock(a_item.ItemMinWidth, a_item.ItemMaxWidth);
        ItemHeightLock = new DimensionLock(a_item.ItemMinHeight, a_item.ItemMaxHeight);
        ItemDepthLock = new DimensionLock(a_item.ItemMinDepth, a_item.ItemMaxDepth);
    }

    public CabinetItem(CabinetItem a_other)
        : base(a_other)
    {
        #region Argument Validation

        if (a_other == null)
            throw new ArgumentNullException("a_cabinet");

        #endregion

        ItemWidth = a_other.ItemWidth;
        ItemDepth = a_other.ItemDepth;
        ItemHeight = a_other.ItemHeight;
        ItemWidthLock = new DimensionLock(a_other.ItemWidthLock.Minimum, a_other.ItemWidthLock.Maximum);
        ItemHeightLock = new DimensionLock(a_other.ItemHeightLock.Minimum, a_other.ItemHeightLock.Maximum);
        ItemDepthLock = new DimensionLock(a_other.ItemDepthLock.Minimum, a_other.ItemDepthLock.Maximum);
    }

    [Required]
    [Display(ResourceType = typeof(CutReady.Web.Languages.Builder.Builder), Name = "WidthLabel")]
    public double ItemWidth { get; set; }

    [Required]
    [Display(ResourceType = typeof(CutReady.Web.Languages.Builder.Builder), Name = "HeightLabel")]
    public double ItemHeight { get; set; }

    [Required]
    [Display(ResourceType = typeof(CutReady.Web.Languages.Builder.Builder), Name = "DepthLabel")]
    public double ItemDepth { get; set; }

    public DimensionLock ItemWidthLock { get; set; }
    public DimensionLock ItemHeightLock { get; set; }
    public DimensionLock ItemDepthLock { get; set; }

    public override JobItem Clone()
    {
        return new CabinetItem(this);
    }

    public override bool Equals(object a_obj)
    {
        if (ReferenceEquals(null, a_obj)) 
            return false;

        if (ReferenceEquals(this, a_obj)) 
            return true;

        if (a_obj.GetType() != typeof (CabinetItem)) 
            return false;

        return Equals((CabinetItem) a_obj);
    }

    public override bool Equals(JobItem other)
    {
        var cabinet = other as CabinetItem;

        if (cabinet == null)
            return false;

        if (ReferenceEquals(this, cabinet)) 
            return true;

        if (!base.Equals(cabinet))
            return false;

        if (!ItemWidth.Equals(cabinet.ItemWidth))
            return false;

        if (!ItemHeight.Equals(cabinet.ItemHeight))
            return false;

        if (!ItemDepth.Equals(cabinet.ItemDepth))
            return false;

        return true;
    }

    public override int GetHashCode()
    {
        unchecked
        {
            int hashCode = base.GetHashCode();

            hashCode = (hashCode * 397) ^ ItemWidth.GetHashCode();
            hashCode = (hashCode * 397) ^ ItemHeight.GetHashCode();
            hashCode = (hashCode * 397) ^ ItemDepth.GetHashCode();

            return hashCode;
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

Chr*_*att 5

对这种行为的唯一合乎逻辑的解释是,您在视图数据中有一些内容覆盖了属性中的值。如果存在以下任何一项且值为 155,则将使用该值:

  • ViewBag.ID
  • ViewData["ID"]
  • Request["ID"]

另外,请记住这些都是不区分大小写的,即ViewBag.id也将用于填充值。