如何将两个模型合并为一个模型并使用asp.net MVC剃刀将其传递给视图 [英] How to Combine two models into a single model and pass it to view using asp.net MVC razor
问题描述
我正在尝试将两个模型(tblCategories和tblProducts)组合为一个模型,并将其传递给视图.但是无法.
I'm trying to combine two models (tblCategories and tblProducts) into a single model and pass it to a view. But was not able to.
tblCategory.cs 具有以下数据
namespace ScaffoldCreate.Models
{
using System;
using System.Collections.Generic;
public partial class tblCategory
{
public int CategoryId { get; set; }
public string Name { get; set; }
}
}
tblProduct.cs
public partial class tblProduct
{
public int ProductId { get; set; }
public int ProductName { get; set; }
public int CategoryId { get; set; }
public int SubCategoryId { get; set; }
public string ProductDescription { get; set; }
public string StockStatus { get; set; }
public IList<tblCategory> CategoryName { get; set; }
}
}
我尝试将tblCategory放入tblProduct,并尝试按如下方式在索引页中检索它:
I tried to get tblCategory into tblProduct and tried to retrieve that in index page as follows:
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.CategoryName.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.ProductId)
</td>
<td>
@Html.DisplayFor(modelItem => item.CategoryId)
</td>
<td>
@Html.DisplayFor(modelItem => item.SubCategoryId)
</td>
<td>
@Html.DisplayFor(modelItem => item.ProductName)
</td>
<td>
@Html.DisplayFor(modelItem => item.ProductDescription)
</td>
<td>
@Html.DisplayFor(modelItem => item.StockStatus)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) |
@Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ }) |
@Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })
</td>
</tr>
}
但是,我无法通过它.有什么可以帮助我解决这个问题的吗?
But, I was not able to get through it. Could any assist me in solving this ?
推荐答案
为您的视图使用ViewModel,它将解决您的问题:
Use the ViewModel for your View that will Solve your Problem::
class CommonViewModel
{
Model1 model1;
Model2 model2;
}
其中Model1& Model2是您的模型aS ::
where Model1 & Model2 are your Models aS::
public class Model1
{
public int ID{get;set;}
public string Name{get;set;}
}
和Model2一样::
and Model2 like::
public class Model2
{
public int ID{get;set;}
public string Name{get;set;}
}
在您的视图中,您将其作为强类型输入:
And into your View you do it as strongly Typed::
@model ProjectName.CommonViewModel
在这里您可以将Model1用作:: 您也可以将模型传递到另一个局部视图,也可以在同一视图中使用:
Here you can use the Model1 as:: you can pass the Model to Another Partial View As well and can use in the same view as well::
@Html.Partial("_PartialViewName",Model.Model1)
@foreach (var item in Model.Model2)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.CategoryName.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.ProductId)
</td>
<td>
@Html.DisplayFor(modelItem => item.CategoryId)
</td>
<td>
@Html.DisplayFor(modelItem => item.SubCategoryId)
</td>
<td>
@Html.DisplayFor(modelItem => item.ProductName)
</td>
<td>
@Html.DisplayFor(modelItem => item.ProductDescription)
</td>
<td>
@Html.DisplayFor(modelItem => item.StockStatus)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) |
@Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ }) |
@Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })
</td>
</tr>
}
这篇关于如何将两个模型合并为一个模型并使用asp.net MVC剃刀将其传递给视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!