如何在MVC模型中使用Webservice

时间:2013-08-30 13:49:48

标签: c# asp.net-mvc wcf web-services

我有一个非常简单的MVC模型,其中我有两个非常简单的Model类Person和Company。

我必须使用Web服务来获取有关人员和公司的数据。

您能否发布一些示例linke,其中使用web服务进行GET或/和POST。

这是我的控制器索引方法。

public ActionResult Index(string id)
{
Webservice webservice = new Webservice();
}

[HttpPost]
public ActionResult Index(string id)
{
Webservice webservice = new Webservice();
}

我不知道是否在Get或Post中编写上述代码。

2 个答案:

答案 0 :(得分:5)

我个人在模型中使用它。例如,我有一个OData服务,我在我的模型中调用它:

public class Person
{
    public string Name {get;set;}
    public Person(int Id)
    {
        var oDataService = new ODataService(new Uri("YourURL"));
        Name = oDataService.Persons.Where(x=>x.Id == Id).Select(x=>x.Name);
    }
}

然后在控制器中:

public ActionResult Index(int Id)
{
    return View(new Person(Id));
}

答案 1 :(得分:1)

模型是您的数据。控制器将管理它。这意味着你应该编写逻辑来在控制器中加载数据并将其转换为模型的对象。