分享

成功mvc视图向控制器传值的3种方法

 北方天空A 2020-06-17

控制器:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.Mvc;

using templates.Models;

namespace templates.Views.test3

{

    public class test3Controller : Controller

    {

        //

        // GET: /test3/

        public ActionResult List(List<string> valuelist,string mn)

        {

            return View();

        }

        public ActionResult Add(Products product)

        {

            return View();

        }

        public ActionResult Add2(Products product)

        {

            return View();

        }

        public ActionResult List2(List<Products> valuelist)

        {

            return View();

        }

    }

}

类:

   public class Products

    {

        public int Id { get; set; }

        [DisplayName("产品名称")]

        [Required(ErrorMessage = "此项不能为空")]

        public string Name { get; set; }

        [DisplayName("产品价格")]

        [Required(ErrorMessage = "此项不能为空")]

        public string Price { get; set; }

}

视图:

添加1:

@{

    ViewBag.Title = "Add";

}

@model templates.Models.Products

<h2>Add</h2>

@using (Html.BeginForm())

{

    <div 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>

    </div>

    <div class="form-group">

        @Html.LabelFor(model => model.Price, new { @class = "control-label col-md-2" })

        <p class="col-md-10">

            @Html.EditorFor(model => model.Price)

            @Html.ValidationMessageFor(model => model.Price)

        </p>

    </div>

    <div class="form-group">

        <p class="col-md-offset-2 col-md-10">

            <input type="submit" value="提交" class="btn btn-default" />

        </p>

    </div>

}

添加2:

@{

    ViewBag.Title = "Add";

}

@model templates.Models.Products

<h2>Add2</h2>

<form>

    <div 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>

    </div>

    <div class="form-group">

        @Html.LabelFor(model => model.Price, new { @class = "control-label col-md-2" })

        <p class="col-md-10">

            @Html.EditorFor(model => model.Price)

            @Html.ValidationMessageFor(model => model.Price)

        </p>

    </div>

    <div class="form-group">

        <p class="col-md-offset-2 col-md-10">

            <input type="submit" value="提交" class="btn btn-default" />

        </p>

    </div>

</form>

列表1:

@{

    ViewBag.Title = "List";

}

<script src="~/Scripts/jquery-1.10.2.min.js"></script>

<h2>List</h2>

<script type="text/javascript">

$(function () {

      var value = ["C#", "JAVA", "PHP"];

      $("#btn1").click(function () {

        $.ajax(

          {

            url: "/test3/List",

            type: "Get",

            data: { valuelist: value,mn:"asdfdfsasdfa" },

            traditional: true, //必须设置该属性,否则控制器中获取不到值

            success: function (data) {

              alert("Success");

            }

          });

      });

    });

</script>

<input id="btn1" type="button" value="测试" />

列表2:

@{

    ViewBag.Title = "List2";

}

<script src="~/Scripts/jquery-1.10.2.min.js"></script>

<h2>List2</h2>

<script type="text/javascript">

    $(function () {

        $("#btn1").click(function () {

            alert(33);

            var promodes = [];

            promodes.push({ Id: "0", Name: "手机",Price:"2499" });

            promodes.push({ Id: "1", Name: "耳机", Price: "268" });

            promodes.push({ Id: "2", Name: "充电器",Price: "99" });

            $.ajax(

              {

                  url: "/test3/List2",

                  type: "Post",

                  data: JSON.stringify(promodes), //必须对数组进行序列化

                  contentType: "application/json", //设置contentType的值为"application/json",默认为"application/json"

                  success: function (data) {

                      alert("Success");

                  }

              });

        });

    });

</script>

<div>

    <input id="btn1" type="button" value="测试" />

</div>

    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约