Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

asp.net mvc Forms Collection when submitting

Tags:

asp.net-mvc

what is the best practice for submitting forms in asp.net mvc. I have been doing code like this below but i have a feeling there is a better way. suggestions?

    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult AddNewLink(FormCollection collection_)
    {
        string url = collection_["url"].ToString();
        string description = collection_["description"].ToString();
        string tagsString = collection_["tags"].ToString();
        string[] tags = tagsString.Replace(" ","").Split(',');

        linkRepository.AddLink(url, description, tags);
like image 298
leora Avatar asked Sep 07 '09 23:09

leora


People also ask

How many ways are there to submit a form in ASP.NET MVC?

There are a total of 13 overloaded ways to use and implement @Html. BeginForm.

How do you use form collections?

Form collection is used to retrieve input elements from the controller action method. Form collection class automatically receives the data form value in controller methods in the form of key/value pair. Key and value pairs are accessed using the key name and index value.

How Stop form submit in MVC?

preventDefault() method used here stops the default action of submit button from submitting the form.


2 Answers

You can use the parameters directly; the parameters will automatically get parsed and casted to its correct type. The parameter names in the method must match the parameter names that are posted from your form.

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult AddNewLink(string url, string description, string tagsString)
{
    string[] tags = tagsString.Replace(" ","").Split(',');

    linkRepository.AddLink(url, description, tags);
}  

This generally works on more complex objects as well, as long as its properties can be set, and as long as your form keys are in the format objectName.PropertyName. If you need anything more advanced, you should look into model binders.

public class MyObject
{
    public int Id {get; set;}
    public string Text {get; set;}
}

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult AddNewLink(MyObject obj)
{
    string[] tags = obj.Text.Replace(" ","").Split(',');

    linkRepository.AddLink(url, description, tags);
}
like image 192
Blake Pettersson Avatar answered Sep 21 '22 06:09

Blake Pettersson


In my opinion, the Model Binder is cleaner. You can learn more at OdeToCode.com

Basically, You wrap your input from a FormCollection to a desirable model as well as validation.

public class LinkModelBinder : IModelBinder
{
    public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        var link = new Link();
        link.Url = GetValue<string>(bindingContext, "url");
        // ... and so on for all properties

        if (String.IsNullOrEmpty(url.Name))
        {
            bindingContext.ModelState.AddModelError("Url", "...");
        }

        return link;
    }

    private T GetValue<T>(ModelBindingContext bindingContext, string key) 
    {
        ValueProviderResult valueResult;
        bindingContext.ValueProvider.TryGetValue(key, out valueResult);            
        return (T)valueResult.ConvertTo(typeof(T));
    }  
}

In the controller

public ActionResult AddNewLink(Link link)
like image 43
Jirapong Avatar answered Sep 20 '22 06:09

Jirapong