3 forms of. NET MVC from view to controller

Source: Internet
Author: User
This article mainly introduces the ASP. NET MVC from the view to the controller of several forms, very good, with reference value, the need for friends can refer to the following

1. Passing arrays


$ (function () {      var value = ["C #", "JAVA", "PHP"];      $ ("input[type= ' button ']"). Click (function () {        $.ajax (          {            URL: "/home/list",            Type: "Get",            data: { Valuelist:value},            traditional:true,//Must be set for this property, otherwise the controller will not get the value            success:function (data) {              alert ("Success") ;            }          });      });    }); Public ActionResult List (list<string> valuelist)    {      return View ();    }

Debug Effect:

2. Passing a single model


@using (Html.BeginForm ()) {<p class= "Form-group" > @Html. labelfor (model = model. Name, new {@class = "Control-label col-md-2"}) <p class= "col-md-10" > @Html. editorfor (model = model . Name) @Html. validationmessagefor (model = model. Name) </p> </p> <p class= "Form-group" > @Html. labelfor (model = model. Price, new {@class = "Control-label col-md-2"}) <p class= "col-md-10" > @Html. editorfor (model = mode L.price) @Html. validationmessagefor (model = model. Price) </p> </p> <p class= "Form-group" > @Html. labelfor (model = model. Color, new {@class = "Control-label col-md-2"}) <p class= "col-md-10" > @Html. editorfor (model = mode L.color) @Html. validationmessagefor (model = model.        Color) </p> </p> <p class= "Form-group" > <p class= "col-md-offset-2 col-md-10" > <input type= "Submit "value=" Submission "class=" btn btn-default "/> </p> </p>" 


public class Products  {public    int Id {get; set;}    [DisplayName ("Product name")]    [Required (errormessage = "This entry cannot be empty")]    public string Name {get; set;}    [DisplayName ("Product price")]    [Required (errormessage = "This entry cannot be empty")]    public string Price {get; set;}    [DisplayName ("Product Color")]    [Required (errormessage = "This entry cannot be empty")]    public string Color {get; set;}  } Public ActionResult ADD (products product)    {      return View ();    }

Debug Effect:

3. Passing Multiple model


$ ("input[type= ' Submit ']"). Click (function () {        var promodes = [];        Promodes.push ({Id: "0", Name: "Phone", Color: "White", Price: "2499"});        Promodes.push ({Id: "1", Name: "Headset", Color: "Black", Price: "268"});        Promodes.push ({Id: "2", Name: "Charger", Color: "Yellow", Price: ","});        $.ajax (          {            URL: "/home/list",            Type: "Post",            data:JSON.stringify (promodes),//Must be serialized            on an array ContentType: "Application/json",//Set the value of ContentType to "Application/json", Default to "Application/json"            success: function (data) {              alert ("Success");});})      ;


Public ActionResult List (list<products> valuelist)    {      return View ();    }

Debug Effect:

Related Article

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.