一个for循环中设置了HiddenFor硬codeD值 [英] Set a hardcoded value in a HiddenFor within a for-loop

查看:110
本文介绍了一个for循环中设置了HiddenFor硬codeD值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当只有一个人选择我还需要分配硬codeD值TRUE到人民[我] .IsSelected属性,以满足我的验证。

我怎么能这样做?

  @if(hasMoreThanOnePerson)
{
    &所述; TD>
        @ Html.CheckBoxFor(M = GT; m.People [I] .IsSelected)
    < / TD>
}
其他
{
    @ Html.HiddenFor(M = GT; m.People [I] .IsSelected,真)//设置为TRUE总是隐藏字段内与索引循环
}


解决方案

使用 htmlAttributes 设置财产隐藏字段使用此重载或< A HREF =htt​​ps://msdn.microsoft.com/en-us/library/ee703622%28v=vs.118%29.aspx相对=nofollow>此重载:

  @ Html.HiddenFor(M = GT; m.People [I] .IsSelected,新{值=真})

它将输出HTML这样的:

 &LT;输入ID =YourModel.People [0] .IsSelected
       NAME =YourModel.People [0] .IsSelected
       类型=隐藏
       值=真&GT;

When there is only one person to select I still need to assign the hardcoded value 'TRUE' to the People[i].IsSelected property to satisfy my validation.

How can I do that?

@if (hasMoreThanOnePerson)
{
    <td>
        @Html.CheckBoxFor(m => m.People[i].IsSelected)
    </td>
}
else
{
    @Html.HiddenFor(m => m.People[i].IsSelected, true) // Set TRUE always to hidden field within for loop with indexer
}

解决方案

Use htmlAttributes to set value property of Hidden Field using this overload or this overload:

@Html.HiddenFor(m => m.People[i].IsSelected, new {Value="true"})

It will output html like:

<input id="YourModel.People[0].IsSelected" 
       name="YourModel.People[0].IsSelected" 
       type="hidden" 
       value="true">

这篇关于一个for循环中设置了HiddenFor硬codeD值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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