Home > Blockchain >  model property is not passed to controller action
model property is not passed to controller action

Time:10-11

What is my mistake? AllFindedBrands property is not passing to SearchBrandResult Action of Controller

Controller:

public async Task<IActionResult> Search(string Articul, int idClient)
    {
        List<BrandList> findedBrands = new List<BrandList>();

        
        @ViewBag.list = woDupes;
        SearchViewModel model = new SearchViewModel();
        model.Articul = Articul;
        model.idClient = idClient;
        model.AllFindedBrands =  new List<BrandList>(findedBrands);

        return View(model);
    }
 

    [HttpPost]
    public async Task<IActionResult> SearchBrandResult(SearchViewModel model)
    {
      
        return View();
    }

View:

<form asp-controller="Search" asp-action="SearchBrandResult" asp-route- 
   Articul="@Model.Articul"
  asp-route-AllFindedBrands="@Model.AllFindedBrands" asp-route- 
   idClient="@Model.idClient" method="post" enctype="multipart/form-data">

<select asp-for="SelectedBrand" asp-items="@(new SelectList(@ViewBag.list,
                                                nameof(FindedBrand.Name),
                                                nameof(FindedBrand.Name)))"
        multiple="true" >
</select>

<input type="submit" />

All other properties of ViewModel is successfully passed to th Action

CodePudding user response:

AllFindedBrands is type of complex model and asp-route-* cannot dynamic bind the value. You can F12 in the browser to check the generated url in the form.

Two ways you can follow:

1.By using asp-all-route-data and foreach the AllFindedBrands to bind the value which passes the value by route data.

Assume your model like below:

public class SearchViewModel
{
    public string Articul { get; set; }
    public string idClient { get; set; }
    public List<BrandList> AllFindedBrands { get; set; }
    public List<string> SelectedBrand { get; set; }
}
public class BrandList
{
    public string Name { get; set; }
}

View(For easy testing, I just hard-coded the dropdownlist):

@model SearchViewModel
@{
    var data = new Dictionary<string, string>();
    for(int i=0;i<Model.AllFindedBrands.Count();i  )
    {
        data.Add("AllFindedBrands["   i   "].Name", Model.AllFindedBrands[i].Name);
    }
 }

<form asp-action="SearchBrandResult" asp-route-Articul="@Model.Articul" asp-all-route-data="@data" asp-route-idClient="@Model.idClient" method="post" enctype="multipart/form-data">
    <select asp-for="SelectedBrand" multiple="true" >
        <option value="aaa">aaa</option>
        <option value="bbb">bbb</option>
        <option value="ccc">ccc</option>
    </select>
    <input type="submit" />
</form>

2.By listing the property and make them hidden inputs which passes the value by form data:

@model SearchViewModel

<form asp-action="SearchBrandResult" asp-route-Articul="@Model.Articul" asp-route-idClient="@Model.idClient" method="post" enctype="multipart/form-data">
    @for (int i = 0; i < Model.AllFindedBrands.Count(); i  )
    {
        <input asp-for="@Model.AllFindedBrands[i].Name" hidden />
    }
    <select asp-for="SelectedBrand" multiple="true" >
        <option value="aaa">aaa</option>
        <option value="bbb">bbb</option>
        <option value="ccc">ccc</option>
    </select>
    <input type="submit" />
</form>
  • Related