System.InvalidOperationException:'具有键'X的ViewData项的类型为'System.Int32',但必须类型为'IEnumerable <SelectListItem>'

问题描述

我想创建我的联系人时遇到此错误。需要帮助我会很感激。 我的数据库sql数据库表是通过实体框架连接的。

家庭控制器/创建动作

[HttpGet]
    public ActionResult Create()

    {
        List<Country> CountryList = db.Countries.ToList();
        ViewBag.CountryList = new SelectList(CountryList,"CountryId","CountryName");

        return View();

    }






    [HttpPost]
    [ValidateAntiForgeryToken]





 public ActionResult Create(CountryStateContactviewmodel csvm)


    {

        if (!ModelState.IsValid)

        {

            return View(csvm);

        }
       
        Contact model = new Contact()
        {
            CountryId = csvm.CountryId,StateId = csvm.StateId,ContactId = csvm.ContactId,ImeOsobe = csvm.ImeOsobe
            ...and all other fields

            
        };
        db.Contacts.Add(model);

        try
        {
            db.SaveChanges();
        }
        catch (System.Data.Entity.Validation.DbEntityValidationException db)
        {
            Exception raise = db;
            foreach (var validationErrors in db.EntityValidationErrors)
            {
                foreach (var validationError in validationErrors.ValidationErrors)
                {
                    string message = string.Format("{0}:{1}",validationErrors.Entry.Entity.ToString(),validationError.ErrorMessage);

                    raise = new InvalidOperationException(message,raise);
                }
            }
            throw raise;

        }
        return RedirectToAction("Index");

    }

现在是viewmodel

public int CountryId { get; set; }
    [required]
    [display(Name = "Naziv županije")]
    public string CountryName { get; set; }

    public int StateId { get; set; }
    [required]
    [display(Name = "Naziv općine")]
    public string StateName { get; set; }


    public int ContactId { get; set; }
    public virtual Country Country { get; set; }
    public virtual State State { get; set; }
    ....and all other fields

最后是“创建”视图。

@model AkvizicijeApp_4_7.Models.CountryStateContactviewmodel




 @{
      ViewBag.Title = "Create";
  }

 <h2>Create</h2>

 @Html.ValidationSummary(true,"",new { @class = "text-danger" })
 <div class="form-group">
    @Html.LabelFor(model => model.CountryId,htmlAttributes: new { @class = "control-label col-md-2" 
 })
    <div class="col-md-10">
    @Html.DropDownListFor(model => model.CountryId,ViewBag.CountryList as SelectList,"--Select 
 Country--",new { @class = "form-control" })
    @Html.ValidationMessageFor(model => model.CountryId,new { @class = "text-danger" })
     </div>
 </div>

 <div class="form-group">
     @Html.LabelFor(model => model.StateId,htmlAttributes: new { @class = "control-label col-md-2" 
 })
<div class="col-md-10">
    @Html.DropDownListFor(model => model.StateId,new SelectList(" "),"--Select State--",new { 
 @class = "form-control" })
    @Html.ValidationMessageFor(model => model.StateId,new { @class = "text-danger" })
     </div>
 </div>


 @using (Html.BeginForm())
 {
@Html.AntiForgeryToken()


<div class="form-horizontal">
    <h4>CountryStateContactviewmodel</h4>
    <hr />
    


    <div class="form-group">
            @Html.LabelFor(model => model.ContactId,htmlAttributes: new { @class = "control-label 
 col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.ContactId,new { htmlAttributes = new { @class = 
 "form-control" } })
                @Html.ValidationMessageFor(model => model.ContactId,new { @class = "text-danger" 
  })
            </div>
        </div>

然后是父/子下拉列表的所有其他字段和js。

@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
 }
 <script src="~/Scripts/jquery-3.3.1.min.js"></script>
 <script>

$(document).ready(function () {
    $("#CountryId").change(function () {
        $.get("/Home/GetStateList",{ CountryId: $("#CountryId").val() },function (data) {
            $("#StateId").empty();
            $.each(data,function (index,row) {
                $("#StateId").append("<option value='" + row.StateId + "'>" + row.StateName + " 
 </option>")
            });
        });
    })
});
 </script>

请帮助我解决问题。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)