Error in crud using wcf in mvc

Posted by Klbaiju under ASP.NET MVC on 11/1/2014 | Points: 10 | Views : 516 | Status : [Member] | Replies : 1
Hi,

I want to display records using wcf in mvc.

First i have created an MVC project,the add new project as wcfLibrary. and named as Wcfservice

this is my code for wcf.

[ServiceContract]
public interface IDbService1
{
[OperationContract]
List<MyAddress> GetAddress(string city);

// TODO: Add your service operations here
}
[DataContract]
public class MyAddress
{

[DataMember]
public string Address1 { get; set; }
[DataMember]
public string City { get; set; }


}

then i added entityDatamodel in wcf for connecting database .its name is AdventureWorks2008R2Entities

implemetaion in wcf

{
AdventureWorks2008R2Entities entities = new AdventureWorks2008R2Entities();
public List<MyAddress> GetAddress(string city)
{
//return entities.Addresses.Where(c => c.City == city);
var a = entities.Addresses.Where(c => c.City == city);
List<MyAddress> list = new List<MyAddress>();
foreach (var address in a)
{
list.Add(new MyAddress{Address1=address.AddressLine1,City=address.City});
}
return list;

}
}


then i buld .there is no error

then i added servicerefference of wcf in mvc project

code in Home controller.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcWcf.ServiceReference1;


namespace MvcWcf.Controllers
{
public class HomeController : Controller
{
public ActionResult ViewCity()
{
DbService1Client client = new DbService1Client();
return View(client.GetAddress("Bothell"));
}
}
}


then i added a view with empty modelclass and add this code in view

@using MvcWcf.ServiceReference1
@model List<WcfService.MyAddress>
@{
foreach(WcfService.MyAddress address in Model)
{
<li>@address.Address1 @address.City</li>
}
}


when i tries to run following error is showing

The model item passed into the dictionary is of type 'WcfService.MyAddress[]', but this dictionary requires a model item of type 'System.Collections.Generic.List`1[WcfService.MyAddress]'.

how to solve this.

Regards

Baiju

[ServiceContract]
public interface IDbService1
{
[OperationContract]
List<MyAddress> GetAddress(string city);

// TODO: Add your service operations here
}
[DataContract]
public class MyAddress
{

[DataMember]
public string Address1 { get; set; }
[DataMember]
public string City { get; set; }


}





Responses

Posted by: Vuyiswamb on: 11/2/2014 [Member] [MVP] [Administrator] NotApplicable | Points: 25

Up
0
Down
try to change

public List<MyAddress> GetAddress(string city)

to

public IList<MyAddress> GetAddress(string city)


Thank you for posting at Dotnetfunda
[Administrator]

Klbaiju, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response