-3

I have a model like this:

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

I was trying to create an object within an action method or to pass as a parameter to that method & use that object to create the view by passing that object to view method. I was expecting the framework to create an appropriate view to show the products. Is it the right way to do this? I have a feeling I'm missing something in between heavily, but can't figure it out. Thnaks.

Badhon Jain
  • 197
  • 1
  • 12

1 Answers1

0

Straight from MSDN: http://msdn.microsoft.com/en-us/library/dd410405.aspx

The main idea is model binding is done automatically in asp.net mvc. You only need to pass the model to the view for the get method and retrieve the model as a parameter in the post method like so:

    [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.
    }

and in the view you must have at the top @model Products and input the fields like so: @Html.EditorFor(m=>m.name) and @Html.EditorFor(m=>m.category)

Be honest. You did not searched the web.

amb
  • 1,599
  • 2
  • 11
  • 18
  • I really searched, but somehow got confused. Even I was trying to read two heavyweight pdf on mvc!! thanks – Badhon Jain Jul 30 '12 at 06:16
  • @Jobskuk Honestly it's better if you can buy a book about asp.net mvc 3. I always prefer to read from a book than from tutorials and sometimes the msdn itself b/c the content is structured and easier to follow. – amb Jul 30 '12 at 06:18