如何从View到控制器获取选定的下拉列表值,然后将其再次传递给视图

时间:2016-11-15 08:27:39

标签: c# asp.net asp.net-mvc

我有这个DropdownList读取枚举:

@Html.DropDownListFor(m => m.HoursScale,
new SelectList(Enum.GetValues(typeof(CalendarComponets.Models.Data.ScaleLine.Scales))), "Select Scale")

我需要在控制器中获取下拉列表的选定值的值,然后再将其传递给我的视图并显示如下:

@Html.DisplayFor(vm => vm.Scale)

我是asp.net mvc的新手,所以感谢任何帮助

2 个答案:

答案 0 :(得分:1)

您可以使用该模型获取所选项目。你需要这个来获得返回值。

[HttpPost]
public ActionResult YourPageName(ModelName model)
{
     ViewBag.Item = model.HoursScale;
     return RedirectToAction("PageName", "ControllerName");
}

在页面上,您要显示来自输入的值:

<p>@ViewBag.Item</p>

在这个例子中,我认为你已经在我的案例中将模型命名为ModelName:

public string HoursScale { get; set; } // Maybe other returning value than string?

答案 1 :(得分:0)

模型

public class YourViewModel
{
    public int SelectedId { get; set; }

    //other codes.....
}

查看

@Html.DropDownListFor(x => x.SelectedId , new SelectList(Model.YourBindField))

控制器

    [HttpPost]
    public ActionResult Create(YourViewModel viewModel)
    {
        ///....
        var selectedIndex = viewModel.SelectedId ;
        ///....            
    }