0

I want to display a checkbox in view using razor syntax. the model property IsActive should be int. Model is:-

public class Student
    {
        public int StudentID { get; set; }
        public string Code { get; set; }
        public string Description { get; set; }
        public int IsActive { get; set; }           
}

View is:-

<div class="editor-label">
        @Html.LabelFor(model => model.IsActive)
    </div>
    <div class="editor-field">
        @Html.CheckBoxFor(model => model.IsActive)
        @Html.ValidationMessageFor(model => model.IsActive)
    </div>

I should display checkbox and if checkbox is checked,1 should be saved to the database and if umchecked 0 should be saved.

Pls help me... Thanks..

1
  • guessing the data table was using an int or bit... a lot of sql tables get setup that way.. if so, create a bool for the checkbox, then use an [if] in the controller to convert the value to the int or bit required for your table. if (ckActive) { isActive = 1; } else { isActive = 0; } Commented Mar 19, 2019 at 3:38

2 Answers 2

3

What don't you understand in the error message ? You can't use CheckBoxFor for an int property.

You should change your model and set

public bool IsActive {get;set;}

Now, there's probably a good reason for it to be an int, but hard to say why with your code...

if IsActive can be only 0 or 1, you should use a ViewModel class, and use a boolean property, which you will map to your class.

public class StudentViewModel
    {
        public int StudentID { get; set; }
        public string Code { get; set; }
        public string Description { get; set; }
        public bool IsActive { get; set; }           
}

then when you get your student from your db (in your "GET" action")

something like that (rustic)

public ActionResult StudentEdit(int id) {
  var student = getStudentById(id);
  var model = new StudentViewModel {
                  StudentId = student.StudentID,
                  Code = student.Code,
                  Description = student.Description,
                  IsActive = student.IsActive == 1 
              };

  return View(model);
}

Your view should be typed with StudentViewModel

@model StudentViewModel

then in your POST action, reverse

[HttpPost]
public ActionResult StudentEdit(StudentViewModel model) {
    var student = getStudentById(model.StudentID);
    student.Code = model.Code;
    student.Description = model.Description;
    student.IsActive = model.IsActive ? 1 : 0;
    UpdateStudent(student);

    return Redirect...
}

EDIT :

This is a really rustic answer, and I'm sure you could do better.

For that, we would need some more infos :

Are you using Entity Framework (version) ?

If yes, Code first, Database first, model first ?

Which database is behind ?

Can you change something to the database ?

What type has IsActive in database ?

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

15 Comments

I want int in model. public int Is Active{get;set;}.
@user1697789 but it has limited values ? Say 1 or 0 ? If not, how do you think you can represent 2, or 10, or 24 in a checkbox ?
please help.my model should be int not bool.But i should get checkbox for that.
mvc4 is used.code first using model first approach..SQL SERVER 2008 R2.IsActive is int.Is shoul store only 1 and 0
got the answer- public int Active { get; set; } public bool ActiveBool { get { return Active > 0; } set { Active = value ? 1 : 0; } }
|
0

IsActive should be a bool, @Html.CheckBoxFor() helper method expects boolean parameter

change code to

public bool IsActive { get; set; } 

Comments

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.