MVC3 使用动态生成的DropDownList,更新partial view

本文介绍了一种使用ASP.NET MVC实现动态下拉列表(DropDownList)的方法,并通过Ajax动态更新部分视图(Partial View),展示了如何根据用户选择加载相应的产品列表。

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

Demo简述:使用动态生成的DropDownlist,动态更新partial view

Control 动态生成 DropdownList 的方法



public ActionResult Index() { var products = (from product in context.GetTable<Product>() select product).ToList(); var categorys = (from category in context.GetTable<ProductCategory>() select new SelectListItem { Text = category.Title, Value = category.Id.ToString() }).ToList(); List<SelectListItem> listItem = new List<SelectListItem>(); listItem.Add(new SelectListItem { Text = "Choose an option"}); listItem.AddRange(categorys); ViewData["products"] = products; ViewData["categorys"] = listItem; return View(); } public ActionResult GetProductById(string id) { var products = from product in context.GetTable<Product>() select product; if (!string.IsNullOrEmpty(id) && id != "All") { products = products.Where(p => p.ParentId.ToString() == id); } return PartialView("ProductControl1", products.ToList()); }Index View:

<div id="loading" style="display:none;color:Red;font-weight:bolder"> Loading Data..... </div> <fieldset> <span>Choose different Product</span> <div> @using(Ajax.BeginForm ("GetProductById", new AjaxOptions { UpdateTargetId = "productList", Confirm = "Do you submit the request?", LoadingElementId = "loading", LoadingElementDuration = 2000 } ) ) { @Html.DropDownListFor(model => Model.Id,ViewData["categorys"] as List<SelectListItem>) <input type="submit" value="Submit" /> } </div> </fieldset> <div id="productList"> @{Html.RenderPartial("ProductControl1",ViewData["products"]);} </div>PartialView

@model IEnumerable<MvcApp.Product> <table> <tr> <td>Title</td> <td>Price</td> <td>CreateTime</td> </tr> @foreach(var p in Model) { <tr> <td>@p.Title</td> <td>@p.Price</td> <td>@p.CreateTime</td> </tr> } </table>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值