Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
136 views
in Technique[技术] by (71.8m points)

c# - 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 UserTable

question from:https://stackoverflow.com/questions/65890669/how-to-get-same-type-of-parameterzied-values-in-controller-through-razor-view

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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
} 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...