【发布时间】:2020-02-08 00:27:18
【问题描述】:
我有一个过滤面板的部分视图,例如:
@model TabNetMVC.Models.FixedAsset.BarcodeTrackingModel
@using (Html.BeginForm("ApprovalList", "BarcodeTracking", FormMethod.Post))
{
@Html.DropDownListFor(model => model.RestaurantNo, Model.Restaurants, new { @class = "form-control", id = "restDropDown" })
<input onclick="" type="submit" name="name" value="Apply Filter" />
}
在这里,我收到了一个类型为 BarcodeTrackingModel 的模型,然后通过 Model.Restaurants 将餐厅名称填充到我的下拉列表中。 Model.Restaurants 是 SelectListItem 类型对象的列表。我希望用户选择一家餐厅,然后此选择的值将被覆盖在现有模型的 RestaurantNo 属性上。为此,我将一个表达式作为第一个参数传递给我的DropDownListFor 方法,如下所示; model => model.RestaurantNo。我的问题是,每当我单击“应用过滤器”按钮并提交表单时,模型都会传递给 Action 方法,ApprovalList 的所有属性都为空,除了RestaurantNo 属性。因此,只有下拉列表中的选定值被传递给 action 方法,而现有模型上的另一个数据会丢失。换句话说,表单发布的模型与其使用的现有 Model 不同。下面是动作方法;
[HttpPost]
public ActionResult ApprovalList(BarcodeTrackingModel model)
{
model.ProductList = model.ProductPagedList.Where(p => p.RestaurantNo == model.RestaurantNo).ToList();
return View("~/Views/BarcodeTracking/List.cshtml", model);
}
什么可能导致这个问题?提前致谢。
【问题讨论】:
-
在您的示例中,提交将仅发布在您的表单的 {} 之间的 RestaurantNo,如果您有任何其他属性并且您不想显示它,请使用隐藏字段,否则您可以使用 Jquery发布此属性
-
@Amine,我想发回整个同一个模型,只有 RestaurantNo 字段填写。
-
如果您想发布 RestaurantNo,您所做的应该可以,但是如果您想发布所有模型(许多文件),您应该对所有属性使用隐藏字段或使用 TempData["Model"]=你的模型。
标签: c# asp.net-mvc razor