C# Razor将错误的值发射到隐藏字段中
我正在处理剃须刀引擎的一些异常行为。我的razor视图中有以下代码:C# Razor将错误的值发射到隐藏字段中,c#,asp.net-mvc,razor,C#,Asp.net Mvc,Razor,我正在处理剃须刀引擎的一些异常行为。我的razor视图中有以下代码: @using (Html.BeginForm("AddCabinetItem", "Builder")) { @Html.AntiForgeryToken() @Html.HiddenFor(model => model.ID) @Html.HiddenFor(model => model.LibraryItemID) ... 模型的价值如下: Model = new Cabin
@using (Html.BeginForm("AddCabinetItem", "Builder"))
{
@Html.AntiForgeryToken()
@Html.HiddenFor(model => model.ID)
@Html.HiddenFor(model => model.LibraryItemID)
...
模型的价值如下:
Model = new CabinetItem {
ID = 0,
LibraryItemID = 155,
...
}
我在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">
剃刀射入两个隐藏的领域。这是怎么回事?我没有在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;
}
}
}
公共抽象类JobItem:IEquatable
{
受保护的作业项()
{
}
受保护的作业项(库项a\U项)
{
#区域参数验证
if(a_项==null)
抛出新的ArgumentNullException(“a_项”);
#端区
LibraryItemID=项目向导LibraryItemID;
Name=a_item.Name;
数量=1;
}
受保护的作业项(作业项a_其他)
{
#区域参数验证
if(a_other==null)
抛出新的异常(“a_other”);
#端区
ID=a_other.ID;
LibraryItemID=a_其他LibraryItemID;
Name=a_other.Name;
数量=其他数量;
}
公共int ID{get;set;}
public int LibraryItemID{get;set;}
公共字符串名称{get;set;}
[必需]
[显示(ResourceType=typeof(CutReady.Web.Languages.Builder.Builder),Name=“QuantityLabel”)]
[Range(1,Int32.MaxValue,ErrorMessageResourceType=typeof(CutReady.Web.Languages.Builder.Builder),ErrorMessageResourceName=“QuantityRangeError”)]
公共整数数量{get;set;}
公共抽象作业项克隆();
公共覆盖布尔等于(对象a_obj)
{
if(ReferenceEquals(null,a_obj))
返回false;
if(ReferenceEquals(this,a_obj))
返回true;
if(a_obj.GetType()!=typeof(JobItem))
返回false;
返回等于((作业项)a_obj);
}
公共虚拟布尔等于(作业项a_其他)
{
if(ReferenceEquals(null,a_其他))
返回false;
if(ReferenceEquals(this,a_other))
返回true;
return LibraryItemID==a_other.LibraryItemID;
}
公共覆盖int GetHashCode()
{
返回LibraryItemID;
}
}
公共类CabinetItem:JobItem
{
公共橱柜项目()
{
}
公共橱柜项目(图书馆项目a\U项目)
:基本(a_项)
{
#区域参数验证
if(a_项==null)
抛出新的ArgumentNullException(“a_项”);
#端区
ItemWidth=a_item.ItemWidth;
ItemDepth=a_item.ItemDepth;
ItemHeight=a_item.ItemHeight;
ItemWidthLock=新尺寸锁(a_item.ItemMinWidth,a_item.ItemMaxWidth);
ItemHeightLock=新尺寸锁(a_item.ItemMinHeight,a_item.ItemMaxHeight);
ItemDepthLock=新尺寸锁(a_item.ItemMinDepth,a_item.ItemMaxDepth);
}
公共橱柜项目(橱柜项目a_其他)
:底座(a_其他)
{
#区域参数验证
if(a_other==null)
抛出新的异常(“a_内阁”);
#端区
ItemWidth=a_other.ItemWidth;
ItemDepth=a_other.ItemDepth;
ItemHeight=a_other.ItemHeight;
ItemWidthLock=新尺寸锁(a_其他.ItemWidthLock.Minimum,a_其他.ItemWidthLock.Maximum);
ItemHeightLock=新尺寸锁(a_其他.ItemHeightLock.Minimum,a_其他.ItemHeightLock.Maximum);
ItemDepthLock=新尺寸锁(a_其他.ItemDepthLock.Minimum,a_其他.ItemDepthLock.Maximum);
}
[必需]
[显示(ResourceType=typeof(CutReady.Web.Languages.Builder.Builder),Name=“WidthLabel”)]
公共双项目宽度{get;set;}
[必需]
[显示(ResourceType=typeof(CutReady.Web.Languages.Builder.Builder),Name=“HeightLabel”)]
公共双项目高度{get;set;}
[必需]
[显示(ResourceType=typeof(CutReady.Web.Languages.Builder.Builder),Name=“DepthLabel”)]
公共双项目深度{get;set;}
公共维度锁ItemWidthLock{get;set;}
公共维度锁项高度锁{get;set;}
公共维度锁ItemDepthLock{get;set;}
公共重写作业项克隆()
{
返回新的橱柜项目(本);
}
公共覆盖布尔等于(对象a_obj)
{
if(ReferenceEquals(null,a_obj))
返回false;
if(ReferenceEquals(this,a_obj))
返回true;
if(a_obj.GetType()!=typeof(CabinetItem))
返回false;
返回等于((细木工项目)a_obj);
}
公共覆盖布尔等于(作业项其他)
{
var机柜=其他机柜项目;
如果(CAB==null)
返回false;
if(参考等于(本、内阁))
返回true;
如果(!base.Equals(CAB))
返回false;
如果(!ItemWidth.Equals(cabinet.ItemWidth))
返回false;
如果(!ItemHeight.Equals(cabinet.ItemHeight))
返回false;
如果(!ItemDepth.Equals(cabinet.ItemDepth))
返回false;
返回true;
}
公共覆盖int GetHashCode()
{
未经检查
{
int hashCode=base.GetHashCode();
hashCode=(hashCode*397)^ItemWidth.GetHashCode();
hashCode=(hashCode*397)^ItemHeight.GetHashCode();
hashCode=(hashCode*397)^ItemDepth.GetHashCode();
返回哈希码;
}
}
}
对这种行为唯一合乎逻辑的解释是,视图数据中的某些内容覆盖了属性中的值。如果存在以下任何值为155的情况,则将使用该值:
ViewBag.ID
ViewDat