【问题标题】:How to get the selected value from dropdownlist in Controller (MVC 2)如何从控制器(MVC 2)的下拉列表中获取选定的值
【发布时间】:2011-06-15 10:23:38
【问题描述】:

我正在努力从 mvc 的硬编码下拉列表中获取选定的值,下面是代码:

查看:

<tr><td>No of Helmets</td><td><div class="editor-field">

                <%: Html.DropDownList("helmets", (SelectList)ViewData["size"], "--select--")%>
                </div></td></tr>

                 <tr><td>No of Garages</td><td><div class="editor-field">
                 <%: Html.DropDownList("garages", (SelectList)ViewData["garages"], "--select--")%>

控制器:

// 头盔下拉菜单

 [HttpPost]
    public ActionResult Create(Event trackday, FormCollection formValues)
    {Product product = new Product();//
        ViewBag.mode = "create";

        // for dropdown track
        ITrackRepository trackResp = new TrackRepository();
        IQueryable<Object> tracks = trackResp.GetVenuesSelectlist();
        ViewData["Venue"] = new SelectList(tracks, "VenueID", "Name");
       var helmets = Enumerable.Range(1, 200).Select(x => new SelectListItem { Value = x.ToString(), Text = x.ToString() });
        ViewData["helmets"] = new SelectList(helmets.ToList(), "Value", "Text");

        // dropdown for garages
        var garages = Enumerable.Range(1, 50).Select(x => new SelectListItem { Value = x.ToString(), Text = x.ToString() });
        ViewData["garages"] = new SelectList(garages.ToList(), "Value", "Text");  product.QtyAvailable = Convert.ToInt32(formValues["garages"]);


        if (ModelState.IsValid)
        {
            trackday.DateAdded = DateTime.Now;
            trackday.DateModified = DateTime.Now;
          //  productResp.Save();//
         //  trackday.Products.Add(product);
            trackdayResp.Add(trackday);
            trackday.Products.Add(product);
            trackdayResp.Save();
            return RedirectToAction("Index");
        }
        else
        {
            return View();
        }


    }`

如何在 mvc 后控制器中获取上述 2 个下拉列表的 Selected 值。

【问题讨论】:

  • 您能向我们展示您到目前为止的帖子操作吗?

标签: asp.net-mvc asp.net-mvc-2 drop-down-menu


【解决方案1】:

如果您调试您的操作并查看 formValues 字典,您应该会看到这两个值。

或者,您可以将模型更改为具有 int 类型的“头盔”和“车库”属性?并且模型绑定器应该填充这些值。

或者您可以将您的操作更改为:

public ActionResult Create(Event trackday, int? helmets, int? garages, FormCollection formValues)

这应该填充下拉列表的 id(选定值)。

更新

这是我从集合或传递的属性中获取值的代码:

HTML:

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
  <h2><%: ViewData["helmetsCollectionValue"]%></h2>
  <h2><%: ViewData["helmetsProperty"]%></h2>
  <h2><%: ViewData["garagesCollectionValue"]%></h2>
  <h2><%: ViewData["garagesProperty"]%></h2>
  <% using (Html.BeginForm()) { %>
  <p>
    <%: Html.DropDownList("helmets", (SelectList)ViewData["size"], "--select--")%>
  </p>
  <p>
    <%: Html.DropDownList("garages", (SelectList)ViewData["garages"], "--select--")%>
  </p>
  <p>
    <input type="submit" value="Submit" />
  </p>
  <% } %>
</asp:Content>

控制器:

public ActionResult Index()
{
    var helmets = Enumerable.Range(1, 200).Select(x => new SelectListItem { Value = x.ToString(), Text = x.ToString() });
    ViewData["helmets"] = new SelectList(helmets.ToList(), "Value", "Text");

    // dropdown for garages
    var garages = Enumerable.Range(1, 50).Select(x => new SelectListItem { Value = x.ToString(), Text = x.ToString() });
    ViewData["garages"] = new SelectList(garages.ToList(), "Value", "Text");

    return View();
}

[HttpPost]
public ActionResult Index(FormCollection collection, int? helmets, int? garages)
{
    ViewData["helmetsCollectionValue"] = collection["helmets"];
    ViewData["helmetsProperty"] = helmets;
    ViewData["garagesCollectionValue"] = collection["garages"];
    ViewData["garagesProperty"] = garages;

    var helmetsList = Enumerable.Range(1, 200).Select(x => new SelectListItem { Value = x.ToString(), Text = x.ToString() });
    ViewData["helmets"] = new SelectList(helmetsList.ToList(), "Value", "Text");

    // dropdown for garages
    var garagesList = Enumerable.Range(1, 50).Select(x => new SelectListItem { Value = x.ToString(), Text = x.ToString() });
    ViewData["garages"] = new SelectList(garagesList.ToList(), "Value", "Text");

    return View();
}

【讨论】:

  • 我不能把它改成 int 因为它是一个集合
  • 它的结果:null :(
  • 表单集合是否包含头盔和车库?你能用你的完整观点更新你的问题吗?
  • 我已经用代码更新了我的答案,显示了它对我的工作原理。
  • 它可以很好地为您提供帮助,还有一件要知道的事情是,头盔链接到产品类型 1,而车库链接到我的数据库中的产品类型 2,所以我正在寻找客户是否选择头盔,它应该将 producttype 1 添加到 db 中,反之亦然..
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-02-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-02
  • 1970-01-01
相关资源
最近更新 更多