我有一个视图,当用户点击提交时,会将文本框和复选框中的数据发回并保存到我的数据库中。首次加载视图时,某些文本框会加载来自db的数据供用户使用,我也想要预先检查某些复选框。以下是我在Get方法中到目前为止的内容:
//vm is an object of my ViewModel
[HttpGet]
public ActionResult AddOrganization(int peopleID = 0)
{
var peopleModel = db.People.Include("EmployeeContacts").Single(g => g.PeopleID == peopleID);
var contactModel = db.EmployeeContacts.Include("People").Single(g => g.PeopleID == peopleID);
vm.People = new People();
vm.EmployeeContacts = new EmployeeContacts();
vm.OrganizationsOptions = new OrganizationOptions();
// grabs info from db to be populated in the view
vm.People.NID = peopleModel.NID;
vm.People.FirstName = peopleModel.FirstName;
vm.People.LastName = peopleModel.LastName;
vm.People.SID = peopleModel.SID;
vm.EmployeeContacts.Email = contactModel.Email;
vm.EmployeeContacts.PrimaryPhone = contactModel.PrimaryPhone;
var list = new List<AddOrganizationViewModel>
{
new AddOrganizationViewModel{ID = 1, Name = "Admin", AdminChecked = true},
new AddOrganizationViewModel{ID = 2, Name = "Breakdown Report", BreakdownReportChecked = true},
new AddOrganizationViewModel{ID = 3, Name = "Favorites", FavoritesChecked = true},
new AddOrganizationViewModel{ID = 4, Name = "Site Admin", SiteAdminChecked = false},
};
return View("../Setup/AddOrganization", vm);
}
我的ViewModel如下所示:
public class AddOrganizationViewModel
{
public Music.Models.Organizations Organizations { get; set; }
public Music.Models.People People { get; set; }
public Music.Models.OrganizationOptions OrganizationsOptions { get; set; }
public Music.Models.EmployeeContacts EmployeeContacts { get; set; }
public int ID { get; set; }
public string Name { get; set; }
public bool AdminChecked { get; set; }
public bool BreakdownReportChecked { get; set; }
public bool FavoritesChecked { get; set; }
public bool SiteAdminChecked { get; set; }
}
这是我的视图中包含复选框的部分,但我不知道我需要在这里将我的ViewModel,Controller和View链接在一起,因为视图需要一种AddOrganizationViewModel,I我还坚持如何发送复选框列表。
@model Music.ViewModels.AddOrganizationViewModel
@using (Html.BeginForm("AddOrganization", "AddOrganization")){
@Html.AntiForgeryToken()
<legend>OPTIONS</legend>
<div>
@Html.Label("Features: Admin")
@Html.CheckBoxFor(Model => Model.AdminChecked)
</div>
<div>
@Html.Label("Features: Breakdown Report")
@Html.CheckBoxFor(Model => Model.BreakdownReportChecked)
</div>
<div>
@Html.Label("Features: Favorites")
@Html.CheckBoxFor(Model => Model.FavoritesChecked)
</div>
<div>
@Html.Label("Features: Site Admin")
@Html.CheckBoxFor(Model => Model.SiteAdminChecked)
</div>
}
编辑:更新的代码以反映我使用CheckBoxFor所做的更改。
答案 0 :(得分:1)
您不应该为所有复选框使用1个通用属性。
我只想在我的viewmodel中为您希望用户访问的每个功能创建一个布尔属性。
public class AddOrganizationViewModel
{
public Music.Models.Organizations Organizations { get; set; }
public Music.Models.People People { get; set; }
public Music.Models.OrganizationOptions OrganizationsOptions { get; set; }
public Music.Models.EmployeeContacts EmployeeContacts { get; set; }
public int ID { get; set; }
public string Name { get; set; }
public bool HasAdminPermissions{ get; set; }
public bool HasSomeOtherPermission{ get; set; }
//etc.. for other checkboxes you might need.
}
要预填充这些复选框,请使用我们在viewmodel中创建的属性在控制器中设置它们:
vm.HasSomeOtherPermission = true;
//etc for other properties
最后,在您看来,将@Html.CheckBox
更改为@Html.CheckBoxFor
。
然后,您可以使用viewmodel上的boolen属性来呈现复选框:
<legend>OPTIONS</legend>
@*Look into @html.checkboxfor *@
<div>
@Html.Label("Features: Admin")
@Html.CheckBoxFor(x => x.HasAdminPermissions)
</div>
<div>
@Html.Label("Features: Breakdown Report")
@Html.CheckBoxFor(x => x.HasSomeOtherPermission)
</div>