欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

ASP.NET MVC从视图传参到控制器的几种形式

程序员文章站 2023-11-25 13:40:22
1. 传递数组 $(function () { var value = ["c#", "java", "php"]; $("input...

1. 传递数组

$(function () {
      var value = ["c#", "java", "php"];
      $("input[type='button']").click(function () {
        $.ajax(
          {
            url: "/home/list",
            type: "get",
            data: { valuelist: value },
            traditional: true, //必须设置该属性,否则控制器中获取不到值
            success: function (data) {
              alert("success");
            }
          });
      });
    });
public actionresult list(list<string> valuelist)
    {
      return view();
    }

调试效果:

ASP.NET MVC从视图传参到控制器的几种形式

2. 传递单个model

@using (html.beginform())
  {
    <div class="form-group">
      @html.labelfor(model => model.name, new { @class = "control-label col-md-2" })
      <div class="col-md-10">
        @html.editorfor(model => model.name)
        @html.validationmessagefor(model => model.name)
      </div>
    </div>
    <div class="form-group">
      @html.labelfor(model => model.price, new { @class = "control-label col-md-2" })
      <div class="col-md-10">
        @html.editorfor(model => model.price)
        @html.validationmessagefor(model => model.price)
      </div>
    </div>
    <div class="form-group">
      @html.labelfor(model => model.color, new { @class = "control-label col-md-2" })
      <div class="col-md-10">
        @html.editorfor(model => model.color)
        @html.validationmessagefor(model => model.color)
      </div>
    </div>
    <div class="form-group">
      <div class="col-md-offset-2 col-md-10">
        <input type="submit" value="提交" class="btn btn-default" />
      </div>
    </div>
  }
public class products
  {
    public int id { get; set; }
    [displayname("产品名称")]
    [required(errormessage = "此项不能为空")]
    public string name { get; set; }
    [displayname("产品价格")]
    [required(errormessage = "此项不能为空")]
    public string price { get; set; }
    [displayname("产品颜色")]
    [required(errormessage = "此项不能为空")]
    public string color { get; set; }
  }
 public actionresult add(products product)
    {
      return view();
    }

 调试效果:

ASP.NET MVC从视图传参到控制器的几种形式

 3. 传递多个model

 $("input[type='submit']").click(function () {
        var promodes = [];
        promodes.push({ id: "0", name: "手机", color: "白色",price:"2499" });
        promodes.push({ id: "1", name: "耳机", color: "黑色", price: "268" });
        promodes.push({ id: "2", name: "充电器", color: "黄色",price: "99" });
        $.ajax(
          {
            url: "/home/list",
            type: "post",
            data: json.stringify(promodes), //必须对数组进行序列化
            contenttype:"application/json", //设置contenttype的值为"application/json",默认为"application/json"
            success: function (data) {
              alert("success");
            }
          });
      });
 public actionresult list(list<products> valuelist)
    {
      return view();
    }

调试效果:

ASP.NET MVC从视图传参到控制器的几种形式

以上所述是小编给大家介绍的asp.net mvc从视图传参到控制器的几种形式,希望对大家有所帮助