当前位置:编程学习 > C#/ASP.NET >>

asp.net mvc 如何通过ajax给htmlhelper的dropdownlist重新赋值

asp.net mvc 如何通过ajax给htmlhelper的dropdownlist重新赋值 --------------------编程问答-------------------- 在对应的控制器中写上 ,下面代码是使用LINQ TO SQL来从Northwind数据库中提取种类,
public ActionResult Index() 

  var db = new NorthwindDataContext(); 
  IEnumerable<SelectListItem> items = db.Categories 
    .Select(c => new SelectListItem 
                   { 
                     Value = c.CategoryID.ToString(),  
                     Text = c.CategoryName 
                   }); 
  ViewData["CategoryID"] = items; 
  return View(); 

在对应的视图中
注意每一个SelectListItem对象都必须给Value和Text属性进行赋值。他们会在运行时分别匹配到html的<option>的value属性和<option></option>之间的内容。注意这里ViewData的key用“CategoryID”显得有点奇怪,但实际上CategoryID正式<select>向服务器提交的值,所以使用这样的命名是有实际意义的。在View中,使用重载方法:

<%= Html.DropDownList("CategoryID") %> 


补充:.NET技术 ,  .NET技术前瞻
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,