【发布时间】:2015-08-18 15:07:52
【问题描述】:
这是我第一次在这里发布问题。我是 mvc 的新手。 我想开发两个级联下拉列表。我正在使用 mvc4。这是我所做的。
工厂类
public class Factory
{
[Key]
public int FactoryId { get; set; }
[Required(ErrorMessage = "Required")]
public string FactoryCode { get; set; }
[Required(ErrorMessage = "Required")]
public string FactoryName { get; set; }
public int City { get; set; }
public int Country { get; set; }
我有分别具有外键关系的 Country 和 City 类
城市类
public class City
{
[Key]
public int CityId { get; set; }
[Required(ErrorMessage = "Required")]
public string CityCode { get; set; }
[Required(ErrorMessage = "Required")]
public string CityName { get; set; }
[ForeignKey("Country")]
public int CountryId { get; set; }
public virtual Country Country { get; set; }
}
国家级
public class Country
{
[Key]
public int CountryId { get; set; }
[Required(ErrorMessage = "Required")]
public string CountryCode { get; set; }
[Required(ErrorMessage = "Required")]
public string CountryName { get; set; }
public virtual List<City> cities { get; set; }
}
工厂控制器
public ActionResult FactoryIndex(string A, int? Id, string sortOrder, string currentFilter, string searchString, int? page)
{
var objContext = new KnittingdbContext();
if (A == "New")
{
var model = new Factory();
ViewData["mdl"] = model;
ViewBag.CountryList = objContext.Countries;
ViewBag.Module = A;
}
.....................
在 IndexView 中渲染局部视图
@if (ViewBag.Module != null)
{
if (ViewBag.Module == "New")
{
Html.RenderPartial("~/Areas/Masters/Views/Factory/_FactoryCreate.cshtml", ViewData["mdl"]);
}
_Factory创建局部视图
@Html.DropDownListFor(a=>a.Country,new SelectList(ViewBag.CountryList, "CountryId", "CountryName"), "Select country", new {id="CountryId", @class = "form-control"})
@Html.DropDownListFor(a => a.City; a.City, new SelectList(Enumerable.Empty<SelectListItem>(), "CityId", "CityName"), "Select city", new {@class = "form-control" })
局部视图中的 jquery 脚本
$(function() {
$('#CountryId').change(function () {
$.ajax({
url: '/Factory/FillCity/',
type: "GET",
dataType: "JSON",
data: { Country: countryId },
success: function (cities) {
$("#City").html("");
$.each(cities, function (i, city) {
$("#City").append(
$('<option></option>').val(city.CityId).html(city.CityName));
});
}
});
})
})
FactoryController 中获取相关城市数据的 ActionResult
public ActionResult FillCity(int country)
{
var db = new KnittingdbContext();
var cities = db.Cities.Where(c =>c.CountryId == country);
return Json(cities, JsonRequestBehavior.AllowGet);
}
国家/地区下拉列表有效。
但城市 DDL 不起作用。数据没有被绑定。
当CountryId 被更改时,它涉及到 jquery 脚本。我可以使用警报识别它。但是在脚本中的 URL 属性之后它不起作用。
Success 方法没有被执行。我认为问题在于我呈现网址的方式。我尝试了不同的方法。但还是没能解决。这些视图和控制器位于一个区域中。
请帮助我。提前致谢!
【问题讨论】:
-
你永远不会向
countryId声明或赋值,所以控制器中的country会为空。 -
首先非常感谢您的回复。正如你所说,我试图为 countryId 分配一个值。我是这样做的。数据{国家:国家}。但它没有用。然后我将其更改为 data{Country:countryId=CountryId}。但仍然不起作用。我认为在从 ddl 选择项目时第一个 ddl 值发生更改后,countryId 被一个值分配,然后将其作为参数发送到控制器方法。请帮忙..
-
您的代码中还有一些其他问题。您能否发布
City的模型(由db.Cities返回) - 不妨为您修复它们:) -
好吧,正如你所说,我在上面添加了 City 和 Country 类并改进了代码 :)
-
你在data中的参数Country和你在FillCity中传入的参数不匹配。一个有大写字符。是故意的吗?
标签: jquery json asp.net-mvc-4