简体   繁体   中英

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

SCENARIO: I have two drop-downs, As soon as I click the submit button, It needs to get two int value of "UserTableId" in the constructor,

Problem: I am unable to understand how should I write them so that I can get values from the drop-downs.

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>

Controller

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

UserTable DbTable用户表

Since you marked your question as MVC , you could be using Model binding in your View with:

@model SomePOCOModel

And then use:

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

When loading your View pass the 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);
}

And your View Model:

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

And finally your controller post method, change to accept the model type bound to your View:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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