如何以编程方式设置@ Html.EditFor或@ Html.TextBoxFor的Id,Name和Class属性 - MVC 4 [英] How to set programatically the Id, Name and Class atribute of a @Html.EditFor or @Html.TextBoxFor - MVC 4

查看:743
本文介绍了如何以编程方式设置@ Html.EditFor或@ Html.TextBoxFor的Id,Name和Class属性 - MVC 4的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图为用于呈现用户输入行列表的部分视图生成唯一的标签和输入文本框。



独特我的意思是,每个输入文本框应该有其唯一的htmlid和name,以便在提交时可以识别每个输入



视图我有

  @model UserDataModel 

@ {
var inpName = benefName+ @ Model.Row;
var inpAge =benefAge+ @ Model.Row;
}
@ Html.LabelFor(x => x.Name,new {@ class =labelhalf})
@ Html.EditorFor(x => x.Name,new {id = @inpName,htmlAttributes = new {@class =form-control animated}})

当视图正在渲染时,这就是我所看到的

 < label class =labelhalffor =Name > Nombre(Opcional)< / label> 
< input class =text-box single-lineid =Namename =Nametype =textvalue =>

正如您所看到的,文本输入的name和id属性是Name和名称,并没有使用@inpName变量的值(例如benefName1)

另外,我正在尝试将一些CSS类分配给相同的输入使用htmlAttributes



我以前曾试过这种方法

 < label form =FormStep_01for = @ inpName class =labelhalf> Nombre(Opcional)< / label> 
< input form =FormStep_01id = @ inpName class =form-control animatedpattern =^ [_A-z0-9] {1,} $type =textplaceholder =需要= >

...但是使用这种方法输入字段的内容不会被忽略,那就是原因我正在尝试使用@ Html.EditorFor



更新
我现在使用TextBoxFor,它将id和class罚款,但不是提交中使用的name

  @ Html.LabelFor(x => ; x.Name,new {@class =labelhalf})
@ Html.TextBoxFor(x => x.Name,new {@id = @inpName,name = @inpName,@class =form - 控制动画))

请让我知道如何在MVC4中实现此功能

解决方案

问题1(使用 EditorFor()

b $ b

您无法在MVC-4中使用 EditorFor()添加html属性。这个特性直到MVC-5.1才被引入,然后正确的用法是

$ $ p $ $ $ c $ @ Html.EditorFor(m => m .SomeProperty,new {htmlAttributes = new {someAttribute =someValue},})

使用 TextBoxFor()



您不能更改名称的值属性使用 new {name =someValue} 。 MVC团队建立一个安全防护来防止这种情况,因为使用 HtmlHelper 方法生成表单控件的全部目的是绑定到您的模型属性,这样做会导致绑定失败。虽然有一个解决方法,但如果你确实发现了它,不要这样做!作为一个简要说明 - 私有静态MvcHtmlString InputHelper()方法的以下行#src / System.Web.Mvc / Html / InputExtensions.csrel =nofollow noreferrer>源代码

  tagBuilder.MergeAttribute(name,fullName,true); 

可以防止您重写它。

问题3(手册html)



您不给输入一个 name 属性。表单根据成功控件的名称属性发布名称/值对,所以如果有没有 name 属性,没有东西会发送到控制器。



注意:如果您手动生成html,那么不需要添加 id 属性,除非您在javascript或css中引用该元素。



其不清楚为什么你试图为与你的模型不相关的东西创建一个输入,但是如果你试图动态生成项目以便将项目添加到模型中的集合属性中,请参阅答案这里这里的一些选项,这将允许您绑定到您的模型。


I am trying to generate a unique label and and input text box for a partial view that is being used to render a list of user input rows.

By unique I mean that each input text box should have its unique html "id" and "name" so that when is submitted each input can be identified

In the View I have

@model UserDataModel

@{
    var inpName = "benefName" + @Model.Row;
    var inpAge = "benefAge" + @Model.Row;
}
@Html.LabelFor(x => x.Name, new { @class="labelhalf"})
@Html.EditorFor(x => x.Name, new { id = @inpName, htmlAttributes = new { @class = "form-control animated" } })

When the view is being render this is what I am seeing

<label class="labelhalf" for="Name">Nombre (Opcional)</label>
<input class="text-box single-line" id="Name" name="Name" type="text" value="">

As you can see the "name" and "id" attributes of the text input is "Name" and "Name" and is not using the value of the @inpName variable ("benefName1" for example)

Also I am trying to assign some CSS classes to that same input using "htmlAttributes"

I had previously tried this with this approach

<label form="FormStep_01" for=@inpName class="labelhalf">Nombre (Opcional)</label>
<input form="FormStep_01" id=@inpName class="form-control animated" pattern="^[_A-z0-9]{1,}$" type="text" placeholder="" required="">

...but the content of the input fields with this approach are not being submited and that is the reason I am trying to use the @Html.EditorFor

UPDATE I am now using the TextBoxFor which takes the "id" and the "class" fine but not the "name" which is used in the submit

@Html.LabelFor(x => x.Name, new { @class = "labelhalf" })
@Html.TextBoxFor(x => x.Name, new { @id = @inpName, name = @inpName, @class = "form-control animated" })

Please let me know how to achieve this in MVC4

解决方案

Issue 1 (Using EditorFor())

You cannot add html attributes using EditorFor() in MVC-4. This feature was not introduced until MVC-5.1, and then the correct usage is

@Html.EditorFor(m => m.SomeProperty, new { htmlAttributes = new { someAttribute = "someValue" }, })

Issue 2 (Using TextBoxFor())

You cannot change the value of the name attribute using new { name = "someValue" }. The MVC team built in a safe guard to prevent this because the whole purpose of using the HtmlHelper methods to generate form controls is to bind to your model properties, and doing this would cause binding to fail. While there is a workaround, if you do discover it, don't do it! As a side note - the following line of the private static MvcHtmlString InputHelper() method in the source code

tagBuilder.MergeAttribute("name", fullName, true);

is what prevents you overriding it.

Issue 3 (Manual html)

You not giving the inputs a name attribute. A form posts back a name/value pair based on the name and value attributes of successful controls, so if there is no name attribute, nothing will be sent to the controller.

Side note: If your manually generating html, there is no real need to add an id attribute unless your referring to that element in javascript or css.

Its unclear why your trying to create a input for something that does not appear to relate to your model, but if your trying to dynamically generate items for adding items to a collection property in your model, refer the answers here and here for some options which will allow you to bind to your model.

这篇关于如何以编程方式设置@ Html.EditFor或@ Html.TextBoxFor的Id,Name和Class属性 - MVC 4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆