2014-07-25 36 views
1

我的視圖模型類是從列表中的視圖模型創建CheckboxFor

public class StudentQuestions 
    { 
     public int StudentId{ get; set; } 
     public int FormId { get; set; } 
     public virtual ICollection<Questions> Question { 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; } 

} 在我看來

@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> 

我不能訪問我。 Question.question但我可以在CheckBox,TextBox中訪問如下,我想將Textbox改爲TextBoxFor和C. heckBox到CheckBoxFor和TextBox到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> 
} 

由於提前.........

回答

2

嘗試這樣,

@Html.CheckBoxFor(i => m.questionID, new { id = @m.questionID, @checked = "checked", Name = "CheckBox" })<span>m.description</span> 

模型

public class AssignProject 
{ 
     public Guid Id { get; set; } 
     public string EmployeesName { get; set; } 
     public Guid? EmployeeId { get; set; } 
     public Guid? ProjectId { get; set; } 
     public string AssignEmployeeId { get; set; } 
     public bool IsChecked { get; set; } 
} 

查看

 @foreach (var item in Model) 
    { 
    @Html.CheckBoxFor(m => item.IsChecked, new { value = item.EmployeeId, id = "chk_" + @item.EmployeeId, @checked = "checked", Name = "CheckBox" }) 

    } 
+0

感謝您的回答,但它給出了差(i => m.questionID ...),它說不能轉換INT爲bool – Khan

+0

@Khan怎麼一回事,因爲存在的布爾值屬性你的模型,這僅僅是一個例子。 – Jaimin

+0

給你一個例子如何綁定。 @Khan – Jaimin

相關問題