如何从的dropdownlist在控制器(MVC 2)选择的值 [英] How to get the selected value from dropdownlist in Controller (MVC 2)

查看:115
本文介绍了如何从的dropdownlist在控制器(MVC 2)选择的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我奋力摆脱MVC中硬codeD DropDownList中选定的值,下面是code:

I am struggling to get the selected value from the hard-coded dropdownlist in mvc , below is the code:

查看:

<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 DropDownList的选定值。

How can I get the Selected value of the above 2 dropdownlist in mvc post controller.

推荐答案

您已经叫你的下拉菜单头盔和车库,如果您调试行动,并期待在 formValues​​ 词典您应该看到这两个值。

You've called your dropdowns "helmets" and "garages" if you debug your action and look at the formValues dictionary your should see these two values.

另外,你可以改变你的模型有一个盔和int类型的车库的属性?和模型绑定应该填充这些值。

Alternatively you could change your model to have a "Helmets" and "Garages" property of type int? and the model binder should populate these values.

或者你可以你的行动改变这样的:

Or you could change your action to something like:

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

这应该与ID的(选择值)下拉列表中进行填充。

This should be populated with the id's (selected value) of the drop down list.

下面是我的code,无论是从收藏还是从传递的属性获得的值:

Here's my code that gets the values either from the collection or from the passed properties:

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();
}

这篇关于如何从的dropdownlist在控制器(MVC 2)选择的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆