1

My View Model Class is

 public class StudentQuestions
    {
        public int StudentId{ get; set; }
        public int FormId { get; set; }
        public virtual ICollection<Questions> Question { get; set; }
    }

and question class is

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; }

} in my view

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

I cant access i.Question.question but I can access in CheckBox, TextBox like following and I want to change Textbox to TextBoxFor and CheckBox to CheckBoxFor and TextBox to 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>
}

Thanks in Advance.........

1 Answer 1

3

Try like this,

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

Example

Model

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; }
 }

View

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

     }
Sign up to request clarification or add additional context in comments.

9 Comments

Thanks for the answer but it gives error on (i => m.questionID... ) it says cannot convert int to bool
@Khan Beacuse there is no boolean property in your model and this is just example.
Give you one example how to bind. @Khan
I added a boolean variable IsEnabled could you please review the code now...Thanks
Thanks Jaiman ...Works perfect now ...Could you please advise me for date filed as well i.e @Html.TextBoxFor(m=> item.questionID, new { value = DateTime.Today.ToString("dd/MM/yyyy"), id ="q" + @item.questionID, Name = "Datet",style = "width: 120px",@class="datepick" }) </td>
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.