Next, we will write the “Page loaded” event handler, which is set by the button click handler.< input type ="button" id ="getPeople" value ="Get People" /> < ul id ="people_list" > * This source code was highlighted with Source Code Highlighter .
< input type ="button" id ="getPeople" value ="Get People" /> < ul id ="people_list" > * This source code was highlighted with Source Code Highlighter .
< input type ="button" id ="getPeople" value ="Get People" /> < ul id ="people_list" > * This source code was highlighted with Source Code Highlighter .
< input type ="button" id ="getPeople" value ="Get People" /> < ul id ="people_list" > * This source code was highlighted with Source Code Highlighter .
When the button is pressed, we initialize the request for JSON data from the path / Home / People, which is mapped to the People method of the Home controller.* This source code was highlighted with Source Code Highlighter .
- $ ( function () {
- $ ( "#getPeople" ) .click ( function () {
- $ .getJSON ( "/ Home / People" , null , getPeople);
- });
- });
All we are doing here is getting a list of people from the database (using LINQ to SQL) and returning them as JSON. When the response is received by the browser, the getPeople function is called, as we specified in the getJSON method. Here is the getPeople function code:* This source code was highlighted with Source Code Highlighter .
- [AcceptVerbs (HttpVerbs.Get)]
- public ActionResult People ()
- {
- var db = new DataClasses1DataContext ();
- return Json (db.Persons);
- }
The data received from the server comes into it as a parameter people. All we need to do is iterate through the collection and for each of its elements add the <li> tag to our list of people. As you can see, the interaction of ASP.NET MVC and jQuery is implemented very simply - no manual conversion of data from C # objects into JavaScript objects was done.* This source code was highlighted with Source Code Highlighter .
- function getPeople (people) {
- $ ( "#people_list" ) .text ( "" );
- $ .each (people, function (i) {
- $ ( "#people_list" ) .append ( "<li>" + this .Name + "</ li>" );
- });
- }
And another button click handler.* This source code was highlighted with Source Code Highlighter .
- < label > Name < input type = "text" id = "name" /> </ label > <br />
- < input type = "button" id = "addPerson" value = "Add Person" />
This time we used the useful post function to send our newly created JSON object to / Home / People using the POST method. Since the POST method is used, this request will be caught by our overloaded People method with the AcceptVerbs [HttpVerbs.Post] attribute.* This source code was highlighted with Source Code Highlighter .
- $ ( "#addPerson" ) .click ( function () {
- var name = $ ( "#name" ) [0] .value;
- if (name == "" ) {
- alert ( "You must provide a name" );
- return ;
- }
- var person = {Name: name};
- $ .post ( "/ Home / People" , person, null , "json" );
- });
All we have to do on the server to get the data from the request is to call the parameter of this method as well as the field of our JSON object, that is, Name. ASP.NET MVC will automatically find the match and give us the necessary data. If you need to use a complex JSON object, then you can deserialize it using built-in databinding. It remains only to create an object for the database and save it.* This source code was highlighted with Source Code Highlighter .
- [AcceptVerbs (HttpVerbs.Post)]
- public ActionResult People ( string Name)
- {
- var db = new DataClasses1DataContext ();
- var person = new Person {Name = Name};
- db.Persons.InsertOnSubmit (person);
- db.SubmitChanges ();
- return null ;
- }
Source: https://habr.com/ru/post/43839/
All Articles