Reputation: 28545
I have this in my controller, it it being posted to on a form submit.
[HttpPost]
public ActionResult Add(OrderDetailsViewModel thisWindowModel)
{
if (!ModelState.IsValid)
{
return View(thisWindowModel);
}
When model staie is invalid and I return the model back to the view Im getting an object reference not set exception pointing at this line:
<h2>Order Details:@Model.Style.Name</h2>
I've inspected the "thisWindowModel" as it enters the httppost method, and style is indeed null there.... but why is it doing that?/how to fix?
Update
I have tried adding hiddenfor now too, and even changed my model to to just use stylename as a string but it still fails throws a null refference exception.
@if(Model==null)
{
<h2>model null</h2>
}else{
if(Model.StyleName==null)
{
<h2>model style null</h2>
}
<h2>Order Details:@Model.StyleName</h2>
}
at the last '}
'
Upvotes: 0
Views: 76
Reputation: 62488
you have to post the @Model.Style.Name
in the form post, you have just used it in your view in heading tag but you need to post all model properties in a form , otherwise it will be posted null
Use @Html.HiddenFor(x=>x.Style.Name)
in your form so that this property value is posted in form.
Upvotes: 1
Reputation: 25370
in your form, add an Html.HiddenFor()
for your model item. As you have it, it has no idea how to recreate Model.Style.Name
when rebuilding the model on postback.
Upvotes: 1