I have asp.net mvc 4 project where in the razor view I have list of orders and each order is the form with value and select inputs. When I try to save entity my value input saved successfully, but select isn't send selected value, only null.
View
@{
ViewBag.Title = "";
Layout = "~/Views/Shared/_Layout.cshtml";
var cityRepo = new Repository<City>();
var listItemsCities = cityRepo.GetAll().Select(city => new ListItem() {
Value = city.Id.ToString(),
Text = city.Value
}).ToList();
}
@foreach (var order in Model) {
<tr>
@using (Html.BeginForm("UpdateDispatcherFromForm", "Home", FormMethod.Post)) {
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<td>
@Html.HiddenFor(x => order.Id)
</td>
<td>
@Html.DropDownList("CityId", new SelectList(listItemsCities, "Value", "Text", order.CityId), "")
@*@if (order.City != null) {
@order.City.Value
}*@
</td>
<td>
@Html.EditorFor(x => order.Address)
@*@order.Address*@
</td>
<td>
<input type="submit" value="Save" />
</td>
}
</tr>
}
Controller
[HttpPost]
public ActionResult UpdateDispatcherFromForm(Order order) {
_orderRepo.Update(order);
_orderRepo.SaveChanges();
return RedirectToAction("Dispatcher");
}