[RESOLVED]Error in crud using wcf in mvc

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. the code i got from a tutorial i followed correctly

Regards

Baiju

replace

klbaiju

@model List<WcfService.MyAddress>

with

@model WcfService.MyAddress[]

WCF service will return array of objects not List<T> . You have to convert it to
List<T> before passing to view by calling ToList():

return View(client.GetAddress("Bothell").ToList());

Hi Friend,

Thanks for your reply.

i have changed the code in controller as

public ActionResult ViewCity()
        {
            
           DbService1Client client = new DbService1Client();
           
            return View(client.GetAddress("Bothell").ToList());

        }

and change the code in view as

@using MvcWcf.ServiceReference1
@model IEnumerable<WcfService.MyAddress> 


<style type="text/css">
table,tr,td{border:1px solid black;border-collapse:collapse;}
</style>

<table>
<tr>
<td>@Html.DisplayNameFor(model => model.Address1)</td><td>@Html.DisplayNameFor(model => model.City)</td>
</tr>
@foreach (var item in Model)
{
    <tr><td>@Html.DisplayFor(modelItem => item.Address1)</td><td>@Html.DisplayFor(modelItem => item.City)</td>
</tr>
}
</table>

this working fine and display in table format.

still i want to solve another problem .

in the above code i wrote table header name and fieldnames manually.because i know i the field names.

how to write the table header name and field names automatically.

regards

Baiju

Hi friend thans for your reply

how to write the table header name and field names automatically in View

klbaiju

how to write the table header name and field names automatically in View

please open a new thread.

I have added a new thread.

http://forums.asp.net/p/2017019/5804204.aspx?p=True&t=635506811254954705&pagenum=1

Leave a Reply