在视图中创建DropDownList并将值传递给controller - MVC

时间:2016-09-29 03:06:57

标签: c# asp.net-mvc

如何在视图中创建值为1-10(int)的DropDownList并将所选值传递给控制器​​?

UserReview模型:

library(data.table)
setDT(df)[, (4:ncol(df)) := lapply(.SD, `*`, count), .SDcols = 4:ncol(df)]

控制器:

    public System.Guid Id { get; set; }
    public System.Guid UserId { get; set; }
    public System.Guid ReviewId { get; set; }
    public Nullable<int> Rating { get; set; }

1 个答案:

答案 0 :(得分:5)

1)第一种方法    在视图中

<select name="YourDropDownValue">
 @for(int i = 1 ; i <= 10 ; i++) 
     {
  <option value="@i">@i</option>
     }
</select>

尝试使用此代码,在名称属性中,您必须保留要在控制器中传递的字段名称

在控制器中,这是你如何绑定下拉值

   [HttpPost]
  public ActionResult getDropDown(int YourDropDownValue)
  {


  }

2)第二种方法 将其保存在控制器中

  List<int> Num = new List<int>() { 1,2,3,4,5,6,7,8,9,10 };
  ViewBag.Number = new SelectList(Num);

现在在视野中

      @Html.DropDownListFor(x => x.YourDropDownValue, ViewBag.Number as SelectList)

如果你没有模特

      @Html.DropDownList("YourDropDownValue", ViewBag.Number as SelectList)

现在在控制器

   [HttpPost]
  public ActionResult getDropDown(int YourDropDownValue)
  {


  }