【问题标题】:How do I add attributes to HTML.BeginForm with ASP.MVC 4?如何使用 ASP.MVC 4 向 HTML.BeginForm 添加属性?
【发布时间】:2013-01-01 08:16:05
【问题描述】:

我正在使用以下代码:

@using (Html.BeginForm(null, null, 
    new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Post, 
    new { data-href = "/User/Account/Login"}))

谁能告诉我它有什么问题。我收到一条指向 data-href 的错误消息并说:

匿名类型成员声明器无效。匿名类型成员必须使用成员赋值、简单名称或成员访问来声明

【问题讨论】:

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


    【解决方案1】:

    -(破折号)不是有效的 C# 标识符字符。使用_(下划线),它将被转换为-,因此您将在生成的HTML中得到正确的data-href

    @using (Html.BeginForm(null, null, 
        new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Post, 
        new { data_href = "/User/Account/Login"}))
    

    【讨论】:

      猜你喜欢
      • 2011-02-20
      • 2016-08-05
      • 1970-01-01
      • 2010-09-18
      • 2013-07-22
      • 1970-01-01
      • 2011-11-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多