First we see how to pull data from wcf client into our mvc, (here i am not giving the example of how to create wcf and publish the service, i hope you are already familier with that process)
we create an interface that will consume the wcf client.
public interface IClientService
{
List
}
public class ClientService : BaseModel, IClientService
{
private readonly ICustomerService _provider;
public ClientService()
: this(null)
{
}
public ClientService(CustomerServiceClient provider)
{
_provider = provider ?? new CustomerServiceClient();
// this service client is WCF service client.
}
// finally database / wcf call done here.
public List
{
List
tbProductMaster[] custArry = _provider.GetProductMaster();
custs = custArry.ToList
return custs;
}
}
Now we create a controller class that will fetch data from client service we have just create in above example
[HandleError]
public class ClientController : Controller
{
// this interface has all defination
public IClientService ClientService { get; set; }
protected override void Initialize(RequestContext requestContext)
{
if (ClientService == null) { ClientService = new ClientService(); }
base.Initialize(requestContext);
}
// here now we load data into model from our data access layer, in this example we are using a WCF service to pull data from data source, so i am not putting to code of wcf call, you can use any data access to pull data from datasource
public ActionResult ManageClients()
{
ViewData["Message"] = "Manage Clients!";
var model = new ClientViewModel();
model.ProductMaster = ClientService.GetProducETGProducts();
return View(model);
}
}
Lets take a look at how the model will look like, we have already used in above ManageClients() method, and this same model we will be using to create a view (.aspx / .ascx ) pages
public class ClientViewModel : BaseModel
{
public List< tbContact> Clients { get; set; }
public List< tbProductMaster> ProductMaster { get; set; }
}
Finally, now this is how your html .aspx page page will look like
<% @ Page Title="Manage Clients" Language="C#" MasterPageFile="~/Views/Shared/Site.Master"
Inherits="System.Web.Mvc.ViewPage
< asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<%: Html.DropDownList("ProductMaster", new SelectList(Model.ProductMaster, "ProductId", "Product")) %>
</ asp:Content>
You are done! simply run the page, you should be able to see the dropdown list, make sure you have set the property name properly.
No comments:
Post a Comment