如何解决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 举报,一经查实,本站将立刻删除。