MVC 3 ViewModel 模式问题(使用 RAZOR)

发布于 2024-10-31 03:36:15 字数 1976 浏览 1 评论 0原文

我研究过这个问题,但找不到解决方案。我的问题是,在我看来,我无法查看 ViewModel 的属性(例如 Model.Supplier.SupplerName)。 Intellisense 只允许我使用 Model.Suppliers 、 Model.ToolTypes 或 Model.ToolTypesSuppliers 。我无法获取包含在 ViewModel 中的每个模型的属性。

ViewModel:

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

namespace ToolOrder.Models
{
    public class ToolTypeSupplierViewModel
    {
        public ToolTypeSupplierViewModel(IEnumerable<ToolType> tooltypes, IEnumerable<Supplier> suppliers, IEnumerable<ToolTypeSupplier> tooltypesuppliers)
        {
            this.ToolTypes = tooltypes;
            this.Suppliers = suppliers;
            this.ToolTypeSuppliers = tooltypesuppliers;
        }

        public IEnumerable<ToolType> ToolTypes { get; private set; }
        public IEnumerable<Supplier> Suppliers { get; private set; }
        public IEnumerable<ToolTypeSupplier> ToolTypeSuppliers { get; private set; }

    }
}

控制器:

public ActionResult ListToolTypeSupplierOptions()
{
    var _tooltypelist =_repository.FetchAllToolTypeOptions();
    var _supplierlist = _repository.FetchAllSupplierOptions();
    var _toolstypesupplierlist = _repository.FetchAllToolTypeSupplierOptions();

    return View(new ToolTypeSupplierViewModel(_tooltypelist, _supplierlist, _toolstypesupplierlist));
}

视图:

@model IEnumerable<ToolOrder.Models.ToolTypeSupplierViewModel>


<table>
    <tr>
        <th>Supplier</th>
        <th>Tool TYpe</th>
        <th>Created</th>
        <th>Last Modified </th>
    </tr>

@foreach (var item in Model.) {
    <tr>
        <td>
        </td>
    </tr>
}

</table>

任何帮助将不胜感激,或者如果需要任何进一步的说明,我可以跟进。如果这有点模糊,我很抱歉,我一直在试图找出为什么我无法提取我包含在 ViewModel 中的每个模型的属性。先感谢您。

I've researched this issue and can not find a solution. My problem is that in my view, I can not view the properties of my ViewModel (e.g., Model.Supplier.SupplerName). Intellisense will only allow me to go Model.Suppliers, or Model.ToolTypes, or Model.ToolTypesSuppliers . I can not get the properties of each of these models that I included into the ViewModel.

ViewModel:

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

namespace ToolOrder.Models
{
    public class ToolTypeSupplierViewModel
    {
        public ToolTypeSupplierViewModel(IEnumerable<ToolType> tooltypes, IEnumerable<Supplier> suppliers, IEnumerable<ToolTypeSupplier> tooltypesuppliers)
        {
            this.ToolTypes = tooltypes;
            this.Suppliers = suppliers;
            this.ToolTypeSuppliers = tooltypesuppliers;
        }

        public IEnumerable<ToolType> ToolTypes { get; private set; }
        public IEnumerable<Supplier> Suppliers { get; private set; }
        public IEnumerable<ToolTypeSupplier> ToolTypeSuppliers { get; private set; }

    }
}

Controller:

public ActionResult ListToolTypeSupplierOptions()
{
    var _tooltypelist =_repository.FetchAllToolTypeOptions();
    var _supplierlist = _repository.FetchAllSupplierOptions();
    var _toolstypesupplierlist = _repository.FetchAllToolTypeSupplierOptions();

    return View(new ToolTypeSupplierViewModel(_tooltypelist, _supplierlist, _toolstypesupplierlist));
}

View:

@model IEnumerable<ToolOrder.Models.ToolTypeSupplierViewModel>


<table>
    <tr>
        <th>Supplier</th>
        <th>Tool TYpe</th>
        <th>Created</th>
        <th>Last Modified </th>
    </tr>

@foreach (var item in Model.) {
    <tr>
        <td>
        </td>
    </tr>
}

</table>

Any help would be appreciated or if any further clarification is required, I can follow up. I apologize if this is somewhat vague, I've been up trying to figure out why I can not pull the the properties of each Model that I've included in my ViewModel. Thank you in advance.

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

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

发布评论

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

评论(3

ペ泪落弦音 2024-11-07 03:36:15

试试这个:

@foreach (var item in Model) {
  @foreach (var supplier in item.Suppliers){
    <tr>
        <td>
           @supplier.SupplierName
        </td>
    </tr>
  }
}

看来您需要循环遍历供应商,它是 IEnumerable。

Try this:

@foreach (var item in Model) {
  @foreach (var supplier in item.Suppliers){
    <tr>
        <td>
           @supplier.SupplierName
        </td>
    </tr>
  }
}

It seems you need to loop through the Suppliers it's IEnumerable.

无力看清 2024-11-07 03:36:15

您的问题在某种程度上具有误导性,因此我将尝试回答这两种情况。

  1. 正如我所看到的,您将 ToolTypeSupplierViewModel 的单个实例从控制器传递到您的视图,并且不可枚举。如果这不是错误,那么您的观点就是错误 - 模型不应是 ToolTypeSupplierViewModel 的 IEnumerable,而是单个 ToolTypeSupplierViewModel。在这种情况下,您可以直接迭代 Model.ToolTypes、Suppliers 等。
  2. 如果错误不在视图中,但在控制器和模型中应该是 IEnumerable 类型,那么首先您应该直接迭代 Model,因为它本身就是 Enumerable。它的项目是ToolTypeSupplierViewModel。在这种情况下,代码将如 ysrb 建议的那样

Your question is somehow misleading, so i'll try to answer both cases.

  1. As i see you pass single instance of ToolTypeSupplierViewModel to your view from controller, and not enumerable. If this is not mistake, than mistake is in your view - model should be not IEnumerable of ToolTypeSupplierViewModel, but single ToolTypeSupplierViewModel. In this case, you could iterate over Model.ToolTypes, Suppliers, etc. Directly
  2. If the mistake is not in view, but in controller and model should be of type IEnumerable<ToolTypeSupplierViewModel>, than first you sould iterate over Model directly, as it IS Enumerable itself. Its items are ToolTypeSupplierViewModel. In this case, code will be as ysrb suggested
三岁铭 2024-11-07 03:36:15

尝试在 Razor 文件顶部使用 @using ToolOrder.Models

或者在 web.config 中,找到命名空间部分并将其添加到那里

  <namespaces>
    ...
    <add namespace="ToolOrder.Models" />

Try @using ToolOrder.Models at the top of your Razor file.

or in web.config, find the namespaces section and add it there

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