javascript 从 @Html.DisplayFor 和 @Html.HiddenFor 获取值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27417156/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 07:27:56  来源:igfitidea点击:

Getting a value from @Html.DisplayFor and @Html.HiddenFor

javascriptjqueryasp.net-mvcasp.net-mvc-4

提问by StackTrace

The HTML

HTML

    int i = 1;
    foreach (var item in Model.MyDataset)
    {
    <td class="tdBorder">
    @Html.DisplayFor(x => item.ID, new { id = "VisibleID" + @i })
    @Html.HiddenFor(x => item.ID, new { id = "HiddenID" + @i })
    </td>
    i += 1;
    }

The jQuery

jQuery

 for (i = 1; i <= rowCount; i++) {
            var myID_Visible = $.trim($("#VisibleID" + i).val());
            var myID_Hidden = $.trim($("#HiddenID" + i).val());
        }

I'm trying to learn some MVC and jQuery.

我正在尝试学习一些 MVC 和 jQuery。

Would some one explain to me why calling

有人会向我解释为什么打电话

var myID_Visible = $.trim($("#VisibleID" + i).val());returns an empty string but

var myID_Visible = $.trim($("#VisibleID" + i).val());返回一个空字符串但是

var myID_Hidden = $.trim($("#HiddenID" + i).val());returns the value of my item.ID?

var myID_Hidden = $.trim($("#HiddenID" + i).val());返回我的 item.ID 的值?

The only difference is that the first jQuery line refers to a @Html.DisplayFor (returns empty string) while the second jQuery line refers to a @Html.HiddenFor (returns actual value).

唯一的区别是第一行 jQuery 引用 @Html.DisplayFor(返回空字符串),而第二行 jQuery 引用 @Html.HiddenFor(返回实际值)。

Why can't i get a value from the @Html.DisplayFor?

为什么我不能从@Html.DisplayFor 获取值?

回答by

Because @Html.DisplayFor()does not render a control and you cannot use .val(). Instead use

因为@Html.DisplayFor()不呈现控件并且您不能使用.val(). 而是使用

myID_Visible = $.trim($("#VisibleID" + i).text())

although this will depend on the html that @Html.DisplayFor()is rendering (are you using a display template?). You need to check the html generated.

虽然这将取决于@Html.DisplayFor()呈现的 html (您是否使用显示模板?)。您需要检查生成的html。

By default DisplayForwill just render the text value of the property. You would need to do something like

默认情况下DisplayFor只会呈现属性的文本值。你需要做类似的事情

int i = 1;
@foreach (var item in Model.MyDataset)
{
  <td class="tdBorder">
    <span id="@i">
      @Html.DisplayFor(x => item.ID)
    </span>
    @Html.HiddenFor(x => item.ID, new { id = "HiddenID" + @i })
  </td>
  i += 1;
}

and in the script

并在脚本中

myID_Visible = $.trim($('#' + i).text());

回答by Shadi

Some input has .html() rather than .val() try:

一些输入有 .html() 而不是 .val() 尝试:

var myID_Visible = $.trim($("#VisibleID" + i).html());

EDIT

编辑

Another thing, remove the @ before the i, you are already inside a C# code

另一件事,去掉i之前的@,你已经在C#代码里面了

@Html.DisplayFor(x => item.ID, new { id = "VisibleID" + i })

回答by Sambalado

The reason is explained in thisand that. For your case however, I prefer to make it like this:

这个那个解释了原因。但是,对于您的情况,我更喜欢这样做:

HTML

HTML

int i = 1;
foreach (var item in Model.MyDataset)
{
  <td class="tdBorder">
    <p id="VisibleID@(i)">@Html.DisplayFor(x => item.ID)</p>
    <p id="HiddenID@(i)">@Html.HiddenFor(x => item.ID)</p>
  </td>
i += 1;
}

So in the script we can call it:

所以在脚本中我们可以调用它:

for (i = 1; i <= rowCount; i++) {
    var myID_Visible = $("#VisibleID" + i).text();
    var myID_Hidden = $("#HiddenID" + i).text();
}

Hopefully this can help you, cheers!

希望可以帮到你,加油!

回答by asdf_enel_hak

@Html.DisplayFor(x => item.ID, new { id = "VisibleID" + @i })

rendred html looks like:

渲染的 html 看起来像:

<span id="VisibleID1">item id value here</span>

whereas

然而

@Html.HiddenFor(x => item.ID, new { id = "HiddenID" + @i })

rendered html looks like

呈现的 html 看起来像

<input type="hidden" id="HiddenID1" value="item id value here">

So in order to have display for value you should you use, $("#VisibleID1).html()as noted by @Shadi

所以为了显示价值,你应该使用,$("#VisibleID1).html()正如@Shadi 所指出的