The model Binding of the front-end and back-end of MVC

Source: Internet
Author: User

1. Front-end Commit JSON string

{"id": "title": "Here is the caption", "Day": "2018-8-16", "status": 0, "arr": [{"Type": "R", "Quest": "333", "Q1": "3", "Q2": "3", "Q3 ":" 3 "},{" type ":" C "," Quest ":" 444 "," Q1 ":" 4 "," Q2 ":" 4 "," Q3 ":" 4 "},{" type ":" T "," Quest ":" 5 "}]}
            $.ajax ({                "/survey/post",                Method:"Post",                Data:json,                " Application/json ",                function  (data) {                    console.log (data.status);                }            })  

Back-end MVC controller (version<=5), if it is WEBAPI, add the [frombody] modifier parameter

    public class Surveydto    {public        string ID {get; set;}        public string Title {get; set;}        Public DateTime Day {get; set;}        public string Status {get; set;}        Public surveyitem[] Arr {get; set;}    }    public class Surveyitem {public        string type {get; set;}        public string Quest {get; set;}        public string Q1 {get; set;}        public string Q2 {get; set;}        public string Q3 {get; set;}    }        [HttpPost]        Public ActionResult Post (surveydto data)        {}

JSON with an array is usually not used directly with entityframework entities. To redefine.

2. Front-end Use form submission

function (JSON) {            if  (json.issuccess) {                           }        });

$.ajax ({
URL: "/survey/postform",
Method: "Post",
Data: "id=" + key + "&title=" + title + "&day=" + Date + "&status=" + STA + "&arr=" + json.stringify (this. Storagearr),
Success:function (data) {
Console.log (Data.status);
}
})

Back end uses FormCollection to accept parameters

        [HttpPost]        Public ActionResult Post (formcollection form)        {            var json = new {status = 0};            Return JSON (JSON);        }

  

Model Binding for the front-end and back-end of MVC

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.