c# - htmlAttributes MVC 5.1 编辑器

标签 c# asp.net-mvc model-binding

我已将以下行添加到我的 View 中

@Html.EditorFor(model => model, new { htmlAttributes = new { @class = "form-control" }, })

如何在相应的 EditorTemplate Razor View 中访问 htmlAttributes?

另外,如何向 EditorTemplate View 中现有的传入 htmlAttributes 添加一些值?

最佳答案

您可以从 ViewData 中获取它们:

@{
    var htmlAttributes = HtmlHelper.AnonymousObjectToHtmlAttributes(ViewData["htmlAttributes"]);
    htmlAttributes.Add("key", "value"); //add new value
}

关于c# - htmlAttributes MVC 5.1 编辑器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22074432/

相关文章:

css - 401 未授权错误访问 CSS 文件 MVC

asp.net-mvc - 模型绑定(bind)后模型嵌套类型为空

c# - 在添加SQL参数时使用null

c# - 找不到 “Error” View ,仅在生产环境中发生,未在test/dev中发生

c# - 从 1 个项目列表创建 2 列项目列表

c# - 在一次提交中创建多条记录

asp.net-mvc - TimeSpan 的 MVC 编辑器模板

javascript - 如何将 Knockout js 模型绑定(bind)到向导式 UI

c# - StreamReader ReadLineAsync 忽略回车并挂起

c# - 为什么线程 IsAlive 即使在线程内执行最后一个命令后仍返回 false