首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 ><Bind(Exclude:="CurrentPrice")>在MVC4中不起作用

<Bind(Exclude:="CurrentPrice")>在MVC4中不起作用
EN

Stack Overflow用户
提问于 2013-06-15 03:14:14
回答 1查看 432关注 0票数 0

我正在用ASP.NET MVC4写这篇文章,并且一直在尝试在表单中有一个字段,其中一个字段被排除在写入之外。我在AuctionsController.vb文件中有以下模型,它控制表单:

代码语言:javascript
复制
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如下所示:

代码语言:javascript
复制
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

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

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

EN

回答 1

Stack Overflow用户

发布于 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?,这样当用户在网页上填写表单时,变量就不是必需的了。

希望这对和我有同样问题的人有所帮助!

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17115723

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档