Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/tfs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 如何从控制器(MVC 2)中的dropdownlist中获取所选值_Asp.net Mvc_Asp.net Mvc 2_Drop Down Menu - Fatal编程技术网

Asp.net mvc 如何从控制器(MVC 2)中的dropdownlist中获取所选值

Asp.net mvc 如何从控制器(MVC 2)中的dropdownlist中获取所选值,asp.net-mvc,asp.net-mvc-2,drop-down-menu,Asp.net Mvc,Asp.net Mvc 2,Drop Down Menu,我正在努力从mvc中硬编码的下拉列表中获取所选值,下面是代码: 视图: 头盔数量 车房数目 控制器: public ActionResult Index() { var helmets = Enumerable.Range(1, 200).Select(x => new SelectListItem { Value = x.ToString(), Text = x.ToString() }); ViewData["helmets"] = new SelectList(he

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

视图:

头盔数量
车房数目
控制器:

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();
}
//头盔下拉列表

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


    }`
[HttpPost]
公共操作结果创建(事件跟踪日、FormCollection formValues)
{Product Product=新产品()//
ViewBag.mode=“创建”;
//用于下拉式轨道
ITrackRepository trackResp=新的TrackRepository();
IQueryable tracks=trackResp.getVenueSelectList();
ViewData[“场馆”]=新的选择列表(曲目,“VenueID”、“名称”);
var helmets=Enumerable.Range(1200).Select(x=>newselectListItem{Value=x.ToString(),Text=x.ToString()});
ViewData[“helmets”]=新的选择列表(helmets.ToList(),“Value”,“Text”);
//车库下拉菜单
var garages=Enumerable.Range(1,50).Select(x=>newselectListItem{Value=x.ToString(),Text=x.ToString()});
ViewData[“garages”]=新建选择列表(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(产品);
trackdayResp.Add(trackday);
trackday.Products.Add(产品);
trackdayrep.Save();
返回操作(“索引”);
}
其他的
{
返回视图();
}
}`

如何在mvc post controller中获取上述两个下拉列表的选定值。

如果调试操作并查看
formValues
字典,您应该会看到这两个值,那么您已经将下拉列表称为“头盔”和“车库”

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

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

public ActionResult Create(Event trackday, int? helmets, int? garages, FormCollection formValues)
应使用下拉列表的id(选定值)填充此字段

更新 下面是我从集合或传递的属性中获取值的代码:

HTML:


你能告诉我们到目前为止你的post操作有什么吗?我不能将其更改为int,因为它是一个集合。它的以下内容:null:(表格集合是否包含头盔和车库?你能用你的完整视图更新你的问题吗?我已经用代码更新了我的答案,显示了它是如何为我工作的。它工作得很好。需要你的帮助,还有一件事要知道的是,helemt链接到产品类型1,而车库链接到我数据库中的产品类型2,所以我是如果客户机选择头盔,则应将producttype 1添加到db,反之亦然。。
<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();
}