Reputation: 295
I have absolutely no idea why my model is null when I'm trying to submit this form with only ONE field, a dropdownlistfor selected value.
The Get works just fine, and the Model is defintively not null. But everytime I try to submit the form, model is always null and I have no idea why at the moment:
Model:
[Required]
public string SelectedOrderStatus { get; set; }
public List<SelectListItem> OrderStatuses { get; set; }
View:
@model Webstore.Models.OrderViewModel
@using (Html.BeginForm("Edit", "Order", FormMethod.Post))
{
@Html.AntiForgeryToken()
@Html.HiddenFor(m => m.OrderId)
<div class="form-horizontal">
<h4>Change Order Status for order: @Model.OrderId</h4>
<div class="form-group">
@Html.LabelFor(model => model.Orderstatus, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(model => model.SelectedOrderStatus, new SelectList(Model.OrderStatuses, "Value", "Text"))
@Html.ValidationMessageFor(model => model.SelectedOrderStatus)
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save Order Status" class="btn btn-default" />
</div>
</div>
</div>
}
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
Controller
[HttpPost]
public ActionResult Edit(Guid id, OrderViewModel model)
{
try
{
Order orderToEdit = _context.Orders.Find(id);
orderToEdit.Orderstatus = (Orderstatus)Enum.Parse(typeof(Orderstatus), model.SelectedOrderStatus);
_context.Entry(orderToEdit).State = EntityState.Modified;
_context.SaveChanges();
return RedirectToAction("Index");
}
catch
{
return View();
}
}
I would appreaciate a lot if you would help me out here!
Regards!
Upvotes: 0
Views: 110
Reputation: 4443
Try to check if FormCollection collection
have value which you need. So your Edit
methods will looks like:
public ActionResult Edit(Guid id, FormCollection collection)
{
// rest of logic here
}
Optionally, check in Request[..]
, like here:
public ActionResult Edit(Guid id)
{
var value1 = Request["SelectedOrderStatus"];
}
Of course this is not as beatifull solution as it should be, but there is some problem with model blinding which I cannot resolve without rest of code.
Upvotes: 1