【问题标题】:Accessing WCF Service in MVC Razor在 MVC Razor 中访问 WCF 服务
【发布时间】:2014-10-06 11:23:33
【问题描述】:

我是 MVC 的新手。我只想使用 mvc 和 wcf 服务在我的页面上显示故事标题列表。调试我的应用程序后抛出异常:

找不到引用合约的默认端点元素 ServiceModel 客户端配置中的“ServiceReference1.IService1” 部分。这可能是因为找不到配置文件 您的应用程序,或者因为没有与此匹配的端点元素 合同可以在客户端元素中找到。

代码如下:

HomeController.cs

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

namespace MvcMovie.Controllers
{
    public class HomeController : Controller
    {
       // ServiceReference1.Service1Client cs = new ServiceReference1.Service1Client();
       public ActionResult Index()
      {
         //ViewBag.Message = "Modify this template to jump-start your ASP.NET MVC application.";
         ServiceReference1.Service1Client cs = new ServiceReference1.Service1Client();
         List<ServiceReference1.Story> tm  = cs.GetBollywoodStory();

         return View();
      }
    }
}

Index.cshtml

@{
    ViewBag.Title = "Index";
}

<h2>My Movie List</h2>
<p>Hello from our View Template!</p>

 @*<%n ServiceReference1.Service1Client cs = new ServiceReference1.Service1Client();%>
      *@Name
      <%foreach (ServiceReference1.Story dr in tm)
      {%>       
      <%=dr["Name"].ToString() %>
      <% } %>

请帮帮我.....

【问题讨论】:

    标签: c# asp.net-mvc wcf asp.net-mvc-4 razor


    【解决方案1】:

    您不应该在视图中编写代码逻辑!

    在您的控制器中,您应该获取查看所需的所有数据并将其放入某个 ViewModel 类中。 然后从您的模型中将其展示给您的客户。

    假设您创建了一些名为 MovieViewModel 的 ViewModel;

    public class MovieViewModel {
       public string Name { get;  set; }
       ... //rest of properties
    }
    

    然后您在控制器中填充您的视图模型并将其发送到您的视图:

    控制器

    public ActionResult Index()
    {
      //ViewBag.Message = "Modify this template to jump-start your ASP.NET MVC application.";
      ServiceReference1.Service1Client cs = new ServiceReference1.Service1Client();
      List<ServiceReference1.Story> tm  = cs.GetBollywoodStory();
    
      //populate viewmodel
      var movies = new  List<MovieViewModel>();
    
      foreach(var item in tm) {
         movies.Add(new MovieViewModel {
            Name = item.Name;
            ...
         });
      }
    
      return View(movies);
    }
    

    当您将填充模型传递给您的视图时,您可以将其呈现给您的客户端:

    查看

    @model IEnumerable<MvcMovie.<folder of your viewmodels>.MovieViewModel>
    
    @{
        ViewBag.Title = "Index";
    }
    
    <h2>My Movie List</h2>
    <p>Hello from our View Template!</p>
    
    @foreach(var movie in Model) {
       @Html.DisplayNameFor(m => m.Name)
       @Html.DisplayFor(m => m.Name)
    }
    

    *注意视图开头的模型参考!

    您应该了解 Razor 以及 MVC 助手... 这可以作为起点,但您需要找到一些关于 MVC 的好教程。

    也许是这样:http://www.microsoftvirtualacademy.com/training-courses/developing-asp-net-mvc-4-web-applications-jump-start

    【讨论】:

    • 感谢freshbm重播,但由于我已经在WCF服务中编写了类,是否有必要在MVC中编写模型?可能这是一个非常外行的问题,但坦率地说,我刚刚开始研究 MVC。抱歉问了这么愚蠢的问题。请尽快回复我,以便我继续对 MVC 感兴趣.......
    • 是的!您使用控制器为视图准备数据,视图仅用于向用户呈现数据。您不应该在视图上编写数据访问代码。所以我的答案是肯定的!这是最佳做法。
    猜你喜欢
    • 2014-10-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-18
    • 1970-01-01
    相关资源
    最近更新 更多