我正在用ASP.NET MVC4写这篇文章,并且一直在尝试在表单中有一个字段,其中一个字段被排除在写入之外。我在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当我在浏览器中进入表单并输入数据时,它仍然显示“(变量名称)是必需的”;如下所示:

为什么绑定排除功能不起作用?
发布于 2013-10-11 01:56:20
在我学习本教程时,我意识到AuctionsController中Create函数的<Bind(Exclude:="CurrentPrice")>部分并不是导致CurrentPrice字段仍然是必需字段的问题。
虽然我使用的是MVC4而不是C#,但这里的帖子回答了我的问题:What does a questionmark (?) mean in a function declaration in C#
这是在变量类型周围使用问号,比如:Decimal?,这样当用户在网页上填写表单时,变量就不是必需的了。
希望这对和我有同样问题的人有所帮助!
https://stackoverflow.com/questions/17115723
复制相似问题