1

我在数据库中有两个表。 我有两个表城市,区域

之后我想为城市和相应区域创建下拉列表。这是我一生中的第一个级联下拉菜单。我尝试遵循一些示例。在我的 get 操作中:

      ViewBag.cities = db.cities.ToList();
      ViewBag.areas = db.areas.ToList();

在我看来:

       <div class="editor-field">
         @Html.DropDownListFor(model => model.city,new SelectList(ViewBag.cities as 
         System.Collections.IEnumerable, "city_id", "name"),
         "Select City", new { id = "ddlCars" }) 
         @Html.ValidationMessageFor(model => model.city)
       </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.area)
    </div>
    <div class="editor-field">
        @Html.DropDownListFor(model => model.area, new 
         SelectList(Enumerable.Empty<SelectListItem>(), "area_id", "name"),
         "Select Area", new { id = "ddlModels" })
        @Html.ValidationMessageFor(model => model.area)
    </div>

我刚刚从一个网站复制了一个 js 文件

       $(document).ready(function () {
       $("#ddlCars").change(function () {
       var idModel = $(this).val();
       $.getJSON("/Post/LoadAreasByCity", { id: idModel },
       function (carData) {
        var select = $("#ddlModels");
        select.empty();
        select.append($('<option/>', {
        value: 0,
       text: "Select Area"
       }));
       $.each(carData, function (index, itemData) {

        select.append($('<option/>', {
        value: itemData.Value,
        text: itemData.Text
         }));
        });
        });
        });


       $("#ddlModels").change(function () {
       var idColour = $(this).val();
      $.getJSON("/Home/LoadColoursByModel", { id: idColour },
      function (modelData) {
      var select = $("#ddlColours");
      select.empty();
      select.append($('<option/>', {
      value: 0,
      text: "Select a Colour"
      }));
     $.each(modelData, function (index, itemData) {

      select.append($('<option/>', {
      value: itemData.Value,
      text: itemData.Text
      }));
      });
      });
      });
      });

在我的 Post/LoadAreasByCity 我的方法是:

          public JsonResult LoadAreasByCity(string id)
          {
           PostManager pm = new PostManager();
           if ( id=="") { id = "1"; }
           int c_id =(int) Convert.ToInt32(id);
           var areaList = pm.GetModels(c_id);

           var modelData = areaList.Select(m => new SelectListItem()
               {
              Text = m.name,
              Value = m.area_id.ToString(),

           });
           return Json(modelData, JsonRequestBehavior.AllowGet);
           } 

它在视图页面中正确传播城市和区域。但是在提交数据后,它在这一行中给出了错误

         @Html.DropDownListFor(model => model.city,new SelectList(ViewBag.cities as   
         System.Collections.IEnumerable, "city_id", "name"),"Select City", new { id = 
         "ddlCars" }) 

它说值不能为空。参数名称:items

最后在我的帖子中

                int c_id = Convert.ToInt32(p.city);
                int a_id = Convert.ToInt32(p.area);
                area a = db.areas.Single(x=>x.area_id == a_id);
                city c = db.cities.Single(s => s.city_id == c_id);
                post.city = c.name;
                post.area = a.name;
                ....Save to DB

这是什么问题.....提前谢谢

4

1 回答 1

0

我怀疑在您的 POST 操作中,您重新显示了相同的视图,但忘记填充ViewBag.citiesViewBag.areas项目,就像您在 GET 操作中所做的那样:

[HttpPost]
Public ActionResult Process()
{
    ... Save to DB

    ViewBag.cities = db.cities.ToList();
    ViewBag.areas = db.areas.ToList();   
    return View();
}
于 2012-05-07T11:39:16.520 回答