我在使用@Html.HiddenFor
帮助器在Razor中创建的表单上有一个隐藏字段:
@Html.HiddenFor(model => model.BidID, new { id="bidItemID" })
My View模型如下所示:
public class BidEditVM
{
[Display(Name = "Bid ID")]
public int BidID { get; set; }
[StringLength(51)]
[Display(Name = "Customer Name")]
public string CustomerName { get; set; }
[StringLength(75)]
[Display(Name = "Bid Name")]
public string BidName { get; set; }
[Display(Name = "Amount")]
public decimal Amount { get; set; }
[Display(Name = "Time")]
public DateTime BidTime { get; set; }
}
当呈现HTML时,不引人注目的javascript会将其添加到隐藏的输入字段中,即使它永远不需要验证:
<input id="bidItemID" type="hidden" value="5198" name="BidID" data-val-required="The Bid ID field is required." data-val-number="The field Bid ID must be a number." data-val="true">
更奇怪的是,它添加的消息和验证甚至不是此局部视图的视图模型的一部分。视图如下所示:
@model AuctionAdmin.Models.ViewModels.BidEditVM
@using (Ajax.BeginForm("UpdateBid", new AjaxOptions { HttpMethod = "Post", UpdateTargetId = "modalBidInfo" }))
{
@Html.ValidationSummary(true)
@Html.HiddenFor(model => model.BidID, new { id="bidItemID" })
<fieldset>
<legend>Edit Bid</legend>
<div class="display-label">@Html.LabelFor(model => model.CustomerName)</div>
<div class="display-field">
@Html.DisplayFor(model => model.CustomerName)
</div>
<div class="display-label">@Html.LabelFor(model => model.BidName)</div>
<div class="display-field">
@Html.DisplayFor(model => model.BidName)
</div>
<div class="editor-label">@Html.LabelFor(model => model.Amount)</div>
<div class="editor-field">
@Html.EditorFor(model => model.Amount)
</div>
<div class="editor-label">@Html.LabelFor(model => model.BidTime)</div>
<div class="editor-field">
@Html.EditorFor(model => model.BidTime)
</div>
</fieldset>
}
从哪里获取此元数据以及如何阻止它?
答案 0 :(得分:1)
由于视图模型中的类型是int。
,因此标记为由于这一行,它正在添加html:
@Html.HiddenFor(model => model.BidID, new { id="bidItemID" })
为什么存在额外属性是一个问题? 如果确实有问题,请尝试将BidId的类型更改为int? (可以为空的)。