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

本文记录了在ASP.NET MVC中使用ViewData传递数据到DropDownList时遇到的无法正确显示的问题。经过4小时的调试,发现原因是DropDownList的ID与ViewData中的关键字相同导致。解决方案是将两者命名不同,如将ID改为'Customer_id4PoItemWithRow',并确保ViewData中的数据类型正确。这次经验旨在提醒开发者注意此类问题,避免浪费调试时间。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

再次经历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);
        }




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值