我在 ASP.NET MVC 4 中编写此代码,并且一直试图在表单中设置一个字段,其中一个字段被排除在写入之外。我在 AuctionsController.vb 文件中有以下模型,它控制表单:
Function Create(<Bind(Exclude:="CurrentPrice")> test_auction As Models.Auction) As ActionResult
Dim categoryList = New SelectList(New Object() {"Automotive", "Electronics", "Games", "Home"})
ViewBag.CategoryList = categoryList
Return View()
End Function
这就是视图 Create.vbhtml 的样子:
lType MVCAuction.Models.Auction
@Code
ViewData("Title") = "Create"
End Code
<h2>Create</h2>
@Using Html.BeginForm()
@*@Html.AntiForgeryToken()*@
@Html.ValidationSummary(True)
@<fieldset>
<legend>Auction</legend>
<div class="editor-label">
@Html.LabelFor(Function(model) model.Category)
</div>
<div class="editor-field">
@*@Html.EditorFor(Function(model) model.Category)*@
@Html.DropDownListFor(Function(model) model.Category, DirectCast(ViewBag.CategoryList, SelectList))
@Html.ValidationMessageFor(Function(model) model.Category)
</div>
<div class="editor-label">
@Html.LabelFor(Function(model) model.Title)
</div>
<div class="editor-field">
@Html.EditorFor(Function(model) model.Title)
@Html.ValidationMessageFor(Function(model) model.Title)
</div>
<div class="editor-label">
@Html.LabelFor(Function(model) model.Description)
</div>
<div class="editor-field">
@Html.EditorFor(Function(model) model.Description)
@Html.ValidationMessageFor(Function(model) model.Description)
</div>
<div class="editor-label">
@Html.LabelFor(Function(model) model.ImageURL)
</div>
<div class="editor-field">
@Html.EditorFor(Function(model) model.ImageURL)
@Html.ValidationMessageFor(Function(model) model.ImageURL)
</div>
<div class="editor-label">
@Html.LabelFor(Function(model) model.StartTime)
</div>
<div class="editor-field">
@Html.EditorFor(Function(model) model.StartTime)
@Html.ValidationMessageFor(Function(model) model.StartTime)
</div>
<div class="editor-label">
@Html.LabelFor(Function(model) model.EndTime)
</div>
<div class="editor-field">
@Html.EditorFor(Function(model) model.EndTime)
@Html.ValidationMessageFor(Function(model) model.EndTime)
</div>
<div class="editor-label">
@Html.LabelFor(Function(model) model.StartPrice)
</div>
<div class="editor-field">
@Html.EditorFor(Function(model) model.StartPrice)
@Html.ValidationMessageFor(Function(model) model.StartPrice)
</div>
<div class="editor-label">
@Html.LabelFor(Function(model) model.CurrentPrice)
</div>
<div class="editor-field">
@Html.EditorFor(Function(model) model.CurrentPrice)
@Html.ValidationMessageFor(Function(model) model.CurrentPrice)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
End Using
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@Section Scripts
@Scripts.Render("~/bundles/jqueryval")
End Section
当我在浏览器中进入表单并输入数据时,它仍然显示“需要(变量名称)”;如下所示:
为什么绑定排除功能不起作用?