0

ASP.NET can do some wonderful things. Is it possible to send form data as objects to the server?

For example, have a simple object:

public class MyClass{
    string a;
    string b;
}

I make a simple form:

<form>
    <input type="text" id="a" value="aaaa">
    <input type="text" id="b" value="bbbb">
</form>

How do I send everything up as object?

public void saveData(MyClass postData){

}
Bill Software Engineer
  • 7,362
  • 23
  • 91
  • 174

2 Answers2

1

Yes, this is possible. The model binder can examine the posted values and attempt to convert them to your model object.

From the documentation

A model binder in MVC provides a simple way to map posted form values to a .NET Framework type and pass the type to an action method as a parameter. Binders also give you control over the deserialization of types that are passed to action methods. Model binders are like type converters, because they can convert HTTP requests into objects that are passed to an action method. However, they also have information about the current controller context.

As far as your current code, no changes need to be made to the server other than converting your method to return an ActionResult and making sure it's in a controller, and has a route.

public ActionResult saveData(MyClass postData)
{
    Database.SaveData(postData); //or however you save to your database
    return RedirectToAction("Success");
}

Your form on the client should of course have an action that points to a URL that is mapped to your saveData action method's route. And for posting a simple object, the name attribute of the form elements should match the property names on the model object.

<form action="MyController/saveData">
    <input type="text" id="a" value="aaaa" name="a">
    <input type="text" id="b" value="bbbb" name="b">
</form>

Though using the MVC helper for BeginForm as in Alex's answer is also possible.

And your model object should have public properties so the model binder can set them:

public class MyClass
{
    public string a {get; set;}
    public string b {get; set; }
}
mason
  • 31,774
  • 10
  • 77
  • 121
  • Thanks a lot for the answer, do you know to do this using AJAX call? – Bill Software Engineer Feb 03 '16 at 02:30
  • 1
    @YongkeBillYu Sure do. But it's better if you try. If you get stuck, and cannot figure it out, then post your code in an [MCVE](http://stackoverflow.com/help/mcve) in a new question and then we can help you. – mason Feb 03 '16 at 02:32
  • 1
    The `` elements need a `name` attribute (matching the property name) otherwise no values will be posted to the controller and the model will not bind (and using the HtmlHelpers - `@Html.TextBoxFor(m => m.a)` will ensure these are added) –  Feb 03 '16 at 04:10
  • @StephenMuecke Good catch. – mason Feb 03 '16 at 04:13
  • 1
    @mason I created a new question because my PostBack is not populating the ViewModel, can you please help? http://stackoverflow.com/questions/35185255/binding-to-viewmodel-on-pjax-postback – Bill Software Engineer Feb 03 '16 at 19:01
0

Please look at How to link HTML5 form action to Controller ActionResult method in ASP.NET MVC 4

You should use name attributes in your fields or

@using(Html.BeginForm())
{
   <input type="text" id="a" name="a" value="aaaa">
   <input type="text" id="b" name="b" value="bbbb">
}

You have to send your form to ActionResult method:

public ActionResult SaveData(MyClass postData){

}

But the best practice is to use view model, for example, you can follow this instruction http://www.edandersen.com/2013/05/28/asp-net-mvc-basics-part-1-view-model-binding/

Community
  • 1
  • 1
Alex Vazhev
  • 1,363
  • 1
  • 18
  • 17