Reputation: 11
I'm getting this error and I can't understand what did I do wrong here.
This is the error:
The model item passed into the dictionary is of type 'System.Data.Objects.ObjectQuery`1[System.Web.Mvc.SelectListItem]', but this dictionary requires a model item of type 'MyProject.Models.cosmetic'.
This is what I've pui in my controller :
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();
}
}
This is my View (Please notice I DO have 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>
Any advice?
This is the error: This property cannot be set to a null value. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Data.ConstraintException: This property cannot be set to a null value.
Source Error:
Line 3081: Oncompany_idChanging(value); Line 3082: ReportPropertyChanging("company_id"); Line 3083: _company_id = StructuralObject.SetValidValue(value, false); Line 3084: ReportPropertyChanged("company_id"); Line 3085: Oncompany_idChanged();
This is the Code i have used :
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();
}
}
Any advice? Idea?
Upvotes: 0
Views: 674
Reputation: 39491
Everything is said by your error and code. _enteties.companies.Select returns Generic ObjectQuery
, which you directly pass to view. But your view says that it should receive type SrT2.Models.cosmetic
, not the ObjectQuery
or even IEnumerable
Upvotes: 0
Reputation: 250812
Here is a demo of how to make it work...
public ActionResult Create(string company_id)
{
var model = new SrT2.Models.cosmetic();
model.CompanyId = company_id;
return View(model);
}
If the Create view expects a "cosmetic" to be passed as the model, you need to make sure that is what you are doing in your controller...
You can add things to the model before you pass it, but it has to be the correct type.
Hopefully this will get you back on track.
Upvotes: 3