English 中文(简体)
页: 1
原标题:asp.net MVC3 @model in partial views

在我的控制者中,我这样做是初步的:

using mylib.product;
using mylib.factory;

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

从部分角度看,我如何用“@model”关键词做同样的事?

增 编

最佳回答

我认为,你们需要将不止一个不同类别的例子转移给观点。 如果是,我建议为此使用“观点”。 与此类似:

// 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)

不要放弃观点Bag是一个动态的集装箱,你需要把它放在一种类型上,因为你想在观点中使用其价值。

问题回答

如果你试图在你看来增加姓名/舱位,那么:

@using mylib.product;

我应该把这一模式同大家一样看待。

return View("ViewName");

and in the view;

@model Project.Namespace.Class

你们应当使用观点模式:

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)




相关问题
What do you have in your Model class?

What do you have in your model classes. Generally if i use any framework or any library (Zend Framework) , my classes only have variable which is table name . I know that in complicated applications ...

Rails Model With Aggregrate Data (not backed by a table)

Id like to create a model in rails that does not correlate to a table in the database. Instead the model should dynamically pull aggregrate data about other models. Example: I have a Restaurant ...

Trouble changing databases for my models in codeigniter

I m making a website with two different databases. Let s say one is DB1, and the other is DB2. I ve set up my database.php in the config folder, so they each have the correct host/password/username/...

EMAIL server FSP model

For my assignment I need to develop FSP model for email server and client. I manage to write simple model which describes one user, server and his mailbox, but I am having problems changing this ...

Rails Custom Model Functions

I m in a databases course and the instructor wants us to develop an e-commerce app. She said we can use any framework we like, and now that we re halfway through the semester she decided that Rails ...

热门标签