简体   繁体   English

如何将详细模型从Controller传递到View

[英]How to Pass Details Model from Controller to View

I am accessing Data in Controller using a WCF Service. 我正在使用WCF服务访问Controller中的数据。

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

Now how to pass the orderdetails from Controller to view and how to access them in View. 现在,如何从Controller传递orderdetails以查看以及如何在View中访问它们。

Edit: 编辑:

I have a Model : 我有一个模型:

 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; }
    }

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

  1. Do I need a Local Model at all in this scenario? 在这种情况下我是否需要本地模型?
  2. How do I display the List values in a table in my view? 如何在视图中的表格中显示列表值?

Edit2: EDIT2:

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

Now getting the Error 现在得到错误

'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 'NorthWindMVCWCF.ServiceOrders.OrderDetails'是'type',在给定的上下文中无效'NorthWindMVCWCF.Models.OrderDetails'是'type',在给定的上下文中无效

I prefer to create view models, I would do it as follows: 我更喜欢创建视图模型,我会这样做:

Create View Models 创建视图模型

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; }
}

Manual projection 手动投影

You could create an OrderDetails view model and project an instance manually as follows: 您可以创建OrderDetails视图模型并手动投影实例,如下所示:

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 alternative projection AutoMapper替代投影

Install AutoMapper, run the following from the package manager console: 安装AutoMapper,从包管理器控制台运行以下命令:

Install-Package AutoMapper

Create an AutoMapperConfig.cs in the App_Start folder with the mappings, similar to below: 使用AutoMapperConfig.csApp_Start文件夹中创建AutoMapperConfig.cs ,类似于以下内容:

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

In your global asax call the configure method: 在您的全局asax中调用configure方法:

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

Then map in your controller: 然后在你的控制器中映射:

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

I prefer to use the AutoMapper approach as the mapping is defined globally and can be reused within your app. 我更喜欢使用AutoMapper方法,因为映射是全局定义的,可以在您的应用程序中重用。

Returning your view model 返回您的视图模型

Your view model would then be passed back as follows: 然后,您的视图模型将按如下方式传回:

        return View(orderDetailsViewModel);

Razor output 剃刀输出

You access it in your view by adding a model reference at the top: 您可以通过在顶部添加模型参考来在视图中访问它:

@model OrderDetailsViewModel

Then output the properties as follows, I've only included OrderId but you can just add the fields the same way: 然后按如下方式输出属性,我只包含OrderId但您可以以相同的方式添加字段:

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

You have create ViewModel for that. 您已为此创建ViewModel。

For example in your model folder create a class: 例如,在您的模型文件夹中创建一个类:

public class MyViewModel // Whatever name you want to give
{
     //My fields which I want to pass to View
     publis string Field1{get;set;}
     etc
     etc
}

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

      List<MyViewModel> viewModelList = new List<MyViewModel>();

      foreach(var orderDetail in orderDetails)
      {
           MyViewModel viewModel = new MyViewModel(); //Create an object of your ViewModel
           viewModel.Field1 = orderDetails.Field1; //set all feilds like that      
           viewModelList.Add(viewModel); 
      }   
      return View(viewModelList); // Pass View Model to View
}

Note: You have to create View for your ViewModel Like 注意:您必须为ViewModel Like 创建View

@model `List<MyViewModel>`

then use this ViewModel to access properties. 然后使用此ViewModel访问属性。

To know more about what is ViewModel, refer to the link below: 要了解有关ViewModel的更多信息,请参阅以下链接:

http://sampathloku.blogspot.ae/2012/10/how-to-use-viewmodel-with-aspnet-mvc.html http://sampathloku.blogspot.ae/2012/10/how-to-use-viewmodel-with-aspnet-mvc.html

in the Controller class write : 在Controller类中写:

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

i assume your project name is MvcApplication2 and your class name is in Model Folder. 我假设您的项目名称是MvcApplication2,您的类名称在模型文件夹中。 so add follow code in top of View (sample.cshtml) : 所以在View(sample.cshtml)的顶部添加以下代码:

@model MvcApplication2.Models.OrderDetail

ok , you can use this code to access properties of DataRecieveClient : 好的,您可以使用此代码访问DataRecieveClient的属性:

<div class="display-label">
     @Html.DisplayNameFor(model => model.F1)
</div>
<div class="display-field">
    @Html.DisplayFor(model => model.F1)
</div>

Sorry for poor english.! 抱歉英语不好。!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM