如何解决如何通过剃刀视图在控制器中获取相同类型的参数化值
场景: 我有两个下拉菜单,只要我点击提交按钮,它就需要在构造函数中获取两个“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",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????
{...............}
解决方法
由于您将问题标记为 MVC,您可以在视图中使用模型绑定:
@model SomePOCOModel
然后使用:
@Html.DropDownListFor(x => Model.UserTableId1,Model.UserTable),"--Select User--",new { @class = "btn btn-primary" })
@Html.DropDownListFor(x => Model.UserTableId2,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
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。