0

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

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

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

4

1 回答 1

1

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

虽然我使用的是 MVC 4 而不是 C#,但这里的帖子回答了我的问题:问号 (?) 在 C# 中的函数声明中是什么意思

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

希望这可以帮助遇到与我相同问题的人!

于 2013-10-10T17:56:20.317 回答