简体   繁体   English

如何使@Html.DropDownList() 允许为空

[英]How to make @Html.DropDownList() allow null

I've used Visual Studio to automatically generate Controllers and Views from a database.我使用 Visual Studio 从数据库自动生成控制器和视图。 In the database, there's a table dbo.Items, which has a FOREIGN KEY (CategoryID) REFERENCES Categories(Id)在数据库中,有一个表 dbo.Items,它有一个FOREIGN KEY (CategoryID) REFERENCES Categories(Id)

The generated View for Items/Create has this block, which forces users to select 01 Category from a drop-down list when adding a new Item, no null value allowed:为 Items/Create 生成的 View 有这个块,它强制用户在添加新 Item 时从下拉列表中选择 01 类别,不允许空值:

    <div class="form-group">
        @Html.LabelFor(model => model.CategoryID, "CategoryID", htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.DropDownList("CategoryID", null, htmlAttributes: new { @class = "form-control" })
            @Html.ValidationMessageFor(model => model.CategoryID, "", new { @class = "text-danger" })
        </div>
    </div>

How do I make the Null option available?如何使 Null 选项可用?

Html.DropDownList() is an html helper method which will generate the HTML markup for rendering a SELECT element. Html.DropDownList()是一个 html 辅助方法,它将生成用于呈现 SELECT 元素的 HTML 标记。 It does not do any "allow/not allow" thing by itself.它本身不做任何“允许/不允许”的事情。

MVC model validation framework does the model validation on the server side when you submit the form based on your view model properties and the data annotations defined on that.当您根据视图模型属性和在其上定义的数据注释提交表单时,MVC 模型验证框架会在服务器端进行模型验证。 The helper method also generates the needed data attributes which the jQuery validate plugin can use to do client side validation. helper 方法还生成所需的数据属性,jQuery 验证插件可以使用这些属性进行客户端验证。

To allow selecting no options in the SELECT element change the CategoryID property to nullable int.要允许在 SELECT 元素中不选择任何选项,请将CategoryID属性更改为可为空的 int。 If you have a view model, you may udpate there.如果你有一个视图模型,你可以在那里更新。

public class YourViewmodel
{
  public int? CategoryID { set;get;}
}

You need to update your db schema as well to save nullable value in the CategoryId column.您还需要更新数据库架构以在CategoryId列中保存可为空的值。 If you are using database first approach, you can make the db schema change (changing the column to nullable) and then regenerate the entity classes.如果您使用数据库优先方法,您可以更改数据库架构(将列更改为可为空),然后重新生成实体类。

I also suggest you using the DropDownListFor helper我还建议您使用DropDownListFor助手

@Html.DropDownListFor(x=>x.CategoryID,ViewBag.CountryCode as  List<SelectListItem>,
                                       "select one", new { @class = "form-control" })

Assuming ViewBag.CountryCode is a list of SelectListItem 's you set in your GET action method.假设ViewBag.CountryCode是您在 GET 操作方法中设置的SelectListItem列表。

You can use fallowing overloaded version to show "select option" initially...您可以使用休闲重载版本最初显示“选择选项”...

@Html.DropDownList("CategoryID", null, "select option", new { @class = "form-control" })

and whenever you are adding options to the CategoryID dropdown, you need to add "select option" as first one and rest after that...并且每当您向 CategoryID 下拉列表添加选项时,您都需要将“选择选项”添加为第一个,然后再添加......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM