【问题标题】:cshtml c# not listening to assigned id for some reason?cshtml c#由于某种原因不听分配的ID?
【发布时间】:2012-08-23 13:40:38
【问题描述】:

所以我有一个我正在处理的基本 CRUD,我正在尝试让 Jquery 验证对其进行处理。除了我需要给我的表单一个 id 外,我几乎都设置好了。我在 Visual Studio 中使用 cshtml 并尝试使用以下代码将 id 分配给表单:

@using (Html.BeginForm( new { id = "daftform" })) {
@Html.ValidationSummary(true)
<fieldset>
<legend>News</legend> 

然而生成的 html 看起来像这样:

<form action="/News/Create/daftform" method="post">    <fieldset>
    <legend>News</legend>

我很确定这是为元素分配 id 的方法,因为我使用此方法以类似的方式分配类。谁能告诉我哪里出错了?

我只是希望它分配 'daftform' 作为 id 而不是作为操作。

对不起,如果它是一个简单的答案,对 c# 来说相当新。

【问题讨论】:

    标签: c# jquery asp.net-mvc-3 visual-studio razor


    【解决方案1】:

    使用this重载

    public static MvcForm BeginForm(
        this HtmlHelper htmlHelper,
        string actionName,
        string controllerName,
        FormMethod method,
        Object htmlAttributes
    )
    

    所以你的代码可以改成

    @using (Html.BeginForm("Create","News",FormMethod.Post, new { id = "daftform" }))
    {
      //form elements
    }
    

    这将创建一个form 标记,其中Id 属性设置为"daftform"

    【讨论】:

      【解决方案2】:

      @using (Html.BeginForm(,"actionname","controllername",formmethod.post/get, new {id = "yourId"})

      要走的路

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-08-23
        • 2012-01-28
        • 1970-01-01
        • 1970-01-01
        • 2021-08-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多