通过WebService下拉列表定义问题

时间:2014-01-20 14:56:58

标签: c# asp.net web-services

我正在尝试通过WebServices填充我的Dropdownlist 我有一些错误。

  

无法隐式转换类型'System.Collections.Generic.List< Get_Car>到'System.Collections.Generic.List< Car>'。

CarServices.cs文件

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

public class CarService : ICarService
{
public List<Car> GetCars()

    {
        using (CarDataContext db = new CarDataContext())
        {
            return (from car in db.Cars
                select new Get_Car()
                {
                    Id = car.Id,
                    Car_name = car.Car_name
                }).ToList();              <---- got an error here
        }
    }
}

ICarService.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;


[ServiceContract]
public interface ICarService
{
[OperationContract]
List<Car> GetCars();
}

Car.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

public class Get_Car
{
private int id;
    private string car_name;

    public Get_Car()
{

    }

    public int Id { get { return this.id; } set { this.id = value; } }
    public string Car_name { get { return this.car_name; } set {this.car_name = value;}}

}

Apointement.aspx

<asp:SqlDataSource ID="sqlDataSource2" runat="server" 
    ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT * FROM 
    [Car]" TableName="Cars"> 
</asp:SqlDataSource>

<div id="template">
    <div class="text-center">
        <div class="titletext">
                 Apointement
        </div>
        <br />
        <asp:LinqDataSource ID="LinqDataSource2" runat="server"></asp:LinqDataSource>

        <asp:LinqDataSource ID="LinqDataSource1" runat="server"    
        ContextTypeName="ApointementDataContext" EnableDelete="True" EnableInsert="True" 
        EnableUpdate="True" EntityTypeName="" TableName="Apointement" 
             Where="IsAvailable = True && dateTrial >= DateTime.Now && 
                    carsTrial = @carsTrial"> 

            <WhereParameters>
               <asp:ControlParameter 
                   Name="carsTrial" 
                   ControlID="DropDownList1" 
                   PropertyName="SelectedValue"
                   DefaultValue="Ferrari458italia2011"
                   Type="String" />
             </WhereParameters>

        </asp:LinqDataSource>

        <div class="center">

            <asp:Label ID="lblCarTrial" runat="server" Text="Choose a car to try"> </asp:Label>

           <div class="value-right"> 
               <asp:DropDownList ID="DropDownList1" runat="server" Width="180px" 
                  AutoPostBack="true" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"
                  DataTextField="Car_name" DataSourceID ="Id" >


               </asp:DropDownList> 

Apointement.aspx.cs

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


public partial class Apointement : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {
        lblMessage.Text = "";

        DropDownList1.DataSource = GetCars();
        DropDownList1.DataBind();


    private void ListCars()
    {
        using (CarDataContext db = new CarDataContext())
        {
            var carItems = from car in db.Cars select car;

            DropDownList1.DataSource = carItems;
            DropDownList1.DataBind();
        };
    }

    private List<CarService.**Car**> GetCars()
    {
        CarService.**CarService** client = new CareService.**CarService**();

        CarService.**car[]** cars = client.GetCars();

       return cars.ToList();
 }                             *<=== all the fields in bold got errors.  
}

3 个答案:

答案 0 :(得分:1)

您将返回List<Get_Car>,其中您应该返回List<Car>,并且由于Get_Car似乎确实从Car继承,您不能这样做,这两种类型不一样,不可互换。

答案 1 :(得分:1)

您只需将您的类从Get.car重命名为Car.cs中的Car:

此:

public class Get_Car

对此:

public class Car

此外,您还需要重命名构造函数。

答案 2 :(得分:0)

问题是我无法进行更新Web /服务引用。有人建议从Visual Studio关闭所有内容并再次打开网站。然后我就能够执行更新Web /服务参考。

最终解决方案如下:

CarService.cs

public class CarService : ICarService
{
    public List<Car>  CareDetail()  
    {
        using (CarDataContext db = new CarDataContext())
        {
            return db.Cars.ToList();
}}}

ICarService.cs

[ServiceContract]
public interface ICarService
{
[OperationContract]
List<Car> CarDetail();
}

代码behind.aspx.cs

private List<CarServiceReference.Car> CarDetail()
{
    CarServiceReference.CarServiceClient client = new CarServiceReference.CareServiceClient();

    CarServiceReference.Car[] cars = client.CarDetail();

    return cars.ToList();

}