首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >删除MVC 5 EditorTemplate附加ID

删除MVC 5 EditorTemplate附加ID
EN

Stack Overflow用户
提问于 2014-05-30 06:38:40
回答 1查看 205关注 0票数 0

我使用这个编辑器模板来使用相同属性名的ViewBag/ViewData的Dropdownlist列表

代码语言:javascript
运行
复制
@model System.String
@*
    For Using this Editor Template
    - There should be a viewbag/viewdata (type SelectList) of same name as of calling Model's Property
*@
@{
    var modelMetadata = ViewData.ModelMetadata;
    // Get property name of the model
    var propertyname = modelMetadata.PropertyName;
}

@if (ViewData[propertyname] == null)
{
    @Html.DropDownList(propertyname , Enumerable.Empty<SelectListItem>(), "--Select--", new { @class = "form-control" })
}
else
{
    @Html.DropDownList(propertyname , null, "--Select--", new { @class = "form-control" })
}

现在使用它作为@Html.EditorFor(i=>i.Country,"CustomDropDown"),我也有一个ViewBag.Country作为国家的SelectList。

一切都正常,但是现在控件的命名变成了

代码语言:javascript
运行
复制
<select class="form-control" id="Country_Country" name="Country.Country">

如何从id和名称中删除附加的Country

附加信息:

我本可以只使用@Html.DropDownList("Country"),但它不允许我将css类添加到控件中。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-05-30 06:51:52

我想我找到了解决办法。

我将DropDownList更改为DropDownListFor,并作了一些更改

代码语言:javascript
运行
复制
@if (ViewData[propertyname] == null)
{
    @Html.DropDownListFor(m=>m, Enumerable.Empty<SelectListItem>(), "--Select--", new { @class = "form-control" })
}
else
{
    @Html.DropDownListFor(m => m, ViewData[propertyname] as SelectList, "--Select--", new { @class = "form-control" })
}

这个自动ViewData绑定有时有点混乱。>_<

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

https://stackoverflow.com/questions/23948549

复制
相关文章

相似问题

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