Asp.net mvc 在ASP核心MVC中发布后如何获取cascade selectList中的项目
我有一个国家/省/市的级联列表,它在创建和编辑操作中运行良好,除了一件事,它在编辑获取中总是变为空,下面是我的代码:Asp.net mvc 在ASP核心MVC中发布后如何获取cascade selectList中的项目,asp.net-mvc,asp.net-core,dropdown,selectlist,selectlistitem,Asp.net Mvc,Asp.net Core,Dropdown,Selectlist,Selectlistitem,我有一个国家/省/市的级联列表,它在创建和编辑操作中运行良好,除了一件事,它在编辑获取中总是变为空,下面是我的代码: public class LocationController : Controller { public List<Country> countries = new List<Country> { new Country(){Id=1,Name="Country1"}, new Cou
public class LocationController : Controller
{
public List<Country> countries = new List<Country>
{
new Country(){Id=1,Name="Country1"},
new Country(){Id=2,Name="Country2"}
};
public List<Province> provinces = new List<Province>()
{
new Province() { Id = 1,CountryId = 1,Name = "Province1"},
new Province() { Id = 2,CountryId = 2,Name = "Province2"},
};
public List<City> cities = new List<City>()
{
new City() { Id = 1,ProvinceId = 1,Name = "City1" },
new City() { Id = 2,ProvinceId = 2,Name = "City2" },
new City() { Id = 3,ProvinceId = 2,Name = "City3" },
};
public IActionResult Province(int value)
{
var l = provinces.Where(x => x.CountryId == value).ToList();
return Json(l);
}
public IActionResult City(int value)
{
var c = cities.Where(c => c.ProvinceId == value).ToList();
return Json(c);
}
}
公共类位置控制器:控制器
{
公开名单国家=新名单
{
new Country(){Id=1,Name=“Country1”},
新国家({Id=2,Name=“Country2”}
};
公共列表省份=新列表()
{
新省(){Id=1,CountryId=1,Name=“Province1”},
新省(){Id=2,CountryId=2,Name=“Province2”},
};
公共列表城市=新列表()
{
新城(){Id=1,ProvinceId=1,Name=“City1”},
新城(){Id=2,ProvinceId=2,Name=“City2”},
新城(){Id=3,ProvinceId=2,Name=“City3”},
};
公共区域结果省(int值)
{
var l=省。其中(x=>x.CountryId==值)。ToList();
返回Json(l);
}
公共城市(int值)
{
var c=cities.Where(c=>c.ProvinceId==value.ToList();
返回Json(c);
}
}
编辑视图:
<div class="form-group row">
<div class="col-2">
<label asp-for="Country" class="col-form-label"></label>
</div>
<div class="col-sm-5">
<select id="CountryList" asp-for="Country" asp-items="@new LocationController().countries.Select(c=> new SelectListItem() {Text=c.Name,Value=c.Id.ToString() }).ToList() as IEnumerable<SelectListItem>" class="form-control">
<option selected disabled value="">--- Choose ---</option>
</select>
</div>
</div>
<div class="form-group row">
<div class="col-2">
<label asp-for="Province" class="col-form-label"></label>
</div>
<div class="col-sm-5">
<select id="ProvinceList" asp-for="Province" data-url="@Url.Action("Province","Location")" class="form-control">
<option selected disabled value="">--- Choose ---</option>
</select>
</div>
</div>
<div class="form-group row">
<div class="col-2">
<label asp-for="City" class="col-form-label"></label>
</div>
<div class="col-sm-5">
<select id="CityList" asp-for="City" data-url="@Url.Action("City","Location")" class="form-control">
<option selected disabled value="">--- Choose ---</option>
</select>
</div>
</div>
---选择---
---选择---
---选择---
这是Javascript:
@section Scripts {
<script>
$(function () {
$("#CountryList").change(function () {
$("#CityList").empty();
var v = $(this).val();
var url = $("#ProvinceList").data("url") + '?value=' + v;
$.getJSON(url, function (data) {
$("#ProvinceList").empty();
$("#ProvinceList").append('<option selected disabled value="">--- Choose ---</option>');
$.each(data, function (i, item) {
$("#ProvinceList")
.append($("<option>").text(item.name).val(item.id));
});
});
});
$("#ProvinceList").change(function () {
var v = $(this).val();
var url = $("#CityList").data("url") + '?value=' + v;
$.getJSON(url, function (data) {
$("#CityList").empty();
$("#CityList").append('<option selected disabled value="">--- Choose ---</option>');
$.each(data, function (i, item) {
$("#CityList")
.append($("<option>").text(item.name).val(item.id));
});
});
});
});
$('#formId').submit(function () {
$('#CountryList option').val(function () {
return $(this).text();
});
$('#ProvinceList option').val(function () {
return $(this).text();
});
$('#CityList option').val(function () {
return $(this).text();
});
});
</script>
}
@节脚本{
$(函数(){
$(“#国家列表”).change(函数(){
$(“#城市列表”).empty();
var v=$(this.val();
变量url=$(“#省列表”)。数据(“url”)+'?值='+v;
$.getJSON(url、函数(数据){
$(“#ProvinceList”).empty();
$(“#ProvinceList”).append('---选择---');
$。每个(数据、功能(i、项){
$(“#省列表”)
.append($(“”).text(item.name).val(item.id));
});
});
});
$(“#ProvinceList”)。更改(函数(){
var v=$(this.val();
变量url=$(“#城市列表”)。数据(“url”)+'?值='+v;
$.getJSON(url、函数(数据){
$(“#城市列表”).empty();
$(“#城市列表”).append('---选择---');
$。每个(数据、功能(i、项){
$(“城市列表”)
.append($(“”).text(item.name).val(item.id));
});
});
});
});
$('#formId')。提交(函数(){
$('#CountryList选项').val(函数(){
返回$(this.text();
});
$('#ProvinceList选项').val(函数(){
返回$(this.text();
});
$(“#城市列表选项”).val(函数(){
返回$(this.text();
});
});
}
当然,在get操作中,我试图从数据库中获取用户的位置,并在get中工作:
[HttpGet]
public async Task<IActionResult> Edit(string id)
{
var user = await _userManager.FindByIdAsync(id);
EditUserViewModel modelVM = new EditUserViewModel
{
Country = user.Country,
Region = user.Region,
City = user.City,
};
return View(modelVM);
}
[HttpGet]
公共异步任务编辑(字符串id)
{
var user=await\u userManager.FindByIdAsync(id);
EditUserViewModel modelVM=新的EditUserViewModel
{
国家=用户。国家,
Region=user.Region,
城市=用户。城市,
};
返回视图(modelVM);
}
但在视图中,省/地区和城市是空的:
如果单击“更新”,省和市将为空。以下是一个关于如何将所选项目传递给操作的工作演示: 型号:
public class Country
{
public int Id { get; set; }
public string Name { get; set; }
}
public class Province
{
public int Id { get; set; }
public string Name { get; set; }
public int CountryId { get; set; }
}
public class City
{
public int Id { get; set; }
public string Name { get; set; }
public int ProvinceId { get; set; }
}
public class UserProfile
{
public string Id { get; set; }
public string City { get; set; }
public string Country { get; set; }
public string Province { get; set; }
}
public class EditUserViewModel
{
public string City { get; set; }
public string Country { get; set; }
public string Province { get; set; }
}
更新:
似乎你想编辑一个用户,而编辑视图会显示用户默认的城市、省份和国家。所以我认为你的js在编辑视图中没有必要
下面是一个工作演示,如下所示:
型号:
public class Country
{
public int Id { get; set; }
public string Name { get; set; }
}
public class Province
{
public int Id { get; set; }
public string Name { get; set; }
public int CountryId { get; set; }
}
public class City
{
public int Id { get; set; }
public string Name { get; set; }
public int ProvinceId { get; set; }
}
public class UserProfile
{
public string Id { get; set; }
public string City { get; set; }
public string Country { get; set; }
public string Province { get; set; }
}
public class EditUserViewModel
{
public string City { get; set; }
public string Country { get; set; }
public string Province { get; set; }
}
cshtml(显示用户数据):
@model IEnumerable
@Html.DisplayNameFor(model=>model.Country)
@DisplayNameFor(model=>model.Province)
@DisplayNameFor(model=>model.City)
@foreach(模型中的var项目)
{
@DisplayFor(modelItem=>item.Country)
@DisplayFor(modelItem=>item.Province)
@DisplayFor(modeleItem=>item.City)
您的获取操作是什么?您的意思是当您将选择项发布到编辑获取操作时,数据始终为空吗?@Rena没有数据,但不在视图中。很抱歉不清楚,我添加了更多详细信息。非常感谢!!这非常有效,我非常感谢您的帮助!
public class HomeController : Controller
{
private List<UserProfile> users = new List<UserProfile>()
{
new UserProfile(){Id="1",Province="1",Country="1",City="1"},
new UserProfile(){Id="2",Province="2",Country="2",City="3"},
};
public IActionResult Index()
{
return View(users);
}
[HttpGet]
public async Task<IActionResult> Edit(string id)
{
var user = users.Where(a => a.Id == id).FirstOrDefault();
ViewBag.Province = new SelectList(new LocationController().provinces,"Id","Name", user.Province);
ViewBag.City = new SelectList(new LocationController().cities,"Id","Name", user.City);
ViewBag.Country = new SelectList(new LocationController().countries,"Id","Name", user.Country);
EditUserViewModel modelVM = new EditUserViewModel
{
Country = user.Country,
Province = user.Province,
City = user.City,
};
return View(modelVM);
}
[HttpPost]
public IActionResult Edit(string city, string province, string country)
{
return RedirectToAction("Index");
}
}