DropDownList for referenced values - on Post: Create - NullReferenceException
On the SaveChanges i get an NullReferenceException.
This is my DropDownList in my view:
@Html.DropDownList("MaterialCategory.Id", Mod开发者_JAVA技巧el.Categorieën)
The selected value is inserted in : MaterialDetail.MaterialCategory.Id , but all the other values are not inserted (eg. name and description of the Category).
[HttpPost]
public ActionResult Create(db.MaterialDetail materiaal_detail)
{
try
{
// TryUpdateModel<db.MaterialDetail>(materiaal_detail); ???
materiaal_detail.Id = Guid.NewGuid();
_db.SaveChanges();
return RedirectToAction("Index");
}
catch
{
return View();
}
}
In short, i receive the selected id but can't seem to save it to the database. The problem has to be somewhere in the entities.
MaterialDetail => OK MaterialCategory, has the id and i thought when i'd save it, it would only insert the appropriate GUID in the MaterialDetail.CategoryId. But this ain't happening.
And this is my Create (View)
<fieldset>
<legend>MaterialDetail</legend>
<div class="editor-label">
Category
</div>
<div class="editor-field">
@Html.DropDownList("MaterialCategory.Id", Model.Categorieën)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Materiaal.name)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Materiaal.name)
@Html.ValidationMessageFor(model => model.Materiaal.name)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Materiaal.description)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Materiaal.description)
@Html.ValidationMessageFor(model => model.Materiaal.description)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Materiaal.inStock)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Materiaal.inStock)
@Html.ValidationMessageFor(model => model.Materiaal.inStock)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
The Guid is inserted in the MaterialCategory.Id How can i solve this?
All your input fields will be prefixed with Materiaal
except the dropdownlist where you have hardcoded it. So you might need to specify this prefix if you want the model binder to be able to properly assign the values:
[HttpPost]
public ActionResult Create([Bind(Prefix = "Materiaal")]db.MaterialDetail materiaal_detail)
{
...
}
or use the top level model (the one that contains the Materiaal
):
[HttpPost]
public ActionResult Create(SomeModel model)
{
db.MaterialDetail m = model.Materiaal;
...
}
Also I feel obligated to point out that using your EF models into the views is BAD practice. I would recommend you design view models which are classes specifically tailored to the needs of the view and contain only the properties necessary for this view. To facilitate the mapping between the models and the view models you could use AutoMapper.
So here are two scenarios:
The controller queries a repository and fetches a model. The controller maps this model into a view model and passes the view model to the view
The controller receives a view model (as argument) from the view. The controller maps the view model to the corresponding model. The controller calls a repository method to update something passing it the model.
精彩评论