Search code examples
c#asp.netrazorhtml.beginform

My Html-BeginForm is passing null values to my controller


I have this form in my view

@using (Html.BeginForm("SearchSubmit", "Search", FormMethod.Post))
{
    <fieldset>
        @Html.DropDownListFor(a => a.Item2.CategoryName, new SelectList(Model.Item1.Categories), "All Categories", new { @class = "makeHigh" })
        @Html.DropDownListFor(a => a.Item2.MarketCategoryName, new SelectList(Model.Item1.Markets), "All Markets", new { @class = "makeHigh" })
        <br />
        @Html.DropDownListFor(a => a.Item2.ColumnName, new List<SelectListItem> {

                       new SelectListItem { Value = "0" , Text = "Item Title" },
                       new SelectListItem { Value = "9" , Text = "Search All" },
                       new SelectListItem { Value = "1" , Text = "Description" },
                       new SelectListItem { Value = "2" , Text = "Upc" },
                       new SelectListItem { Value = "3" , Text = "Sku" },
                       new SelectListItem { Value = "4" , Text = "Brand" },
                       new SelectListItem { Value = "5" , Text = "Size" },
                       new SelectListItem { Value = "6" , Text = "Location" },
                       new SelectListItem { Value = "7" , Text = "Price" },

                    }, new { @class = "makeHigh" })

        @Html.DropDownListFor(a => a.Item2.ValueExpression, new List<SelectListItem> {
                       new SelectListItem { Value = "0" , Text = "Contains" },
                       new SelectListItem { Value = "1" , Text = "Equals Exactly" },
                       new SelectListItem { Value = "2" , Text = "Contains Partial" },
                    }, new { @class = "makeHigh" })
        @Html.TextBoxFor(a => a.Item2.SearchQuery, new { @class = "makeWide" })<br />

        <button class="btn-dark btn-sm btn-info" type="submit">Search</button> 
    </fieldset>
}

and when I hit the submit button, it passes null values to my controller action

   [HttpPost]
    public IActionResult SearchSubmit(SearchFormModel search) {

       var results =  _assets.searchInventoryAssets(search.SearchQuery, search.CategoryName, search.MarketCategoryName, search.ColumnName, search.ValueExpression);
       return RedirectToAction("Index", new { assets = results });
    }

I'm not sure why the data from the form isn't being sent

The Item2 is a model @model Tuple;

where searchformmodel looks like this

    public string SearchQuery { get; set; }
    public string CategoryName { get; set; }
    public string MarketCategoryName { get; set;  }
    public string ColumnName { get; set; }
    public string ValueExpression { get; set; }

Solution

  • Your viewmodel should be SearchFormModel, not Tuple, or change your Controller Action to expect a Tuple.