简体   繁体   English

ASP.NET CORE MVC ViewModel 属性在发布时返回 Null

[英]ASP.NET CORE MVC ViewModel property Return Null On Post

Inside the view model class I have a property在视图模型类中,我有一个属性

public List<SelectListItem> Genres { get; set; }

which is fetched from the database.这是从数据库中获取的。 But when the form is submitted, this property is null.但是当表单提交时,这个属性为空。

This is the Get action这是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);
}

and this is the post method这是post方法

[HttpPost]
[Route("PostAd")]
public async Task<IActionResult> PostAd(PostAdViewModel model)
{
        if (ModelState.IsValid)
        {
            return null;
        }
        else
        {
            return View(model);
        }
}

And this is the view这是视图

<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>

so every time this form is submit, "Genres" collection return null, how can i fix this?所以每次提交这个表单时,“流派”集合返回空,我该如何解决这个问题? thanks谢谢

The property will need to be repopulated on Post as well该属性也需要在 Post 上重新填充

Create a common function to get the data创建一个通用函数来获取数据

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;
}

And call it in the controller actions.并在控制器动作中调用它。

[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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM