我得到这个错误,我不明白我在这里做错了什么。一个错误,我不明白如何修复
这是错误:
传递到词典中的模型项的类型为“System.Data.Objects.ObjectQuery`1 [System.Web.Mvc.SelectListItem]”,但本词典需要一个模型类型为“MyProject.Models.cosmetic”的项目。
这是我贝我的控制器:
public ActionResult Create(string company_id)
{
IEnumerable<SelectListItem> items = _enteties.companies.Select(x => new SelectListItem()
{
Value = x.company_id,
Text = x.company_id
});
return View(items);
}
[AcceptVerbs (HttpVerbs.Post)]
public ActionResult Create([Bind(Exclude = "ID")]cosmetic cosmeticToCreate ,company companyToCreate)
{
if (!ModelState.IsValid)
return View();
try
{
_repository.Create(cosmeticToCreate,companyToCreate);
return RedirectToAction("Index");
}
catch
{
return View();
}
}
这是我的看法(请注意我有coametic.model)
@model SrT2.Models.cosmetic
@{
ViewBag.Title = "Create";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Create</h2>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>cosmetic</legend>
<div class="editor-label">
@Html.LabelFor(model => model.brand)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.brand)
@Html.ValidationMessageFor(model => model.brand)
</div>
@Html.DropDownListFor(model => model.company_id, new SelectList(ViewData["items"] as IEnumerable<SelectListItem>,"Value","Text"))
@* @Html.DropDownListFor(model => model.company_id, ViewData["items"] as SelectList)*@
<div class="editor-label">
@Html.LabelFor(model => model.product)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.product)
@Html.ValidationMessageFor(model => model.product)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.size)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.size)
@Html.ValidationMessageFor(model => model.size)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.sale_type)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.sale_type)
@Html.ValidationMessageFor(model => model.sale_type)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.price)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.price)
@Html.ValidationMessageFor(model => model.price)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.description)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.description)
@Html.ValidationMessageFor(model => model.description)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.date)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.date)
@Html.ValidationMessageFor(model => model.date)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.company_id)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.category)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.category)
@Html.ValidationMessageFor(model => model.category)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.special)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.special)
@Html.ValidationMessageFor(model => model.special)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.male_female)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.male_female)
@Html.ValidationMessageFor(model => model.male_female)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
有什么建议?
这是错误: 此属性不能设置为空值。 描述:执行当前Web请求期间发生未处理的异常。请查看堆栈跟踪以获取有关该错误的更多信息以及源代码的位置。
异常详细信息:System.Data.ConstraintException:该属性不能设置为空值。
源错误:
线3081:Oncompany_idChanging(值); 3082行:ReportPropertyChanging(“company_id”); 3083行:_company_id = StructuralObject.SetValidValue(value,false); 3084行:ReportPropertyChanged(“company_id”); 3085行:Oncompany_idChanged();
这就是我的代码已经使用:
public ActionResult Create(string company_id)
{
var model = new SrT2.Models.cosmetic();
//There is No "model.CompanyId " so i have Pass model.cpmpany_id
model.company_id = company_id;
return View(model);
}
[AcceptVerbs (HttpVerbs.Post)]
public ActionResult Create([Bind(Exclude = "ID")]cosmetic cosmeticToCreate ,company companyToCreate)
{
if (!ModelState.IsValid)
return View();
try
{
_repository.Create(cosmeticToCreate,companyToCreate);
return RedirectToAction("Index");
}
catch
{
return View();
}
}
有何建议?理念?