问题描述
嗨, 我有一个json文件,其中包含我的contentroot中的国家/地区列表。 看起来像这样
{
"countries": [
{
"name": "Afghanistan","cca2": "af","code": 93
},{
"name": "Albania","cca2": "al","code": 355
},{
"name": "Algeria","cca2": "dz","code": 213
}
]
}
我想将该国家的列表填充到我的下拉列表中,该下拉列表在我的视图中为asp-items=@Model.Countrydropdownlist,来自我的控制器,但出现空异常。 System.NullReferenceException:'对象引用未设置为对象的实例。'
我试图像这样在模型类中反序列化我的json文件
public class AccountController : Controller
{
//Other stuffs
private readonly IWebHostEnvironment _env;
public AccountController(IWebHostEnvironment env)
{
_env = env
}
public void OnGet(Registration registration)
{
registration.CountryDropDownList = GetCountryItems();
}
public List<SelectListItem> GetCountryItems()
{
string filepath = Path.Combine(_env.ContentRootPath,"CountryList.json");
string jsonlist = System.IO.File.ReadAllText(filepath);
var result = JsonConvert.DeserializeObject<RootCountry>(jsonlist);
List<SelectListItem> _countrydropdownlist = new List<SelectListItem>();
foreach (var nation in result.Countries)
{
_countrydropdownlist.Add(new SelectListItem { Value = nation.Code.ToString(),Text = nation.Name });
}
return _countrydropdownlist;
}
我相信我无效的OnGet没有被击中。 更新-我的完整查看代码。
@model TravelMate.ModelFolder.RegistrationModel.Registration
<!DOCTYPE html>
<html lang="en">
<head>
<Meta charset="utf-8" />
<Meta name="viewport" content="width=device-width,shrink-to-fit=no" />
@{
ViewBag.Title = "Registration";
}
<link href="~/bootstrap/dist/css/bootstrap.min.css" rel="stylesheet" />
</head>
<body>
<div class="container">
<div class="row">
<div class="col text-center">
<br />
<img src="~/Image/GeneralUser.jpg" width="200" height="400" />
<br />
<h3>User Registration</h3>
</div>
<form method="post">
<br />
<h3>Person Details</h3>
<div asp-validation-summary="All" class="text-danger"></div>
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label asp-for="Name"></label>
<input asp-for="Name" class="form-control" />
<span asp-validation-for="Name" class="text-danger"></span>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label asp-for="DateOfBirth"></label>
<input asp-for="DateOfBirth" class="form-control" />
<span asp-validation-for="DateOfBirth" class="text-danger"></span>
</div>
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label asp-for="Mobile"></label>
<input asp-for="Mobile" class="form-control" />
<span asp-validation-for="Mobile" class="text-danger"></span>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label asp-for="Email"></label>
<input asp-for="Email" class="form-control" />
<span asp-validation-for="Email" class="text-danger"></span>
</div>
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label asp-for="Country"></label>
<select asp-for="Country" asp-items="@Model.CountryDropDownList">
<option selected disabled> ---Select Country---</option>
</select>
<span asp-validation-for="Country" class="text-danger"></span>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label asp-for="Gender"></label>
<select asp-for="Gender" class="form-control">
<option value="0" selected disabled>Select Gender</option>
<option value="1">Male</option>
<option value="2">Female</option>
</select>
<span asp-validation-for="Gender" class="text-danger"></span>
</div>
</div>
</div>
<br />
<h3>Sign-in Details</h3>
<div class="row">
<div class="col-md-4">
<div class="form-group">
<label asp-for="UserName">User Name</label>
<input asp-for="UserName" class="form-control" />
<span asp-validation-for="UserName" class="text-danger"></span>
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<label asp-for="Password"></label>
<input asp-for="Password" class="form-control" />
<span asp-validation-for="Password" class="text-danger"></span>
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<label asp-for="ConfirmPassword"></label>
<input asp-for="ConfirmPassword" class="form-control" />
<span asp-validation-for="ConfirmPassword" class="text-danger"></span>
</div>
</div>
</div>
<button type="submit" class="btn btn-primary">Register</button>
</form>
</div>
</div>
<script src="~/jquery/jquery.slim.min.js"></script>
<script src="~/bootstrap/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>
解决方法
我想将该国家列表填充到我的下拉列表中
要满足您的要求,您可以参考以下代码段。
RegistrationModel类
public class RegistrationModel : PageModel
{
private IWebHostEnvironment _env;
public string Country { get; set; }
public List<SelectListItem> Countrydropdownlist { get; set; }
public RegistrationModel(IWebHostEnvironment env)
{
_env = env;
}
public void OnGet()
{
Countrydropdownlist = GetCountryItems();
}
public List<SelectListItem> GetCountryItems()
{
var filepath = Path.Combine(_env.ContentRootPath,"countrylist.json");
var jsonlist = System.IO.File.ReadAllText(filepath);
var result = JsonConvert.DeserializeObject<RootCountry>(jsonlist);
List<SelectListItem> _countrydropdownlist = new List<SelectListItem>();
foreach (var nation in result.Countries)
{
_countrydropdownlist.Add(new SelectListItem { Value = nation.Code.ToString(),Text = nation.Name });
}
return _countrydropdownlist;
}
}
在Registration.cshtml
<select asp-for="Country" asp-items="Model.Countrydropdownlist"></select>
countrylist.json文件存储如下
测试结果
注意 :有关内容根目录和Web根目录的更多信息,请检查以下文档
更新:
根据您的新更新,您似乎想在MVC项目中填充并显示下拉菜单,要实现此目标,请参考以下示例。
public class AccountController : Controller
{
private readonly IWebHostEnvironment _env;
public AccountController(IWebHostEnvironment env)
{
_env = env;
}
public IActionResult Index()
{
ViewBag.Countrydropdownlist = GetCountryItems();
return View();
}
//...
在Index.cshtml
文件夹下的Views/Account
文件中
@{
ViewData["Title"] = "Index";
}
<h1>Index</h1>
<select name="Country" asp-items="ViewBag.Countrydropdownlist"></select>
此外,您可以从此文档中进一步了解视图发现以及如何将数据传递给视图:
Update2:
似乎您通过viewmodel数据传递了项目并填充了<select>
,请确保您提供并传递了数据以从控制器操作中进行查看,如下所示。
//...
//your code logic here
var model = new Registration
{
CountryDropDownList = GetCountryItems()
};
return View(model);