Asp.net mvc 4 在视图模型中为“自”列表创建复选框
我的视图模型类是Asp.net mvc 4 在视图模型中为“自”列表创建复选框,asp.net-mvc-4,asp.net-mvc-viewmodel,checkboxfor,Asp.net Mvc 4,Asp.net Mvc Viewmodel,Checkboxfor,我的视图模型类是 public class StudentQuestions { public int StudentId{ get; set; } public int FormId { get; set; } public virtual ICollection<Questions> Question { get; set; } } 公共课堂学生问题 { 公共int StudentId{get;set;} 公共i
public class StudentQuestions
{
public int StudentId{ get; set; }
public int FormId { get; set; }
public virtual ICollection<Questions> Question { get; set; }
}
公共课堂学生问题
{
公共int StudentId{get;set;}
公共int FormId{get;set;}
公共虚拟ICollection问题{get;set;}
}
问题课是
public partial class Questions
{
public int questionID { get; set; }
public string field_name { get; set; }
public string question { get; set; }
public int qutyp_refID {get,set}
public string description { get; set; }
public int ord { get; set; }
public bool IsEnabled { get; set;}
public virtual ICollection<Answers> Answers { get; set; }
公共部分类问题
{
public int questionID{get;set;}
公共字符串字段_name{get;set;}
公共字符串问题{get;set;}
public int qutyp_refID{get,set}
公共字符串说明{get;set;}
公共整数ord{get;set;}
公共布尔值已启用{get;set;}
公共虚拟ICollection答案{get;set;}
}
在我看来
@model Test.ViewModels.StudentQuestions
<table>
<tr><td>@Model.FormId</td><td>@Model.StudentId</td></tr>
@foreach(var q in Model.Question)
{
<tr>
<td> @Html.CheckBoxForFor(i=> i.Question.question)</td>
</tr>
}
</table>
@model Test.ViewModels.StudentQuestions
@模型。FormId@Model.StudentId
@foreach(模型中的var q.问题)
{
@CheckBoxForFor(i=>i.Question.Question)
}
我无法访问I.Question.Question,但我可以访问复选框、文本框,如下面所示,我想将文本框更改为TextBoxFor,将复选框更改为CheckBoxFor,将文本框更改为TextBoxFor
@foreach(var q in Model.Question)
{
<tr>
@if (@q.qutyp_refID == 4)
{
<td>@Html.CheckBox(q.questionID.ToString())
</td>
}
else if (@q.qutyp_refID <= 2)
{
<td>@Html.TextBox("txtDateQuestions", DateTime.Today.ToString("dd/MM/yyyy"), new { style = "width: 120px" }) </td>
}
else
{
<td>@Html.TextBox(q.questionID.ToString(), null)</td>
}
</tr>
}
@foreach(Model.Question中的var q)
{
@如果(@q.qutyp_refID==4)
{
@复选框(q.questionID.ToString())
}
否则,如果(@q.qutyp_refID这样尝试
@Html.CheckBoxFor(i => m.questionID, new { id = @m.questionID, @checked = "checked", Name = "CheckBox" })<span>m.description</span>
查看
@foreach (var item in Model)
{
@Html.CheckBoxFor(m => item.IsChecked, new { value = item.EmployeeId, id = "chk_" + @item.EmployeeId, @checked = "checked", Name = "CheckBox" })
}
谢谢你的回答,但它给出了错误(i=>m.questionID…)它表示不能将int转换为bool@Khan因为您的模型中没有布尔属性,这只是一个示例。请给您一个如何绑定的示例。@KhanI添加了一个布尔变量IsEnabled,请您现在查看代码……谢谢Hanks Jaiman……现在工作得很好……请您告知我提交的日期,好吗(m=>item.questionID,new{value=DateTime.Today.ToString(“dd/MM/yyyy”),id=“q”+@item.questionID,Name=“Datet”,style=“width:120px”,@class=“datepick”})
@foreach (var item in Model)
{
@Html.CheckBoxFor(m => item.IsChecked, new { value = item.EmployeeId, id = "chk_" + @item.EmployeeId, @checked = "checked", Name = "CheckBox" })
}