c# - 如何将细节模型从 Controller 传递到 View

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

我正在使用 WCF 服务访问 Controller 中的数据。

 public ActionResult Index()
        {
            DataRerieveClient _proxy = new DataRerieveClient();
            var orderDetails = _proxy.GetProductDetails(null);
            return View();
        }

现在如何传递 orderdetails从 Controller 到 View 以及如何在 View 中访问它们。

编辑:

我有一个模型:

 public class OrderDetails
    {
        public int OrderId { get; set; }
        public int ProductId { get; set; }
        public decimal UnitPrice { get; set; }
        public int quanity { get; set; }
        public decimal Discount { get; set; }
    }

_proxy.GetProductDetails(null)返回 List<ServiceType.OrderDetails>

  1. 在这种情况下我是否需要本地模型?
  2. 如何在我的 View 中显示表格中的列表值?

编辑2:

public class AutoMapperConfig
    {
        public static void Configure()
        {
            Mapper.Map(ServiceOrders.OrderDetails, NorthWindMVCWCF.Models.OrderDetails);
        }
    }

现在得到错误

'NorthWindMVCWCF.ServiceOrders.OrderDetails' is a 'type', which is not valid in the given context 'NorthWindMVCWCF.Models.OrderDetails' is a 'type', which is not valid in the given context

最佳答案

我更喜欢创建 View 模型,我会按如下方式进行:

创建 View 模型

public class OrderDetailViewModel
{
  public int OrderId { get; set; }
  public int ProductId { get; set; }
  public decimal UnitPrice { get; set; }
  public int Quanity { get; set; }
  public decimal Discount { get; set; }
}

public class OrderDetailsViewModel
{
  public OrderDetailsViewModel()
  {
      OrderDetails = new List<OrderDetailsViewModel>();
  }

  public List<OrderDetailsViewModel> OrderDetails { get; set; }
}

手动投影

您可以创建一个 OrderDetails View 模型并手动投影一个实例,如下所示:

var orderDetailsViewModel = new OrderDetailsViewModel();
foreach(var orderdetail in orderDetails)
{
 orderDetailsViewModel.Add(new OrderDetailsViewModel { OrderId = orderDetail.OrderId, ProductId = orderDetail.ProductId, UnitPrice = orderDetail.UnitPrice, Quanity = orderDetail.quantity, Discount = orderDetail.Discount });
}

AutoMapper 替代投影<​​/strong>

安装 AutoMapper,从包管理器控制台运行以下命令:

Install-Package AutoMapper

使用映射在 App_Start 文件夹中创建一个 AutoMapperConfig.cs,类似于以下内容:

public static class AutoMapperConfig
{
  public static void Configure()
  {
    Mapper.CreateMap<OrderDetails, OrderDetailViewModel>();
  }
}

在您的全局 asax 中调用配置方法:

protected void Application_Start()
{
   ...
   AutoMapperConfig.Configure();
   ...
}

然后映射到你的 Controller 中:

var orderDetailsViewModel = new OrderDetailsViewModel();
orderDetailsViewModel.OrderDetails = Mapper.Map<List<OrderDetails>, List<OrderDetailsViewModel>>(orderDetails);

我更喜欢使用 AutoMapper 方法,因为映射是全局定义的,可以在您的应用中重复使用。

返回你的 View 模型

然后您的 View 模型将按如下方式传回:

        return View(orderDetailsViewModel);

Razor 输出

您可以通过在顶部添加模型引用来在您的 View 中访问它:

@model OrderDetailsViewModel

然后按如下方式输出属性,我只包含了 OrderId 但你可以用同样的方式添加字段:

<table>
<tr>
<th>OrderId</th>
</tr>
@foreach(var orderDetail in Model.OrderDetails)
{
  <tr>
    <td>@orderDetail.OrderId</td>
  </tr>
}
</table>

关于c# - 如何将细节模型从 Controller 传递到 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21878957/

相关文章:

javascript - 使用 Jquery.valid() 在 Asp.net MVC 中进行多表单验证

c# - 如何在 c# Azure Function 项目中的多个类中重用 ILogger?

jquery - 如何在Jquery中使用ID选择asp.net控件?

c# - 使用 ASP.NET MVC 时如何设置新页面/ View 的 URL

c# - 将选定值的集合从 Select2-Multi DropDownList 传递到 Controller

c# - 如何在 C# 中从数据库 > 资源文件中检索数据注释字符串?

c# - Azure 表存储在一个事务中读取和更新项目

c# - Zebra ZPL 自定义 header - 打印机解释

c# - 找不到类型或命名空间名称 'dynamic'

javascript - 从服务器上传并返回多个 HttpPostedFiles