use*_*007 5 javascript jquery c#-4.0 asp.net-mvc-3
尝试在客户端为js生成一个数组:
var ds = [{ text: "john", value: "1" }, { text: "paul", value: "2" }];
Run Code Online (Sandbox Code Playgroud)
在我的asp.net mvc 3控制器中,我创建了一个entityframework模型并尝试返回一个列表:
NORTHWNDEntities db = new NORTHWNDEntities();
public ActionResult GetCustomers()
{
return Json( db.Customers,JsonRequestBehavior.AllowGet);
}
Run Code Online (Sandbox Code Playgroud)
目前我无法弄清楚只是将customername + customerid属性作为客户列表(NWind数据库)返回?
试试这个 - 为每个客户创建一个具有所需属性的新匿名对象.
public ActionResult GetCustomers()
{
var customers = from o in db.Customers
select new { customerName = o.CustomerName, customerId = o.CustomerId};
return Json(customers.ToList(), JsonRequestBehavior.AllowGet);
}
Run Code Online (Sandbox Code Playgroud)
请注意,如果你想要ex.将"text"和"value"作为JSON数组中的值只需将上面的customerName和customerId更改为您想要的任何名称.
试试这个:
public ActionResult GetCustomers()
{
var customers = for c in db.Customers
select new { text = c.CustomerName, value = c.CustomerId};
return Json( customers.ToArray(), JsonRequestBehavior.AllowGet);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
13876 次 |
| 最近记录: |