【问题标题】:how to custom Html.DropDownListFor suppet set name attributes in ASP.NET Core MVC如何自定义 Html.DropDownListFor 在 ASP.NET Core MVC 中设置名称属性
【发布时间】:2020-03-05 23:04:37
【问题描述】:
new { id = " dd" name = " myName" } 

不在name属性上覆盖,id编码的name属性怎么覆盖

@Html.DropDownListFor(x => x.Customer.CustomerBiaoshi, Html.GetEnumSelectList<TgNetJZ.CustomerBiaoshi>(), "test",new { @class = "form-control custom-select" ,@id="ddd",@name="myName"})

我这样写总是显示模型名称Customer.CustomerBiaoshi,我想显示我设置的名称属性“myName”

应该是我编辑页面只能使用dropdownlistfor绑定下拉框选择item,所以需要解决name属性Customer.CustomerBiaoshi override和Model对应CustomerBiaoshi

或如何在 mvc 核心支持集名称属性 like so 上自定义下拉列表 htmlstring

【问题讨论】:

    标签: asp.net asp.net-core-mvc


    【解决方案1】:

    您可以使用其他版本的下拉列表创建器

    @Html.DropDownList(string "name", IEnumerable<SelectListItem> selectList, 
                       object htmlAttributes) 
    

    在你的情况下:

    @Html.DropDownList("myName", Html.GetEnumSelectList<TgNetJZ.CustomerBiaoshi>(),
                        new { @class = "form-control custom-select", @id = "ddd" }).
    

    但是您的模型绑定到Customer.CustomerBiaoshi 将不起作用,因为模型绑定使用名称属性并且您将名称属性更改为myName

    【讨论】:

    • 我的编辑表单使用dropdownlistfor绑定选择但是我的Dto Field CustomerBiaoshi我需要设置name和dto对应来assign所以我只能用dropdownlistfor解决name属性覆盖
    猜你喜欢
    • 1970-01-01
    • 2017-12-14
    • 1970-01-01
    • 1970-01-01
    • 2011-01-15
    • 1970-01-01
    • 2019-11-12
    • 1970-01-01
    • 2011-04-04
    相关资源
    最近更新 更多