MVC 3 ViewModel 模式问题(使用 RAZOR)
我研究过这个问题,但找不到解决方案。我的问题是,在我看来,我无法查看 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
试试这个:
看来您需要循环遍历供应商,它是 IEnumerable。
Try this:
It seems you need to loop through the Suppliers it's IEnumerable.
您的问题在某种程度上具有误导性,因此我将尝试回答这两种情况。
ToolTypeSupplierViewModel
的单个实例从控制器传递到您的视图,并且不可枚举。如果这不是错误,那么您的观点就是错误 - 模型不应是ToolTypeSupplierViewModel
的 IEnumerable,而是单个ToolTypeSupplierViewModel
。在这种情况下,您可以直接迭代 Model.ToolTypes、Suppliers 等。IEnumerable
类型,那么首先您应该直接迭代 Model,因为它本身就是 Enumerable。它的项目是ToolTypeSupplierViewModel
。在这种情况下,代码将如 ysrb 建议的那样Your question is somehow misleading, so i'll try to answer both cases.
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 ofToolTypeSupplierViewModel
, but singleToolTypeSupplierViewModel
. In this case, you could iterate over Model.ToolTypes, Suppliers, etc. DirectlyIEnumerable<ToolTypeSupplierViewModel>
, than first you sould iterate over Model directly, as it IS Enumerable itself. Its items areToolTypeSupplierViewModel
. In this case, code will be as ysrb suggested尝试在 Razor 文件顶部使用
@using ToolOrder.Models
。或者在 web.config 中,找到命名空间部分并将其添加到那里
Try
@using ToolOrder.Models
at the top of your Razor file.or in web.config, find the namespaces section and add it there