必须先绑定数据源,然后才能执行此操作。 Web网格错误 [英] A data source must be bound before this operation can be performed. Web grid Error

查看:68
本文介绍了必须先绑定数据源,然后才能执行此操作。 Web网格错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

我在运行我的代码时遇到错误。我在mvc3中有点新请帮助我



index.cshtml



 @ model IEnumerable< irasoftwares.models.person> 
@ {
ViewBag.Title = 主页;
WebGrid grid = new WebGrid();
}





< h2>人员名单< /   h2  >  
@ grid.GetHtml(列: new [] {
grid.Column( ID),
grid.Column( 名称 ),
grid.Column( 姓氏
})







型号代码



 命名空间 IRASoftwares.Models 
{
public class Person
{
public int id { get ; set ; }
public string 名称{ get ; set ; }
public string 姓氏{ get ; set ; }
}
}









和控制器



  public  ActionResult Index()
{
Person loperson = new Person();
列表< person> lapeaople = new List< person>();

loperson.id = 101 ;
loperson.Name = Lakhan;
loperson.Surname = Aanjana;
lapeaople.Add(loperson);

loperson = new Person();
loperson.id = 102 ;
loperson.Name = Manish;
loperson.Surname = Aanjana;
lapeaople.Add(loperson);

loperson = new Person();
loperson.id = 103 ;
loperson.Name = Mahesh;
loperson.Surname = Patel;
lapeaople.Add(loperson);

loperson = new Person();
loperson.id = 104 ;
loperson.Name = Nittile;
loperson.Surname = Gupta;
lapeaople.Add(loperson);

loperson = new Person();
loperson.id = 105 ;
loperson.Name = Harish;
loperson.Surname = Verma;
lapeaople.Add(loperson);

loperson = new Person();
loperson.id = 106 ;
loperson.Name = Lokesh;
loperson.Surname = Mor;
lapeaople.Add(loperson);
var data = lapeaople;

return 查看(数据);

}





但我收到错误请帮帮我

解决方案

它为您提供错误,因为您没有将模型分配给网格,请检查以下链接:



http://weblogs.asp.net/scottgu/archive/2010/07/02/introducing-razor .aspx [ ^ ]

如何使用razor在asp.net mvc 4中使用网格引擎 [ ^ ]

你有返回Ienumarable< loperson>



然后它工作正常。



和你的v iew

@model Ienumerable< irasoftwares.models.person>



如果不能恢复你的冥想。


Hello,
I am Getting error while run my code.i am bit new in mvc3 please help me

index.cshtml

@model IEnumerable<irasoftwares.models.person>
@{
    ViewBag.Title = "Home Page";
    WebGrid grid = new WebGrid();
}



<h2>List Of Person</h2>
@grid.GetHtml(columns:new[]{
grid.Column("ID"),
grid.Column("Name"),
grid.Column("Last Name")
})




model code

namespace IRASoftwares.Models
{
    public class Person
    {
        public int id { get; set; }
        public string Name { get; set; }
        public string Surname { get; set; }
    }
}





and controller

public ActionResult Index()
       {
           Person loperson = new Person();
           List<person> lapeaople = new List<person>();

           loperson.id = 101;
           loperson.Name = "Lakhan";
           loperson.Surname = "Aanjana";
           lapeaople.Add(loperson);

           loperson = new Person();
           loperson.id = 102;
           loperson.Name = "Manish";
           loperson.Surname = "Aanjana";
           lapeaople.Add(loperson);

           loperson = new Person();
           loperson.id = 103;
           loperson.Name = "Mahesh";
           loperson.Surname = "Patel";
           lapeaople.Add(loperson);

           loperson = new Person();
           loperson.id = 104;
           loperson.Name = "Nittile";
           loperson.Surname = "Gupta";
           lapeaople.Add(loperson);

           loperson = new Person();
           loperson.id = 105;
           loperson.Name = "Harish";
           loperson.Surname = "Verma";
           lapeaople.Add(loperson);

           loperson = new Person();
           loperson.id = 106;
           loperson.Name = "Lokesh";
           loperson.Surname = "Mor";
           lapeaople.Add(loperson);
           var data = lapeaople;

           return View(data );

       }



but i am getting error please help me

解决方案

It gives you error as you didn't assign model to grid, check these links:

http://weblogs.asp.net/scottgu/archive/2010/07/02/introducing-razor.aspx[^]
How to use grids in asp.net mvc 4 with razor engine[^]


you have return Ienumarable<loperson>

then it works fine.

and in your view
@model Ienumerable<irasoftwares.models.person>

if not working revert your coment.


这篇关于必须先绑定数据源,然后才能执行此操作。 Web网格错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆