我是MVC新手并构建一个包含一些表单字段的页面,然后是一个数据列表,其中列表中的每个项目都有一个复选框。
用户填写表单并检查所需的复选框,然后提交表单。问题是复选框列表总是返回null
。
查看模型 - 有关客户端和用户的信息,然后是要显示复选框的列表。
public class ClientUserViewModel
{
public Client ClientDetail { get; set; }
public ClientUser ClientUserDetail { get; set; }
public List<ClientSite> ClientSitesList { get; set; }
}
客户端网站模型 - 网站列表的数据和SelectedSites
保持复选框状态
public class ClientSite
{
public int SiteId { get; set; }
public int ClientId { get; set; }
public string SiteName { get; set; }
public string Address1 { get; set; }
public string Address2 { get; set; }
public string Town { get; set; }
public string County { get; set; }
public string Postcode { get; set; }
public string Tel { get; set; }
public string Email { get; set; }
public bool Enabled { get; set; }
public bool SelectedSites { get; set; }
}
列出视图的一部分
@foreach (var item in Model.ClientSitesList)
{
<tr>
<td>@item.SiteName</td>
<td>
@{
string fullAddress = "";
fullAddress += (String.IsNullOrEmpty(@item.Address1) ? "" : @item.Address1);
fullAddress += (String.IsNullOrEmpty(@item.Address2) ? "" : ", " + @item.Address2);
fullAddress += (String.IsNullOrEmpty(@item.Town) ? "" : ", " + @item.Town);
fullAddress += (String.IsNullOrEmpty(@item.Postcode) ? "" : ", " + @item.Postcode);
}
@fullAddress
</td>
<td>@item.Tel</td>
<td><a href="mailto:@(item.Email)">@item.Email</a></td>
<td style="text-align:right;">
@Html.CheckBoxFor(model => item.SelectedSites, new { @class = "form-control chk"})
</td>
</tr>
}
控制器
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(ApplicationDbContext DbContext, ClientUserViewModel model, List<ClientSite> ClientSitesList)
{
// create new user under selected client
try
{
if (ModelState.IsValid)
{
IdentityResult ir;
var rm = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(DbContext));
//ir = rm.Create(new IdentityRole("pxpuser"));
var um = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(DbContext));
um.UserValidator = new UserValidator<ApplicationUser>(UserManager)
{
AllowOnlyAlphanumericUserNames = false
};
var user = new ApplicationUser()
{
UserName = model.ClientUserDetail.Email,
Email = model.ClientUserDetail.Email,
FirstName = model.ClientUserDetail.Firstname,
LastName = model.ClientUserDetail.Surname,
UserGroup = "Client"
};
ir = um.Create(user, model.ClientUserDetail.Password);
if (ir.Succeeded == false)
{
Response.Write(ir.Errors);
return View(model);
}
else
{
// add new user to pxpclient role
ir = um.AddToRole(user.Id, "pxpclient");
// insert sites user has access to
foreach (var site in model.ClientSitesList)
{
if (site.SelectedSites == true)
{
var userSites = new PgsUsersSite()
{
UserId = user.Id,
SiteId = site.SiteId
};
context.PgsUsersSites.InsertOnSubmit(userSites);
context.SubmitChanges();
}
}
// insert new client user details
var clientUser = new PgsUser()
{
UserId = user.Id,
ClientId = model.ClientDetail.ClientId,
Email = model.ClientUserDetail.Email,
Firstname = model.ClientUserDetail.Firstname,
Surname = model.ClientUserDetail.Surname,
AppointmentBooked = model.ClientUserDetail.AppointmentBooked,
PostVisit = model.ClientUserDetail.PostVisit,
PxOffer = model.ClientUserDetail.PxOffer,
SurveyInstruction = model.ClientUserDetail.SurveyInstruction,
EstateAgentInstruction = model.ClientUserDetail.EstateAgentInstruction,
BuyinExchange = model.ClientUserDetail.BuyinExchange,
BuyinNotes = model.ClientUserDetail.BuyinNotes,
SolicitorInstruction = model.ClientUserDetail.SolicitorInstruction,
AccessCaseDetails = model.ClientUserDetail.AccessCaseDetails,
AccessFeedback = model.ClientUserDetail.AccessFeedback,
AccessImages = model.ClientUserDetail.AccessImages,
AccessValuations = model.ClientUserDetail.AccessValuations,
AccessPxOffer = model.ClientUserDetail.AccessPxOffer,
AccessLegal = model.ClientUserDetail.AccessLegal,
AccessReports = model.ClientUserDetail.AccessReports
};
context.PgsUsers.InsertOnSubmit(clientUser);
context.SubmitChanges();
}
return RedirectToAction("Edit", "Clients", new { id = model.ClientUserDetail.ClientId });
}
}
catch (Exception ex)
{
ModelState.AddModelError(string.Empty, ex.Message);
}
return View(model);
}
因此,无论选择了多少个复选框,model.ClientSitesList
始终都会返回null
。