asp.net-mvc 如何在asp.net mvc中使用jquery设置@Html.EditorFor的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17335054/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to set value of @Html.EditorFor using jquery in in asp.net mvc
提问by Hammad Shahid
I want to set the value of my input text using jquery. I have tried by doing this :
我想使用 jquery 设置输入文本的值。我试过这样做:
<div class="editor-label">
@Html.LabelFor(model => model.ID )
</div>
<div class="editor-field">
@Html.EditorFor(model => model.ID)
@Html.ValidationMessageFor(model => model.ID)
</div>
and jquery syntax :
和 jquery 语法:
$("#ID").val(data.ID);
But it is not working. I have tried the same jquery code with this :
但它不起作用。我已经尝试了相同的 jquery 代码:
<input type="text" id="ID" />
This approach is working , but I don't know why @Html.EditorFor is not working with jquery .Thanks in advance
这种方法有效,但我不知道为什么 @Html.EditorFor 不能与 jquery 一起使用。提前致谢
采纳答案by JonVD
You're better off using the TextBoxFor method as it allows you to change the id and class of the element directly whereas EditorFor doesn't unless you create a custom edit state for it (using EditorTemplate).
您最好使用 TextBoxFor 方法,因为它允许您直接更改元素的 id 和类,而 EditorFor 则不允许,除非您为其创建自定义编辑状态(使用 EditorTemplate)。
@Html.TextBoxFor(model => model.ID, new { id = "ID" })
I believe @Html.EditorFor(model => model.ID)will set the name attribute as that is what mvc depends upon to do its model binding so if the above is not to your liking you can perhaps use the name as a selector.
我相信@Html.EditorFor(model => model.ID)将设置 name 属性,因为这是 mvc 进行模型绑定所依赖的,因此如果上述内容不符合您的喜好,您可以将名称用作选择器。
回答by d2z2
You can add the @id, @class, etc on EditorForas follows:
您可以添加的@id,@class等上EditorFor如下:
@Html.EditorFor(model => model.ID, new {htmlAttributes = new {@id="someid"}})
回答by Jayvee Grapa
this works for me
这对我有用
<div class="editor-field">
@Html.EditorFor(model => model.ID)
@Html.ValidationMessageFor(model => model.ID)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.SetID)
@Html.ValidationMessageFor(model => model.SetID)
</div>
and javascript is
和 javascript 是
var d = $("#ID ").val();
$("#SetID").val(d);
or
或者
$("#SetID").val($("#ID ").val());
回答by StringBuilder
You should use
你应该使用
@Html.EditorFor(model => model.ID, new {@id="someid"})
Then
然后
$("#someid").val (someval)

