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

时间:2015-02-18 15:52:30

标签: c# asp.net-mvc razor

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

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

    @Html.HiddenFor(model => model.ID)
    @Html.HiddenFor(model => model.LibraryItemID)
    ...

模型的价值如下:

Model = new CabinetItem {
    ID = 0,
    LibraryItemID = 155,
    ...
}

我在剃刀视图本身的调试中验证了这些值是正确的。发出的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">

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

}

1 个答案:

答案 0 :(得分:3)

此行为的唯一合理解释是,您在视图数据中有一些内容覆盖了属性的值。如果以下任何一个值为155,则将使用该值:

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

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