Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/261.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 带有Html属性的Html.net mvc4_C#_Razor_Asp.net Mvc 4 - Fatal编程技术网

C# 带有Html属性的Html.net mvc4

C# 带有Html属性的Html.net mvc4,c#,razor,asp.net-mvc-4,C#,Razor,Asp.net Mvc 4,我使用Html.BeginForm进行了编辑操作。如何添加HTML属性 我只知道一种方法: @using (Html.BeginForm("Edit", "Clients", FormMethod.Post, new { @class="example"})) { } 但如果使用此方法,则无法传递当前ID 是否可以在不修改操作URL的情况下向表单添加HTML属性?您需要的覆盖是: @using( Html.BeginForm("Edit", "Clients", new { Id=Model

我使用
Html.BeginForm
进行了编辑操作。如何添加HTML属性

我只知道一种方法:

@using (Html.BeginForm("Edit", "Clients", FormMethod.Post, new { @class="example"})) {

}
但如果使用此方法,则无法传递当前ID


是否可以在不修改操作URL的情况下向表单添加HTML属性?

您需要的覆盖是:

@using( Html.BeginForm("Edit", "Clients", new { Id=Model.Id},
                       FormMethod.Post, new { @class = "example" } ) )
{
}
  • 像“id”这样的路由值作为第三个参数传递
  • 像“class”这样的HTML属性作为第五个参数传递

请参阅文档。

通过ControllerA的ActionLink调用

@using (Html.BeginForm("Create",
    "StudentPChoice",
    new { StudentPChoiceId = Model.StudentPChoiceId },
    FormMethod.Post))
{

}


操作和控制器参数也可以为null以使用默认操作:

Html.BeginForm( null, null, FormMethod.Post, new { id=”formname”, @class="formclass" })

如果这对某些人有帮助的话,这对我很有用:

@using (Html.BeginForm("RefreshData", "Home", FormMethod.Post, 
        new { Id = "timerangeId", @name = "timerange" }))
    {
        // form elements and input
    }
在Javascript中:

document.getElementById("timerangeId").submit();

您使用的是强类型viewYes吗?当前id是什么?为什么不能传递它?您希望使用什么HTTP方法?您可以很容易地指定
FormMethod.Get
或其他任何内容,而不是
FormMethod.Post
,如果您愿意的话。为什么不放一个“,”并添加一个id属性呢。您可以像那样附加多个Atribute
@使用(Html.BeginForm(“Edit”,“Clients”,FormMethod.Post,new{@class=“example”,id=Model.id}))
这是您正在寻找的还是确切的问题?当然,您也可以在第5个参数对象中传递
@id=“blah”
,这将具有设置呈现表单标记的HTML id的完全不同的效果。如果要发布到当前控制器的相同操作(post方法),前两个参数可以为null,例如:
@using(HTML.BeginForm(null,null,new{id=Model.id},FormMethod.post,new{@class=“example”}))
确实如此。
document.getElementById("timerangeId").submit();