2012-07-30 76 views
-3

我有一个这样的模型:模型在MVC结合3

public Products() 
{ 
    string name {get; set;} 
    string category { get; set;} 
} 

我试图操作方法中创建一个对象,或者作为一个参数传递给该方法&使用该对象来创建视图通过传递该对象来查看方法。我期待这个框架能够创建一个适当的视图来展示产品。这是否是正确的方法?我有一种感觉,我在很大程度上错过了一些东西,但无法弄清楚。 Thnaks。从MSDN

+0

可能的重复:http://stackoverflow.com/questions/5692964/asp-net-mvc-3-model-binding-resources – 2012-07-30 06:03:01

+0

我检查过,但这些答案没有帮助。 – 2012-07-30 06:07:04

+0

你可以编辑你的问题来描述你有什么具体的问题,这些资源不解释? – 2012-07-30 06:08:32

回答

0

直:http://msdn.microsoft.com/en-us/library/dd410405.aspx

主要思想是模型绑定会自动在asp.net mvc的完成。你只需要在模型传递到GET方法的观点和检索模型,像这样在POST方法的参数:

[HttpGet] 
    public ViewResult MyProducts() 
    { 
     Products model = new Products() 
     return View(model); 
    } 

    [HttpPost] 
    public ViewResult MyProducts(Products model) 
    { 
    // model.name contains the value from the view 
    // model.category contains the value from the view. 
    } 

,并在视图中,您必须在顶部@model Products和输入的字段像这样:@Html.EditorFor(m=>m.name)@Html.EditorFor(m=>m.category)

说实话。你没有搜索过网页。

+0

我真的搜查了一下,但不知何故弄糊涂了。即使我正试图读mvc上的两个重量级的PDF!谢谢 – 2012-07-30 06:16:55

+0

@Jobskuk老实说,如果你可以买一本关于asp.net mvc的书,会更好。3.我总是喜欢从书中读取,而不是从教程中读取,有时候msdn本身b/c内容是结构化的,更容易遵循。 – amb 2012-07-30 06:18:38