如何通过razor view在Controller中获取相同类型的参数化值

How to get same type of parameterzied values in Controller through razor view

场景: 我有两个下拉菜单,只要我点击提交按钮,它就需要在构造函数中获取“UserTableId”的两个 int 值,

问题: 我无法理解应该如何编写它们才能从下拉菜单中获取值。

Create/View

 <div class="form-group  row">
  <label>User Id</label>
  <div class="col-md-10">
 @Html.DropDownList("UserTableId", null, htmlAttributes: new { @class = "btn  btn-primary" })
 @Html.ValidationMessageFor(model => model.UserTable.User_id, "", new { @class = "text-danger" })
                </div>
          </div>



<div class="form-group  row">
 <label>User Id</label> 
    <div class="col-md-10">
     @Html.DropDownList("UserTableId", null, htmlAttributes: new { @class = "btn  btn-primary"})
     @Html.ValidationMessageFor(model => model.UserTable.User_id, "", new { @class = "text-danger" })
         </div>
  </div>

控制器

public ActionResult ShowAllDetail(int UserTableId,int UserTableId)     //how should I pass the same parameter or whats the solution????
    {...............}

UserTable DbTable

由于您将问题标记为 MVC,您可以在视图中使用模型绑定:

@model SomePOCOModel

然后使用:

@Html.DropDownListFor(x => Model.UserTableId1, Model.UserTable),
                    "--Select User--",
                    new { @class = "btn  btn-primary" })

@Html.DropDownListFor(x => Model.UserTableId2, Model.UserTable),
                    "--Select User--",
                    new { @class = "btn  btn-primary" })

加载视图时传递模型:

public ActionResult Index()
{
   SomePOCOModel myModel = new SomePOCOModel();
   //Set the property UserTable to an IEnumerable<SelectListItem> 
   //built using your DB user table
   return View("Index", myModel);
}

你的视图模型:

public class SomePOCOModel()
{
    public int UserTableId1 {get; set;}
    public int UserTableId2 {get; set;}
    public IEnumerable<SelectListItem> UserTable {get; set;}
}

最后是你的控制器 post 方法,更改为接受模型类型 绑定到您的视图:

//Accept the model type bound to your View
[HttpPost]
[ValidateAntiForgeryToken] //Use this if placing an antiforgery token in your form
public ActionResult ShowAllDetail(SomePOCOModel myModel)
{
   //Do something with the drop down selections
   //myModel.UserTableId1
   //myModel.UserTableId2
}