我的视图模型中有一个IEnumerable DropDownListFor。 这最初是我现在已经转换的可以为空的int。
但是现在我在我的表单的post方法中有一个错误,我认为该错误来自该方法使用的服务中的Variable。
错误是:错误2参数12:无法从'System.Collections.Generic.IEnumerable'转换为'int?'
我更改为IEnumerable的LicenceType及其在视图模型中的部分是:
public IEnumerable<LicenceType> LicenceTypes { get; set; }
服务代码是:
public GRCMemberService(ApplicationDbContext dbContext)
{
db = dbContext;
}
public void CreateGRCMember(string firstName, string lastName, string address1, string address2, string city, string county, string postcode, string telephone, DateTime dateOfBirth, string dietary, string compLicenceNo , int? LicenceTypeId, string nOKFirstName, string nOKLastName, string nOKTelephone, int? relationshipTypeId, bool otherOrgsGRC, bool otherClubEvents, bool otherOrgsOutside, string userId)
{
var GRCMember = new GRCMember { FirstName = firstName, LastName = lastName, Address1 = address1, Address2 = address2, City = city, County = county, Postcode = postcode, Telephone = telephone, DateOfBirth = dateOfBirth, Dietary = dietary, CompLicenceNo = compLicenceNo, LicenceTypeId = LicenceTypeId, NOKFirstName = nOKFirstName, NOKLastName = nOKLastName, NOKTelephone= nOKTelephone, RelationshipTypeId = relationshipTypeId, OtherOrgsGRC = otherOrgsGRC, OtherClubEvents = otherClubEvents, OtherOrgsOutside = otherOrgsOutside, ApplicationUserId = userId };
db.GRCMember.Add(GRCMember);
db.SaveChanges();
}
post方法是:
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Register(RegisterViewModel model)
{
if (ModelState.IsValid)
{
var user = new ApplicationUser { UserName = model.Email, Email = model.Email };
var result = await UserManager.CreateAsync(user, model.Password);
if (result.Succeeded)
{
UserManager.AddClaim(user.Id, new Claim(ClaimTypes.GivenName, model.FirstName));
var service = new GRCMemberService(HttpContext.GetOwinContext().Get<ApplicationDbContext>());
service.CreateGRCMember(model.FirstName, model.LastName, model.Address1, model.Address2, model.City, model.County, model.Postcode, model.Telephone, model.DateOfBirth, model.Dietary, model.CompLicenceNo, model.LicenceTypes, model.NOKFirstName, model.NOKLastName, model.NOKTelephone, model.RelationshipTypeId, model.OtherOrgsGRC, model.OtherClubEvents, model.OtherOrgsOutside, user.Id);
await SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false);
return RedirectToAction("Index", "Home");
}
AddErrors(result);
}
return View(model);
}
答案 0 :(得分:0)
感谢大家的帮助和对我的支持,这无疑是第1天的第1天问题,但谢天谢地,耐心帮助解决了这个问题。
我已将视图模型改为:
[Display(Name = "Licence Type")]
public int? SelectedLicenceTypeId { get; set; }
public IEnumerable<LicenceType> LicenceTypes { get; set; }
后方法:
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Register(RegisterViewModel model)
{
if (ModelState.IsValid)
{
var user = new ApplicationUser { UserName = model.Email, Email = model.Email };
var result = await UserManager.CreateAsync(user, model.Password);
if (result.Succeeded)
{
UserManager.AddClaim(user.Id, new Claim(ClaimTypes.GivenName, model.FirstName));
var service = new GRCMemberService(HttpContext.GetOwinContext().Get<ApplicationDbContext>());
service.CreateGRCMember(model.FirstName, model.LastName, model.Address1, model.Address2, model.City, model.County, model.Postcode, model.Telephone, model.DateOfBirth, model.Dietary, model.CompLicenceNo, model.SelectedLicenceTypeId, model.NOKFirstName, model.NOKLastName, model.NOKTelephone, model.RelationshipTypeId, model.OtherOrgsGRC, model.OtherClubEvents, model.OtherOrgsOutside, user.Id);
await SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false);
return RedirectToAction("Index", "Home");
}
AddErrors(result);
}
return View(model);
}
服务:
public void CreateGRCMember(string firstName, string lastName, string address1, string address2, string city, string county, string postcode, string telephone, DateTime dateOfBirth, string dietary, string compLicenceNo , int? LicenceTypeId, string nOKFirstName, string nOKLastName, string nOKTelephone, int? relationshipTypeId, bool otherOrgsGRC, bool otherClubEvents, bool otherOrgsOutside, string userId)
{
var GRCMember = new GRCMember { FirstName = firstName, LastName = lastName, Address1 = address1, Address2 = address2, City = city, County = county, Postcode = postcode, Telephone = telephone, DateOfBirth = dateOfBirth, Dietary = dietary, CompLicenceNo = compLicenceNo, LicenceTypeId = LicenceTypeId, NOKFirstName = nOKFirstName, NOKLastName = nOKLastName, NOKTelephone= nOKTelephone, RelationshipTypeId = relationshipTypeId, OtherOrgsGRC = otherOrgsGRC, OtherClubEvents = otherClubEvents, OtherOrgsOutside = otherOrgsOutside, ApplicationUserId = userId };
db.GRCMember.Add(GRCMember);
db.SaveChanges();
}
观点:
@Html.DropDownListFor(m => m.SelectedLicenceTypeId, new SelectList(Model.LicenceTypes, "LicenceTypeId", "Type"), new { @class = "form-control" })
全部谢谢