【问题标题】:How can i set the html id attribute in razor如何在 razor 中设置 html id 属性
【发布时间】:2014-11-22 07:34:31
【问题描述】:

我想在我的 MVC 5 表单中配置 datepicker。我已经包含了所有的参考库,如 Jquery、datepicker 等。

以下是 MVC razor 视图中的一行,它是由 Visual Studio 在 MVC 5 脚手架的结果中创建的。

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

这方面的任何帮助

【问题讨论】:

    标签: asp.net-mvc razor asp.net-mvc-5


    【解决方案1】:

    只需将 id 属性添加到 html 属性。这将覆盖 editorfor-helper-methode 生成的默认 ID。

    看看这个帖子: How can I set id using Html.EditorFor with MVC3

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

    【讨论】:

      【解决方案2】:

      我刚刚使用 MVC5 .net 4.5.1 测试了这个解决方案。我试图手动设置表单 ID,因为我将在同一页面中有多个表单,我需要使用 JS 单独控制:

      @using (Ajax.BeginForm("Edit", "Products", null, new AjaxOptions { HttpMethod = "POST", UpdateTargetId = "divEmp"+Model.Id }, htmlAttributes : new { id = "form_" + Model.Id } ))
      {
      
      }
      

      最后一个参数Enfasis:htmlAttributes : new { id = "form_" + Model.Id }

      @Html.BeginForm() 和其他助手也是如此。

      htmlAttributes: 部分并不是必需的。

      【讨论】:

        猜你喜欢
        • 2012-03-29
        • 2015-05-10
        • 2010-11-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-26
        • 2021-12-17
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多