【问题标题】:Mvc add id and css class in MVC Html.BeginForm()MVC 在 MVC Html.BeginForm() 中添加 id 和 css 类
【发布时间】:2014-04-14 14:45:11
【问题描述】:

我是 MVC 新手。

如何在 MVC Html.BeginForm() 中添加 id 和 css 类。

这是 html 表单。

<form id="frmId" class="frmStyle">
    ...
</form>

我试过了,但出错了。

@Html.BeginForm("actionName", "controllerName", new {id="frmId", class = "frmStyle"})

【问题讨论】:

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


    【解决方案1】:

    我们添加为 htmlAttributes 的一部分。

    @Html.BeginForm("actionName", "controllerName", FormMethod.Post,
     new {id="frmId", @class = "frmStyle"})
    

    【讨论】:

    • 为什么id前面需要加一个@
    • 属性名称前的@ 字符将其转义,以免与C# 关键字发生冲突。 class 是一个关键字。如果你不逃避它们,你会得到一个编译器错误。不完全确定为什么 id 在这个答案中被转义; AFAIK 这不是关键字。
    • @ 只需要 @class
    猜你喜欢
    • 2014-06-18
    • 2011-02-20
    • 2018-04-01
    • 2023-03-18
    • 1970-01-01
    • 2016-08-05
    • 1970-01-01
    • 1970-01-01
    • 2011-06-14
    相关资源
    最近更新 更多