0

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");
        }
BorHunter
  • 893
  • 3
  • 18
  • 44

2 Answers2

0

Use the DropDownListFor method instead:

http://msdn.microsoft.com/en-us/library/system.web.mvc.html.selectextensions.dropdownlistfor(v=vs.118).aspx

Edit based on question in comment

Here is the modified code for you:

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.DropDownListFor(m=>m.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>
                    }
Mr.Brownstone
  • 714
  • 6
  • 14
0

What you want to do is bind the selection to your model. So, you should use the DropDownListFor method instead.

To do that you should change the line

 @Html.DropDownList("CityId", new SelectList(listItemsCities, "Value", "Text", order.CityId), "")

to this

 @Html.DropDownListFor(m=>m.CityId, new SelectList(listItemsCities, "Value", "Text", order.CityId), "")

See this MSDN article and the answer here is also a good implementation for the solution.

Community
  • 1
  • 1
Charmless
  • 61
  • 2