tags:

views:

65

answers:

4

Hi, i have the sql server database , that is having the table User. I want to create the user through the asp.net MVC application. I have the application based on asp.net MVC 2 version.I have added the controller as UsersController, also creates the view as Users. The UsersController having the methods Create, Index,delete..so on.. To take the UI inputs what I have to be do? and if there generates the Create method as taking the parameters FormCollection (overloads) , then How to execute this method ?

A: 

Here's one approach. Add a submit button to your form:

<button type="submit" value="Submit">ButtonText</button>

or

<input type="submit" value="ButtonText" />

Create an overload of the Create method:

    [HttpGet]
    public ActionResult Create ()
    {
        ...
    }

    [HttpPost]
    public ActionResult Create(FormCollection collection)
    {
        ...
    }

The attributes (HttpGetAttribute and HttpPostAttribute) let the framework know the first method should be invoked for a get request and the second one for a post request.

Now getting the values in the FormCollection is pretty straight forward:

var value = collection["key"];
Jeroen
`HttpGet` attribute can easily be omitted if there's only two actions with the same name (hence only one having an action selector attribute on them - namely `HttpPost`). But of you have multiple actions with various action selectors (like two for anonymous users and two for authenticated) then all of them should have these attributes on them.
Robert Koritnik
A: 

You don't call the Create method directly. The Create View will submit a HttpPost to the server and ASP.NET will forward the request on to the Create method that has the HttpPost attribute.

Here is a link to the execution process in MVC2 There are also plenty of other tutorials etc on that site (www.asp.net/mvc)

Barry
A: 

the

[HttpPost]
public ActionResult Create(FormCollection abc)

will be called when you submit your form.

ajay_whiz
+1  A: 

MVC actions are run according to your route registrations.

For example:

routes.MapRoute(
   "Default", // Route name
   "{controller}/{action}/{id}", // URL with parameters
   new {controller = "Home", action = "Index", id = UrlParameter.Optional} // Parameter defaults
);

In the case of the default route that comes with MVC, if you access /Users/Create/ in the browser, MVC routes this to the Users controller and then runs the Create() action. Id will only be taken into account if you do something like /Users/Edit/1234.

Your actions can make use of the [HttpPost] attribute to indicate that it receives data from an incoming Http POST. You can then do anything you want with the data (persist to database etc.):

[HttpPost]
public ActionResult Create(FormCollection form)
{
    /* do something to form values */
}

Going over concept of routes and controllers (as per ASP.NET MVC) would require a tutorial on its own. Why don't you try going through the Nerddinner project? Nerddiner is an open source ASP.NET MVC project that you can go through to understand the concepts and implementation specifics of ASP.NET MVC.

zulkamal