c# - ASP.NET CORE MVC ViewModel 属性在发布时返回 Null
问题描述
在视图模型类中,我有一个属性
public List<SelectListItem> Genres { get; set; }
这是从数据库中获取的。但是当表单提交时,该属性为空。
这是Get
动作
[HttpGet]
[Route("PostAd")]
public async Task<IActionResult> PostAd()
{
var model = new PostAdViewModel();
var Genres = new List<SelectListItem>();
var dbGenres = _appDbContext.Genres.ToList();
var SelectListGroups = dbGenres.Where(x => x.ParentId == 0)
.ToDictionary(y => y.Id,y=>new SelectListGroup(){Name = y.Name});
foreach (var genre in dbGenres)
{
if (genre.ParentId != 0)
{
Genres.Add(new SelectListItem()
{
Value = genre.Id.ToString(),
Text = genre.Name,
Group = SelectListGroups[genre.ParentId]
});
}
}
model.Genres = Genres;
return View(model);
}
这是post方法
[HttpPost]
[Route("PostAd")]
public async Task<IActionResult> PostAd(PostAdViewModel model)
{
if (ModelState.IsValid)
{
return null;
}
else
{
return View(model);
}
}
这就是观点
<form class="form-horizontal" asp-controller="Ads" asp-action="PostAd" enctype="multipart/form-data">
<div asp-validation-summary="All" class="text-danger"></div>
<div class="form-group row">
<label class="col-sm-3 col-form-label">Janr</label>
<div class="col-sm-8">
@* @Html.DropDownListFor(model => Model.Genres, Model.Genres, "Janr", new { @class = "form-control",@id = "lstGenres", @multiple = "multiple", @title= "Janr Seçin" }) *@
@Html.DropDownListFor(model => model.SelectedGenres,Model.Genres,new { @class = "form-control",@id = "lstGenres", @multiple = "multiple", @title= "Janr Seçin",@name = "SelectGenre" })
</div>
</div>
<div class="form-group row">
<label for="" class="col-sm-3 col-form-label">Add Type</label>
<div class="col-sm-8"><button type="submit" id="button1id"
class="btn btn-success btn-lg" asp-action="PostAd" asp-controller="Ads">Submit</button></div>
</div>
</form>
所以每次提交这个表单时,“流派”集合返回 null,我该如何解决这个问题?谢谢
解决方案
该属性也需要在 Post 上重新填充
创建一个通用函数来获取数据
private List<SelectListItem> getGenres () {
var Genres = new List<SelectListItem>();
var dbGenres = _appDbContext.Genres.ToList();
var SelectListGroups = dbGenres.Where(x => x.ParentId == 0)
.ToDictionary(y => y.Id,y=> new SelectListGroup(){ Name = y.Name });
foreach (var genre in dbGenres) {
if (genre.ParentId != 0) {
Genres.Add(new SelectListItem() {
Value = genre.Id.ToString(),
Text = genre.Name,
Group = SelectListGroups[genre.ParentId]
});
}
}
return Genres;
}
并在控制器动作中调用它。
[HttpGet]
[Route("PostAd")]
public IActionResult PostAd() {
var model = new PostAdViewModel();
model.Genres = getGenres();
return View(model);
}
[HttpPost]
[Route("PostAd")]
public IActionResult PostAd(PostAdViewModel model) {
if (ModelState.IsValid) {
//...do something with model
//and redirect if needed
}
//if we reach this far model is invalid
//and needs to be repopulated
model.Genres = getGenres();
return View(model);
}
推荐阅读
- git - 变基如何在非快进合并中模拟快进?
- python - 如何在烧瓶服务器上检查现有用户的 sql 数据库
- sql - 在 presto sql 中创建 bin - 以编程方式
- excel - VBA宏,按字母顺序对列进行排序,后面有行,如何?
- typescript - 打字稿、复杂对象和联合类型
- reactjs - React 仅针对具有省略号的文本显示 Material-UI 工具提示
- directus - 无法在 Directus 中访问用户令牌
- angular - 如何比较和访问 Angular 8+ HTML 模板中的枚举变量?
- php - wordpress url中的非英文字符导致404错误
- gradle - 将 Gradle 从 Groovy 转换为 Kotlin DSL(用于 liquibase-gradle-plugin)