2

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

[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

[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

1 Answer 1

3

The property will need to be repopulated on Post as well

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);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.