<bind(exclude:=“current price”)>在MVC 4中不起作用</bind(exclude:=“current price”)>

时间:2013-06-14 19:14:14

标签: asp.net-mvc vb.net asp.net-mvc-4 data-binding visual-studio-2012

我正在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

当我在浏览器中进入表单并输入数据时,它仍然显示“(变量的名称)是必需的”;如下所示:  the field is still required

为什么绑定排除功能不起作用?

1 个答案:

答案 0 :(得分:1)

在我学习本教程时,我意识到AuctionsController中的Create函数的<Bind(Exclude:="CurrentPrice")>部分不是导致仍然需要CurrentPrice字段的问题。

虽然我使用的是MVC 4而不是C#,但这里的帖子回答了我的问题:What does a questionmark (?) mean in a function declaration in C#

使用变量类型周围的问号,例如:Decimal?,因此当用户填写网页上的表单时,它不需要变量。

希望这可以帮助那些与我有同样问题的人!