使用asp.net mvc查看要填充的袋子复选框列表



下面是我的模型类

public class InformationMissing : AbstractDBObject
{
public int Id { get; set; }
public string PatientDemographics { get; set; }
public string InsuranceDetails { get; set; }
public string PhysicianDetails { get; set; }
public string Note { get; set; }
public int personId {get;set;}
public string status { get; set; }
public List<CheckboxList_model> RetrievalItem { get; set; }
}
public class CheckboxList_model
{
public string Text { get; set; }
public int Value { get; set; }
public Boolean Selected { get; set; }
}

下面是我的控制器方法

public ActionResult Process()
{
List<CheckboxList_model> chkRetrieval = new List<CheckboxList_model>(){
new CheckboxList_model{ Text="One", Value = 1 ,Selected=false},
new CheckboxList_model{ Text="Two", Value = 2 ,Selected=false},
new CheckboxList_model{ Text="Three", Value = 3 ,Selected=false }
};
ViewBag.Retrieval = chkRetrieval;
return View();
}

下面的代码我已经尝试过,但复选框列表没有填写

@foreach (var Retrieval in ViewBag.Retrieval)
{
@Html.CheckBoxFor(Retrieval.Selected, new { @class = "flat" })
@Html.DisplayFor(Retrieval.Text)
@Html.HiddenFor(Retrieval.Value)
}

从下面可以看到文本值,但上面的复选框列表没有填写

@foreach (var Retrieval in ViewBag.Retrieval)
{
<p>
@Retrieval.Text </p>
}

您需要在视图中的实际模型列表中取消选中ViewBag.Reviceive,如下所示。

在您的视野中展开拳击

@{
var checkboxList = (List<CheckboxList_model>)ViewBag.Retrieval
}

循环复选框列表

@foreach (var Retrieval in checkboxList)
{
<p>@Retrieval.Text </p>
}

以下代码适用于我使用Viewbag

@foreach (var names in ViewBag.Retrieval)
{
var checkBoxId = "chk" + names.Value;
var tdId = "td" + names.Value;
<table width="100%">
<tr>
<td width="20px">
<input type="checkbox" id="@checkBoxId" class="chkclass" value="@names.Value" />
</td>
<td id="@tdId" width="100px">
@names.Text
</td>
</tr>
</table>
}

最新更新