jeudi 19 février 2015

How to display razor checkbox with appropriate skills and get selected checkbox value on post method?

I am creating one registration form which contains details like:



FullName:Textbox
Email:Textbox
Skills:checkbox playing
checkbox dancing
checkbox programming


I want to display my view like this.but only problem i am getting is with my checkbox.


My Model:



public class EmployeeModel
{
public int Id { get; set; }
public string Fullname { get; set; }
public List<SelectedSkillsModel> Skills { get; set; }
}

public class SelectedSkillsModel
{
public bool Selected { get; set; }
public int Id { get; set; }
public string Name { get; set; }
}


My Controller:



public ActionResult Index()
{
var employeeModel = new EmployeeModel();
employeeModel.Skills = GetSkills();
return View(employeeModel);
}


private List<SelectedSkillsModel> GetSkills()
{
var Skills = new List<SelectedSkillsModel>();
Skills.Add(new SelectedSkillsModel { Name = "Playing", Id = 1, Selected = false });
Skills.Add(new SelectedSkillsModel { Name = "Dancing", Id = 2, Selected = false });
Skills.Add(new SelectedSkillsModel { Name = "Music", Id = 3, Selected = false });
Skills.Add(new SelectedSkillsModel { Name = "Programming", Id = 4, Selected = false });
return Skills;
}


This is my View:



<fieldset>
<legend>EmployeeModel</legend>

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

<div class="editor-field">
@Html.TextBoxFor(model => model.Email)
@Html.ValidationMessageFor(model => model.Email)
</div>

//What should i do here to display checkbox with appropraite skills???

<div class="editor-label">
@Html.LabelFor(model => model.Skills)
</div>
<div class="editor-field">
@foreach (var item in Model.Skills)
{
@Html.CheckBox();
}
</div>


When i will submit my registration form then i should get appropriate skills with Id,Selected and Name value in my EmployeeModel model.


Note:I want to use Only Razor checkbox


How do i do this???





Aucun commentaire:

Enregistrer un commentaire