繁体   English   中英

如何在 Controller 到 razor 视图中获取相同类型的参数化值

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

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

问题:我无法理解我应该如何编写它们以便我可以从下拉列表中获取值。

创建/查看

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

Controller

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

用户表数据库表用户表

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

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

加载视图时,通过 Model:

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

以及您的视图 Model:

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

最后,您的 controller 发布方法,更改为接受绑定到您的视图的 model 类型:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM