数据源是无效类型。它必须是ilistource,notienume或idatasource? [英] Data source is an invalid type. It must be either an ilistsource, ienumerable, or idatasource?

查看:59
本文介绍了数据源是无效类型。它必须是ilistource,notienume或idatasource?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的api代码与实体框架..

this is my api code with entity framework..

public class UpdateHotelInfoesController : ApiController
    {
        private UpdateHotelInfoEntities db = new UpdateHotelInfoEntities();

        // GET: api/UpdateHotelInfoes
        public IQueryable<updatehotelinfo> GetUpdateHotelInfo()
        {
            return db.UpdateHotelInfo;
        }

        // GET: api/UpdateHotelInfoes/5
        [ResponseType(typeof(UpdateHotelInfo))]
        public IHttpActionResult GetUpdateHotelInfo(int id)
        {
            UpdateHotelInfo updateHotelInfo = db.UpdateHotelInfo.Find(id);
            if (updateHotelInfo == null)
            {
                return NotFound();
            }

            return Ok(updateHotelInfo);
        }



这是源代码




this is source code

 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnSelectedIndexChanged="GridView1_SelectedIndexChanged1">
            <columns>
                <asp:BoundField DataField="HOTEL_NAME" HeaderText="HOTEL_NAME" />
                <asp:BoundField DataField="ADDRESS_LINE" HeaderText="ADDRESS_LINE" />
                <asp:BoundField DataField="AREA" HeaderText="AREA" />
                <asp:BoundField DataField="CITY" HeaderText="CITY" />
                <asp:BoundField DataField="POSTAL_CODE" HeaderText="POSTAL_CODE" />
                <asp:BoundField DataField="STATE" HeaderText="STATE" />
                <asp:BoundField DataField="COUNTRY" HeaderText="COUNTRY" />
                <asp:BoundField DataField="PHONE_NUMBER" HeaderText="PHONE_NUMBER" />
                <asp:BoundField DataField="LANDLINE" HeaderText="LANDLINE" />
                <asp:BoundField DataField="EMAIL_ID" HeaderText="EMAIL_ID" />
                <asp:BoundField DataField="WEBSITE" HeaderText="WEBSITE" />
                <asp:BoundField DataField="PROPERTY_TYPE" HeaderText="PROPERTY_TYPE" />
                <asp:BoundField DataField="PROPERTY_GRADE" HeaderText="PROPERTY_GRADE" />
                <asp:BoundField DataField="REGISTRATION" HeaderText="REGISTRATION" />
            
        

this is my c# code for displaying.it in grid view


protected async void Button4_Click(object sender, EventArgs e)
        {

          
            up.POSTAL_CODE = Convert.ToInt32(TextBox2.Text);
          
            HttpClient client = new HttpClient();
            client.BaseAddress = new Uri(Base_URL);
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            StringContent con = new StringContent(JsonConvert.SerializeObject(up), Encoding.UTF8, "application/json");



            HttpResponseMessage response = await client.GetAsync("api/UpdateHotelInfoes/" + TextBox2.Text);
            response.EnsureSuccessStatusCode();
            if (response.IsSuccessStatusCode)
            {            
                  var data = await response.Content.ReadAsStringAsync();
                

                    var a = JsonConvert.DeserializeObject<mainobject>(data);
                                   
                         GridView1.DataSource = a;>>>..here am getting exception.
                         GridView1.DataBind();
}
anyone knows how to get rid of this issue?

What I have tried:

i tried using the above code .but its giving the exception  <pre>Data source is an invalid type. It must be either an ilistsource, ienumerable, or idatasource?.
how to get rid of this?

推荐答案

您正在尝试将数据源绑定到单个类,但是数据源需要一组课程。如果要将数据源绑定到单个类,则需要创建一个只包含该类的集合



You are trying to bind the datasource to a single class, but a datasource needs a collection of classes. If you want to bind a datasource to a single class you need to make a collection that only has that class in it

GridView1.DataSource = new List<mainobject>{a};


这篇关于数据源是无效类型。它必须是ilistource,notienume或idatasource?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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