结合不同模型的两个视图

发布于 2024-12-02 02:22:56 字数 1577 浏览 1 评论 0原文

大家好,我不知道该怎么做。我有两个视图和两个独立的模型,我想将这两个视图合并起来,以便它们都在一个视图上。

视图 1:

@model IEnumerable<TelephoneNumberManagement.Models.Range>

<table>
    <tr>
        <th>
            RangeName
        </th>
        <th>
            RangeNumber
        </th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.ActionLink(item.RangeName, "ViewSubRange", new { id = item.ID })
        </td>
        <td>
            @item.RangeNumber
        </td>
    </tr>
}
</table>

视图 2:-

@model IEnumerable<TelephoneNumberManagement.Models.TestNumber>
<h2>Index</h2>

<table>
    <tr>
        <th>
            Number
        </th>
        <th>
            Status
        </th>
        <th>
            Customer
        </th>
    </tr>

@foreach (var item in Model)
{
    <tr> 
        <td>@item.Number</td>
        <td>@item.Status.StatusName</td>
        <td>@item.CustomerID</td>
    </tr> 

}
</table>

HomeController:-

public ViewResult Index()
{
  return View(context.Ranges.ToList().OrderBy(m => m.RangeName));
}

public ActionResult ViewSubRange(int id)
{
  IEnumerable<TestNumber> testNumbersList = context.TestNumbers.Where(m => m.RangeID == id).ToList();

  return View("SubRange", testNumbersList);
}

任何帮助将非常感激,因为我正在用这个把头撞在墙上!

Hi all i'm not sure how to do this. I have two views with 2 separate models, i would like to combine the 2, so that they are both on one view.

View 1:

@model IEnumerable<TelephoneNumberManagement.Models.Range>

<table>
    <tr>
        <th>
            RangeName
        </th>
        <th>
            RangeNumber
        </th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.ActionLink(item.RangeName, "ViewSubRange", new { id = item.ID })
        </td>
        <td>
            @item.RangeNumber
        </td>
    </tr>
}
</table>

View 2:-

@model IEnumerable<TelephoneNumberManagement.Models.TestNumber>
<h2>Index</h2>

<table>
    <tr>
        <th>
            Number
        </th>
        <th>
            Status
        </th>
        <th>
            Customer
        </th>
    </tr>

@foreach (var item in Model)
{
    <tr> 
        <td>@item.Number</td>
        <td>@item.Status.StatusName</td>
        <td>@item.CustomerID</td>
    </tr> 

}
</table>

HomeController:-

public ViewResult Index()
{
  return View(context.Ranges.ToList().OrderBy(m => m.RangeName));
}

public ActionResult ViewSubRange(int id)
{
  IEnumerable<TestNumber> testNumbersList = context.TestNumbers.Where(m => m.RangeID == id).ToList();

  return View("SubRange", testNumbersList);
}

Any help would be really appreciated as i'm banging my head against the wall with this!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

深府石板幽径 2024-12-09 02:22:56

获取两个视图并将它们放入局部视图中(如果您仍然需要原始视图,请让它们也使用局部视图,以便您的视图定义在一处 - 局部视图)。对于“两者都在一个视图中”,添加一个新操作和一个模型,该模型具有部分所需的每个模型的实例。

模型:

public class MyBothInOneModel
{
    public ModelA modelA { get; set; }
    public ModelB modelB { get; set; }
}

操作:

public ViewResult BothInOne(int idForB)
{
    MyBothInOneModel m = new MyBothInOneModel();
    m.modelA = context.Ranges.ToList().OrderBy(m => m.RangeName));
    m.modelB = context.TestNumbers.Where(m => m.RangeID == idForB).ToList();
}

视图 - 添加一个视图,并调用为每个视图渲染部分:

...
     Html.RenderPartial(Model.modelA, "PartialA");
     Html.RenderPartial(Model.modelB, "PartialB");
...

或者您可以使用 ViewData/ViewBag 来避免创建 MyBothInOneModel 类。

Take your two views and put them into partials (if you still need the original views have them also use the partials so your views are defined in one place -- the partials). For your "both in one view", add a new action and a model that has an instance of each model needed for the partials.

Model:

public class MyBothInOneModel
{
    public ModelA modelA { get; set; }
    public ModelB modelB { get; set; }
}

Action:

public ViewResult BothInOne(int idForB)
{
    MyBothInOneModel m = new MyBothInOneModel();
    m.modelA = context.Ranges.ToList().OrderBy(m => m.RangeName));
    m.modelB = context.TestNumbers.Where(m => m.RangeID == idForB).ToList();
}

View - add a view with a call to render partial for each:

...
     Html.RenderPartial(Model.modelA, "PartialA");
     Html.RenderPartial(Model.modelB, "PartialB");
...

Or you can use ViewData/ViewBag to avoid creating MyBothInOneModel class.

空城缀染半城烟沙 2024-12-09 02:22:56

你有很多选择。首先,您可以创建一个具有属性

IEnumerable的 ViewModel TestNumbers

和属性

IEnumerableRanges

,然后返回一个视图,绑定到新的 ViewModel 并使用组合数据。

作为替代方案(恕我直言,更智能的解决方案),您可以将每个视图渲染为子视图 @Html.Action("ActionForView1")@Html.Action("ActionForView2")

You have a bunch of options. First you can create a ViewModel having a property

IEnumerable<TelephoneNumberManagement.Models.TestNumber> TestNumbers

and a property

IEnumerable<TelephoneNumberManagement.Models.Range> Ranges

and then return a view, binding to the new ViewModel and using the combined data.

As an alternative (imho smarter solution) you could render each view as a child view @Html.Action("ActionForView1") and @Html.Action("ActionForView2").

呆橘 2024-12-09 02:22:56

您可以使用 ViewModel 模式来解决这个问题。创建一个“ViewModel”,它结合了给定视图所需的所有不同模型和/或表示逻辑。

例如:

class TelephoneViewModel
{
    public IEnumerable<TelephoneNumberManagement.Models.TestNumber> { get; set;}
    public IEnumerable<TelephoneNumberManagement.Models.Range> { get; set; }
}

然后让您的新组合视图使用 ViewModel 作为其模型。

You can use the ViewModel pattern to solve this. Create a "ViewModel" that combines all the different models and/or presentation logic you need for a given view.

e.g.:

class TelephoneViewModel
{
    public IEnumerable<TelephoneNumberManagement.Models.TestNumber> { get; set;}
    public IEnumerable<TelephoneNumberManagement.Models.Range> { get; set; }
}

Then let your new combined view use the ViewModel for it's model.

梦中楼上月下 2024-12-09 02:22:56

另一种选择是为视图使用动态变量

@model IEnumerable<dynamic>

然后您可以将所需的任何模型发送到视图,但您需要明智地了解要使用的属性。

Another option is to use a dynamic variable for your views

@model IEnumerable<dynamic>

Then you could send any model you want to the view, but you would need to be wise about what properties to use.

﹏半生如梦愿梦如真 2024-12-09 02:22:56

这是我正在谈论的一个基本示例。

 public class LoginRegisterMemberViewModel
{
    public OpenIdLoginViewModel OpenIdLoginViewModel { get; set; }
    public SiteLoginViewModel SiteLoginViewModel { get; set; }
    public RegisterMemberViewModel RegisterMemberViewModel { get; set; }
    public string ReturnUrl { get; set; }
}

在我的登录视图上,我使用 @model Web.ViewModels.LoginRegisterMemberViewModel

部分视图也使用 @model Web.ViewModels.LoginRegisterMemberViewModel
所以现在我可以访问 LoginRegsiterMemberViewModel.SiteLoginViewModel.property 等。

希望这并不完全偏离您想要完成的目标。

Here is a basic example of what I am talking about.

 public class LoginRegisterMemberViewModel
{
    public OpenIdLoginViewModel OpenIdLoginViewModel { get; set; }
    public SiteLoginViewModel SiteLoginViewModel { get; set; }
    public RegisterMemberViewModel RegisterMemberViewModel { get; set; }
    public string ReturnUrl { get; set; }
}

On my Login View I user @model Web.ViewModels.LoginRegisterMemberViewModel

The partial views also use @model Web.ViewModels.LoginRegisterMemberViewModel
So now I have access to LoginRegsiterMemberViewModel.SiteLoginViewModel.property etc..

Hope this is not totally off base from what you are trying to accomplish.

花开浅夏 2024-12-09 02:22:56

如果您希望获得干净的分离,另一个不错的选择是从主视图使用 Html.RenderAction。然后为所描述的每个视图实现单独的控制器。

Also a good option, if you wish to obtain clean separation, is to use Html.RenderAction from a main view. Then implement to separate controllers for each of the views described.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文