如何在有或没有Json的控制器中获取selectlist的值

时间:2011-08-19 13:16:56

标签: json asp.net-mvc-3 selectlist

我可以通过这种方式获取下拉列表的值,但是我无法使用此代码获取选择列表项的值。我可以做些什么来将值传入我的控制器以进行创建操作。

我的控制器包含:

    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Create(Product products, Design designs, Material materials, Color colors, Picture pictures, FormCollection form,EventArgs e)
    {
        if (Request.Files != null)
        {

            long prod = Convert.ToInt64(form["Product"]);
            pictures.product_id = db.Products.Single(x => x.id == prod).id;

我的观点包含:

   @Html.DropDownList("Product", new SelectList((System.Collections.IEnumerable)ViewData["Productlist"], "id", "name"), "Please Select Product", new { onchange = "productlist()", style = "width:190px; padding:4px; margin:4px;" })

我可以获得下拉列表值,但无法获得selectlist的值..

我的视图包含:(选择列表)

   <select id="Color" style=" width:190px; padding:4px; margin:4px;" onchange="colorlist()">
   <option label="Please Select Color" ></option>
   </select>

所以,如果我需要使用json,我如何在创建动作和视图中使用它。

1 个答案:

答案 0 :(得分:0)

如果要使用默认绑定,则需要在名为“Product”的Create操作中使用您传递的任何类型的参数(即字符串)。然后当表单POST到操作时,活页夹将该参数值设置为POST时选择的选项。