Asp.net MVC中传递ViewData数据不能正确传递的问题

再次经历MVC中两个DropDownList中一个通过ViewData或ViewBag传递数据自动选择出现问题的情况,也就是数据没有被正确传递,debug 了4 hours,妈的,怎么调试怎么没有错误,把数据通过json显示也没有错,但是就是在partialView中就是不能够显示正确,再一次陷入了漩涡,妈的。

忽然间想起来前面出现过类似的问题,当时把DropDownList的ID值和绑定的ViewData中的关键字修改成不同就可以了,因此就做了如下修改,果然ok,无语了,再次做了记录,希望能够对我和对大家加强印象。

例如:

Html.DropDownList("Customer_id4PoItemWithRow", ViewData["skuselect"] as IEnumerable<SelectListItem>, "--Select One--"),我们要让

Customer_id4PoItemWithRow和skuselect不同,否则的话会在很大程度上造成该DropDownList的值显示出现问题,比如本来应该自动选择给定的内容,并没有被自动显示选择,fuck。

 <div id="CollectionInRow">
            @Html.DropDownList("Collections",ViewData["CollectionInRow"] as IEnumerable<SelectListItem>, "--Select One--")
            @Html.ValidationMessage("Collections", "", new { @class = "text-danger" })
            
</div>
<div id="SKUDiv">
           @Html.DropDownList("Customer_id4PoItemWithRow", ViewData["skuselect"] as IEnumerable<SelectListItem>, "--Select One--")
           @Html.ValidationMessage("Customer_id4PoItemWithRow", "", new { @class = "text-danger" })
            
        
</div>



public ActionResult ShowProduct4RowByProductID(Product  model)
        {

            string productId = Request.QueryString["Product_id"];
            
            int productID = Int32.Parse(productId);
            model = db.Products.Find(productID);
            string customerType_ID = Request.QueryString["CustomerType_id"];
            int cID = Int32.Parse(customerType_ID);
            List<SelectListItem> item = new List<SelectListItem>();
            int collectionID = -1;
            foreach (var it in db.Products.Where(x => x.Customer_id == cID))
            {
                Sku sku=db.Skus.Find(it.SKU);
                string skuName = sku.name;
                //collectionID = db.Skus.Find(it.SKU).collectionID;
                if (sku.ID==productID)
                    collectionID = sku.collectionID;
                item.Add(new SelectListItem { Text = skuName, Value = sku.ID.ToString(), Selected = (it.ID == productID) });

            }
            List<SelectListItem> tmpCol = new List<SelectListItem>();
            foreach (var it in db.Collections)
            {
                tmpCol.Add(new SelectListItem { Text = it.Name, Value = it.ID.ToString(), Selected = ((collectionID == it.ID)) });
            }

            ViewData["CollectionInRow"] = tmpCol;
            ViewData["skuselect"] = item;

            //return Json(ViewData["CollectionInRow"], JsonRequestBehavior.AllowGet);
            return PartialView("_product4rowByid",model);
        }




没有更多推荐了,返回首页