我得到這個錯誤,我不明白我在這裏做錯了什麼。一個錯誤,我不明白如何修復
這是錯誤:
傳遞到詞典中的模型項的類型爲「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();
}
}
有何建議?理念?