部分视图中的 asp.net MVC3 @model

发布于 2024-12-10 01:35:48 字数 197 浏览 0 评论 0原文

在我的控制器中,我进行如下初始化:

using mylib.product;
using mylib.factory;

product p = new product();
factory f = new factory(p);

如何在部分视图中使用 @model 关键字执行相同的操作?

谢谢

In my controller I do initialization like this:

using mylib.product;
using mylib.factory;

product p = new product();
factory f = new factory(p);

How do I do the same thing using the @model keyword in a partial view?

Thanks

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

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

发布评论

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

评论(4

霊感 2024-12-17 01:35:48

如果您尝试将名称空间/类添加到视图中,那么它是:

@using mylib.product;

If you are trying to add namespaces/classes to you view, then it's:

@using mylib.product;
聽兲甴掵 2024-12-17 01:35:48

将模型解析为视图;

return View("ViewName");

我应该通过视图

@model Project.Namespace.Class

I should parse the model to the view by

return View("ViewName");

and in the view;

@model Project.Namespace.Class
孤君无依 2024-12-17 01:35:48

您应该使用视图模型:

public class MyViewModel
{
    public string Name { get; set; }
    public string Address { get; set; }
}

它将从控制器操作传递到视图:

public ActionResult Index()
{
    product p = new product(); 
    factory f = new factory(p);   
    var model = new MyViewModel
    {
        Name = p.Name,
        Address = f.Address
    }
}

然后您的视图将被强类型化到该视图模型:

@model MyViewModel
@Html.DisplayFor(x => x.Name)
@Html.DisplayFor(x => x.Address)

You should use view models:

public class MyViewModel
{
    public string Name { get; set; }
    public string Address { get; set; }
}

which will be passed to the view from the controller action:

public ActionResult Index()
{
    product p = new product(); 
    factory f = new factory(p);   
    var model = new MyViewModel
    {
        Name = p.Name,
        Address = f.Address
    }
}

and then your view will be strongly typed to this view model:

@model MyViewModel
@Html.DisplayFor(x => x.Name)
@Html.DisplayFor(x => x.Address)
深爱不及久伴 2024-12-17 01:35:48

我认为你需要将不同类的多个实例传输到View。(我是对的吗?)如果是,我建议使用ViewBag。像这样的事情:

// Controller
=========
product p = new product(); 
factory f = new factory(p);
....
// Add some value for p and f 
ViewBag.Product = p;
ViewBag.Factory = f;
return View();

// View
=========
var p = (product) ViewBag.Product;
var f = (factory) ViewBag.Factory;
// now you have access to p and f properties, for example:
@Html.Label(p.Name)
@Html.Label(f.Address)

不要忘记 ViewBag 是一个动态容器,当您想在 View 中使用它的值时,您需要将其转换为类型

I think you need to transfer more than one instance of different classes to View.(Am I right?) If yes, I suggest to use ViewBag for it. Something like this:

// Controller
=========
product p = new product(); 
factory f = new factory(p);
....
// Add some value for p and f 
ViewBag.Product = p;
ViewBag.Factory = f;
return View();

// View
=========
var p = (product) ViewBag.Product;
var f = (factory) ViewBag.Factory;
// now you have access to p and f properties, for example:
@Html.Label(p.Name)
@Html.Label(f.Address)

Do not forgot that ViewBag is a Dynamic container and you need to Cast it to a type when you want to use its value in View

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