在服务中声明IEnumerable

本文关键字:IEnumerable 声明 服务 | 更新日期: 2023-09-27 17:54:49

我有一个IEnumerable DropDownListFor在我的视图模型。这最初是一个可空的整型,现在我已经转换了。

然而,现在我有一个错误在我的post方法为我的形式,我相信是从变量在我的服务中使用的方法。

错误2参数12:不能从"System.Collections.Generic"转换。IEnumerable' to 'int?'

我已经将LicenceType更改为IEnumerable,其在视图模型中的部分是:

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);
    }

在服务中声明IEnumerable

感谢大家的帮助和耐心,这无疑是一个1周1天的问题,但感谢你们的耐心帮助解决了它。

我将视图模型修改为:

[Display(Name = "Licence Type")]
    public int? SelectedLicenceTypeId { get; set; }
    public IEnumerable<LicenceType> LicenceTypes { get; set; }

Post Method to:

     [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();
    }

和view to:

@Html.DropDownListFor(m => m.SelectedLicenceTypeId, new SelectList(Model.LicenceTypes, "LicenceTypeId", "Type"), new { @class = "form-control" })

谢谢所有的