我的Html-BeginForm将空值传递给我的控制器

时间:2019-08-31 20:56:20

标签: c# asp.net razor html.beginform

我认为此表单是

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

当我按下“提交”按钮时,它将空值传递给我的控制器操作

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

我不确定为什么表单中的数据没有被发送

Item2是模型@model Tuple;

其中的searchformmodel看起来像这样

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

1 个答案:

答案 0 :(得分:0)

您的viewmodel应该是SearchFormModel,而不是Tuple, 或更改您的Controller Action来期望Tuple