值不能为空。参数名称:dropDownlist

时间:2019-06-05 12:24:00

标签: c# asp.net

我正在使用数据库时出现空错误

//this is my view model
public class MasterViewModel
{
    public class AccountGroupViewModel
    {

        public string Name { get; set; }
        public AccountPrimaryGroup AccountPrimaryGroup { get; set; }
        public AccountGroupType AccountGroupType { get; set; }

    }

    public class NewAccountGroupViewModel
    {
        public AccountGroupViewModel AccountGroupViewModel { get; set; }
        public IEnumerable<AccountPrimaryGroup> AccountPrimaryGroup { get; set; }
        public IEnumerable<AccountGroupType> AccountGroupType { get; set; }
    }

    //this is my controller
    public class AccountGroupTypeViewModel
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }
    public class AccountPrimaryGroupViewModel
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }
}

public ActionResult Create()
{

    var accountPrimaryGroup = db.AccountPrimaryGroup.ToList();
    var accountGroupType= db.AccountGroupType.ToList();
    var accountGroupTypeViewModel = new NewAccountGroupViewModel
    {

        AccountGroupType=accountGroupType,
        AccountPrimaryGroup=accountPrimaryGroup

    };


    return View(accountGroupTypeViewModel);
}

// POST: AccountGroups/Create
// To protect from overposting attacks, please enable the specific properties you want to bind to, for 
// more details see https://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Create(NewAccountGroupViewModel accountGroup)
{
    if (ModelState.IsValid)
    {
        var accountGroupNew = new AccountGroup
        {

            Name = accountGroup.AccountGroupViewModel.Name,
            AccountGroupType=accountGroup.AccountGroupViewModel.AccountGroupType,
            AccountPrimaryGroup=accountGroup.AccountGroupViewModel.AccountPrimaryGroup


        };
        var accountPrimaryGroup = db.AccountPrimaryGroup.ToList();
        var accountGroupType = db.AccountGroupType.ToList();

        db.AccountGroup.Add(accountGroupNew);
        await db.SaveChangesAsync();
        return RedirectToAction("Index");
    }

    return View(accountGroup);
}

//this is my razerview
<div class="form-group">
@Html.LabelFor(model => model.AccountGroupViewModel.AccountGroupType, htmlAttributes: new { @class = "control-label col-md-2" })
   <div class="col-md-10">
        @Html.DropDownListFor(model => model.AccountGroupViewModel.AccountGroupType, new SelectList(Model.AccountGroupType, "Id","Name"), "Select Account Group", new { @class = "form-control" })
  </div>
  </div>

//这是下拉菜单的选择,这就是问题所在                  @ Html.LabelFor(model => model.AccountGroupViewModel.AccountPrimaryGroup,htmlAttributes:new {@class =“ control-label col-md-2”})                       @ Html.DropDownListFor(model => model.AccountGroupViewModel.AccountPrimaryGroup,new SelectList(Model.AccountPrimaryGroup,“ Id”,“ Name”),“ Select Account Group”,new {@ class =“ form-control”})              

//这是例外 值不能为空。 参数名称:项目 说明:执行当前Web请求期间发生未处理的异常。请查看堆栈跟踪,以获取有关错误及其在代码中起源的更多信息。

异常详细信息:System.ArgumentNullException:值不能为null。 参数名称:项目

源错误:

第27行: 第28行: 第29行:@ Html.DropDownListFor(model => model.AccountGroupViewModel.AccountGroupType,新的SelectList(Model.AccountGroupType,“ Id”,“名称”),“选择帐户组”,新的{@class =“ form-control”} ) 第30行: 第31行:

源文件:C:\ Users \ Sujit \ source \ repos \ MrSolution \ MrSolution \ Views \ AccountGroup \ Create.cshtml行:29

堆栈跟踪:

[ArgumentNullException:值不能为null。]

0 个答案:

没有答案