如何在视图中创建值为1-10(int)的DropDownList并将所选值传递给控制器?
UserReview模型:
public System.Guid Id { get; set; }
public System.Guid UserId { get; set; }
public System.Guid ReviewId { get; set; }
public Nullable<int> Rating { get; set; }
Run Code Online (Sandbox Code Playgroud)
控制器:
public ActionResult Rating(Guid id) //got its ReviewId from previous page
{
//so far empty, no clue what to do
return Content("");
}
Run Code Online (Sandbox Code Playgroud)
1) 视图中的第一种方法
<select name="YourDropDownValue">
@for(int i = 1 ; i <= 10 ; i++)
{
<option value="@i">@i</option>
}
</select>
Run Code Online (Sandbox Code Playgroud)
尝试使用此代码,在名称属性中,您必须保留要在控制器中传递的字段名称
在控制器中,这是绑定下拉值的方式
[HttpPost]
public ActionResult getDropDown(int YourDropDownValue)
{
}
Run Code Online (Sandbox Code Playgroud)
2)第二种方法 将此保留在控制器中
List<int> Num = new List<int>() { 1,2,3,4,5,6,7,8,9,10 };
ViewBag.Number = new SelectList(Num);
Run Code Online (Sandbox Code Playgroud)
现在在视野中
@Html.DropDownListFor(x => x.YourDropDownValue, ViewBag.Number as SelectList)
Run Code Online (Sandbox Code Playgroud)
如果你没有模特
@Html.DropDownList("YourDropDownValue", ViewBag.Number as SelectList)
Run Code Online (Sandbox Code Playgroud)
现在在控制器中
[HttpPost]
public ActionResult getDropDown(int YourDropDownValue)
{
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1014 次 |
| 最近记录: |