我有一个MVC3项目(使用C#)并且正在使用Razor和EntityFramework 4.1,我想完成以下任务:
提前致谢!
我已经创建了我的模型,
public class Provinces
{
[Key]
public int ProvinceID {get; set;}
public string Province {get; set;}
public virtual ICollection<City> City {get;set;}
}
public class City
{
[Key]
public int CityID {get; set;}
public string City {get; set;}
public virtual ICollection<Suburb> Suburb {get; set;}
public virtual Province Province {get; set;}
}
public class Suburb
{
[Key]
public int SuburbID {get; set;}
public string Suburb {get; set;}
public virtual City City {get; set;}
}
用于处理下拉列表的ViewModel
public class MyViewModel
{
public string SelectedProvinceID {get; set;}
public string SelectedCityID {get; set;}
public IEnumerable<Province> Province {get; set;}
}
控制器
public class Suburbs : Controller
{
MyEntities suburbsDB = new MyEntities();
public ActionResult Create()
{
//For Province dropdown
ViewBag.Province = suburbsDB.Province.OrderBy(prov => prov.Province).ToList();
var suburbDetails = new Suburb();
return View(suburbDetails);
}
我对EntityFramework,Json和Razor很陌生,我对如何创建视图有疑问。我在MVC3 Razor视图中的级联下拉列表中引用了答案Cascading drop-downs in MVC 3 Razor view
如何根据郊区下拉列表的值刷新表格?
答案 0 :(得分:0)
现在有点老了,但请看看斯蒂芬·沃尔特(Stephen Walther)的这篇文章:
他创建了两个下拉列表并使用jQuery / Ajax获取列表,以便在第一个中选择一个值时填充第二个(您可以轻松地将其扩展为第三个下拉列表):
点击文章查看完整代码,但这里有一段显示一些重要的javascript(我添加了一些评论):
function pageLoad() {
ddlMakes = $get("Makes");
ddlModels = $get("Models");
// Bind the "bindOptions" function to the change event of the ddlMakes dropdown
$addHandler(ddlMakes, "change", bindOptions);
// Call the function immediately, so the next dropdown will be populated if there is already something selected in the first
bindOptions();
}
function bindOptions() {
// Clear out existing options in the second dropdown
ddlModels.options.length = 0;
// If something is selected in the first dropdown
var makeId = ddlMakes.value;
if (makeId) {
// Send an ajax request to the corresponding url
var url = "/Action/Models/" + makeId;
getContent(url, bindOptionResults);
}
}
function bindOptionResults(data) {
var newOption;
// Loop through the options in the response
for (var k = 0; k < data.length; k++) {
// Add a new option to the second dropdown for this item
newOption = new Option(data[k].Name, data[k].Id);
ddlModels.options.add(newOption);
}
}
答案 1 :(得分:0)
在您看来,您定义了两个下拉菜单,类似于:
@Html.DropDownListFor(model => model.Provinces.ProvinceID, Model.Province, new { id = "ddlProvinces", onChange = "$:populateCitiesDropdown()" })
@Html.DropDownListFor(model => model.City.CityID, Model.City, new { id = "ddlCities" })
在Javascript中,您的populate方法如下所示:
function populateIssuesDropdown() {
$("#ddlCities").empty();
var typeID = getProvinceID();
$.getJSON('@Url.Action("GetCitiesByProvince", "some_controller")?TypeID=' + typeID, function (result) {
$.each(result, function () {
$("<option>").attr("value", this.CityID).text(this.City).appendTo("#ddlCities");
});
});
}
function getProvinceID() {
var provinceID = $("#ddlProvinces").val();
return provinceID;
}
在您的控制器中:
public JsonResult GetCitiesByProvince(int ProvinceID)
{
var result = _someRepository.GetCities(ProvinceID).Select(
c => new { CityID = c.CityID, City = c.City });
JsonResult jsonResult = new JsonResult { Data = result, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
return jsonResult;
}