Asp.net MVC transmits data from the foreground to the background, including a single object, multiple objects, sets, asp. netmvc
Today, we will share with you several methods for Asp.net MVC to transfer data from the foreground to the background.
Environment: VS2013, MVC5.0 framework
1. Basic data type
We usually transmit int, string, bool, double, decimal, and other types.
Note that the parameters passed in the foreground must be consistent with those in the background Action. Otherwise, MVC cannot bind the values.
Front-end code:
// Transfer the basic data type $ ('# btn1 '). on ('click', function () {var obj = {parm1: 100, parm2: "I am a string", parm3: true, parm4: 1.23, parm5: 9.999999 }; $. getJSON ('/home/simplebasedata', obj, function (data) {alert (data );});});
Background code:
public ActionResult SimpleBaseData(int parm1, string parm2, bool parm3, double parm4, decimal parm5) { string result = string.Format("int={0},string={1},bool={2},double={3},decimal={4}", parm1, parm2, parm3, parm4, parm5); return Json(result, JsonRequestBehavior.AllowGet); }
Effect:
2. Pass a single object
Assign values to each attribute of an object in the foreground, and then receive the passed value in the Action method.
In this example, you need to pass a user object to the background.
Class:
public class UserInfo { public string UserName { get; set; } public string UserPassWord { get; set; } public string UserSex { get; set; } public string UserPhone { get; set; } }
Front-end:
// Pass one object. The background receives the value $ ('# btn2') passed by the foreground in the form of a class '). on ('click', function () {var obj = {UserName: 'zhang san', UserPassWord: 'Don't Tell You ', UserSex: 'mal', UserPhone: '200 '}; $. getJSON ('/home/getsingleobject', obj, function (data) {alert (data );});});
Background:
public ActionResult GetSingleObject(UserInfo userinfo) { string result = Newtonsoft.Json.JsonConvert.SerializeObject(userinfo); return Json(result, JsonRequestBehavior.AllowGet); }
Effect:
3. Pass multiple different objects
Sometimes multiple objects need to be transferred to the background at the same time. For example, when reporting data, you often need to fill in multiple data items, and the data in each tab is different. If it is not possible to pass a single object, we need to make some minor changes at the front-end.
In this example, the user object and address object must be passed to the background.
Class:
Create an address class
public class Address { public string Country { get; set; } public string Province { get; set; } public string City { get; set; } public string Street { get; set; } }
Front-end:
You need to set contentType: 'application/json'. The data part needs to be processed using the json. stringify () method.
$ ('# Btn3 '). on ('click', function () {var userinfoObj = {UserName: 'zhang san', UserPassWord: 'Don't Tell You ', UserSex: 'mal', UserPhone: '123 '}; var addressObj = {Country: 'China', Province: 'jiangxi ', City: 'nanchang', Street: 'xx Road, honggu tan New District '}; $. ajax ({url: '/home/GetTwoObject', type: 'post', // changing to get is invalidContentType: 'application/json', Data: JSON. stringify ({userinfo: userinfoObj, address: addressObj }),Success: function (data) {alert ("ViewModel not used:" + data );}});});
Background:
Define two parameter objects
Public ActionResult GetTwoObject (UserInfo userinfo, Address address) {string result = string. format ("{0} lives in {2} city, {1} Province", userinfo. userName, address. province, address. city); return Json (result, JsonRequestBehavior. allowGet );}
Effect:
4. Transfer object set
The idea of passing an object set is the same as that of passing multiple objects. Set contentType: 'application/json' at the front end, and then process data using the json. stringify () method.
Front-end:
// Transfer the set $ ('# btn4 '). on ('click', function () {var userList = []; for (var I = 0; I <5; I ++) {userList. push ({UserName: 'name' + I, UserPassWord: 'Password' + I, UserSex: 'gender '+ I, UserPhone: 'phone' + I});} $. ajax ({url: '/home/GetUserList', type: 'post', // The contentType: 'application/json', data: json, cannot be obtained after the get method is changed. stringify (userList), success: function (data) {alert (data );}});});
Background:
public ActionResult GetUserList(List<UserInfo> list) { string result = Newtonsoft.Json.JsonConvert.SerializeObject(list); return Json(result, JsonRequestBehavior.AllowGet); }
Effect:
Share finished, manual.