【问题标题】:How to get a selected value of dropdownlist from View to controller then pass it over again to the view如何从视图中获取下拉列表的选定值到控制器,然后将其再次传递给视图
【发布时间】:2016-11-15 17:24:39
【问题描述】:

我有这个 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 的新手,所以感谢任何帮助

【问题讨论】:

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


    【解决方案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?
    

    【讨论】:

      【解决方案2】:

      型号

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-05-12
        • 1970-01-01
        相关资源
        最近更新 更多