gpt4 book ai didi

asp.net-mvc-4 - MVC Razor 强类型 View 上表行的回发复选框

转载 作者:行者123 更新时间:2023-12-01 01:08:06 25 4
gpt4 key购买 nike

我有强类型 View 显示来自

ViewModel 
public class GoldSetnUsers
{
bool Public { get; set; }
public List<GSUsers> gsUsers { get; set; }


public GoldSetnUsers()
{
UsersContext _dbm = new UsersContext();
this.gsUsers = _dbm.UserProfiles.Select(n => new GSUsers { UserName = n.UserName, isEditor = false, isReviewer = false }).ToList();
}

public class GSUsers
{
public string UserName { get; set; }
public bool isEditor { get; set; }
public bool isReviewer { get; set; }
}

}

Controller Httpget 方法显示此 View

enter image description here

问题是,回发模型将所有行复选框返回为 false。表外的复选框 Public 会返回正确的回发值。

Controller 回发代码
[HttpPost]
public ActionResult Create(GoldSetnUsers newGS)
{
if (ModelState.IsValid)
{ // newGS gets me value

}

}

看法
@model mvc2db.Models.GoldSetnUsers
@using BootstrapSupport;
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)


@Html.BeginControlGroupFor(model=>model.Public)
@Html.LabelFor(model => model.Public,new {@class="control-label"})
<div class="controls">
@Html.EditorFor(model => model.Public,new {@class="input-xlarge"})
@Html.ValidationMessageFor(model => model.Public,null,new{@class="help-inline"})
</div>
<div class="controls">
<table class="table">
<thead>
<tr>

<th>Name</th>
<th>Reviewer</th>
<th>Editor</th>

</thead>
<tbody>

@foreach (var item in Model.gsUsers) {
<tr>

<td>
@Html.DisplayFor(modelItem => item.UserName)
</td>

<td>
@Html.EditorFor(modelItem => item.isEditor)
</td>

<td>
@Html.EditorFor(modelItem => item.isReviewer)
</td>



</tr>
}
</tbody>
</table></div>
@Html.EndControlGroup()

<div class="form-actions">
<button type="submit" class="btn btn-primary">Save changes</button>
<button class="btn">Cancel</button>
</div>
</fieldset>
}

最佳答案

我猜因为您使用 foreach 循环生成复选框,所以所有复选框都将具有相同的 ID。因此,对于哪些被检查,哪些不被检查会有歧义。
您可以尝试将用户名作为复选框的 ID。

关于asp.net-mvc-4 - MVC Razor 强类型 View 上表行的回发复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17181335/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com