c# - Post form data to server as object -


asp.net can wonderful things. possible send form data objects server?

for example, have simple object:

public class myclass{     string a;     string b; } 

i make simple form:

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

how send object?

public void savedata(myclass postdata){  } 

yes, possible. model binder can examine posted values , attempt convert them model object.

from the documentation

a model binder in mvc provides simple way map posted form values .net framework type , pass type action method parameter. binders give control on deserialization of types passed action methods. model binders type converters, because can convert http requests objects passed action method. however, have information current controller context.

as far current code, no changes need made server other converting method return actionresult , making sure it's in controller, , has route.

public actionresult savedata(myclass postdata) {     database.savedata(postdata); //or save database     return redirecttoaction("success"); } 

your form on client should of course have action points url mapped savedata action method's route. , posting simple object, name attribute of form elements should match property names on 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 mvc helper beginform in alex's answer possible.

and model object should have public properties model binder can set them:

public class myclass {     public string {get; set;}     public string b {get; set; } } 

Comments

Popular posts from this blog

Delphi XE2 Indy10 udp client-server interchange using SendBuffer-ReceiveBuffer -

Qt ActiveX WMI QAxBase::dynamicCallHelper: ItemIndex(int): No such property in -

Enable autocomplete or intellisense in Atom editor for PHP -