ASP.NET MVC之从视图传递数据到控制器
1.数据存储模型Model:此方式未用到数据存储模型Model,仅简单的字符串string型数据传递
前台接收显示数据视图View:1 11 12"height:300px; width:100%"> 13 14"margin-left:100px;margin-top:50px;"> 15 16 "testData" type="text" style="width:200px;" />21 22
17 18 "submitButton" type="button" style="height: 25px; width: 75px;margin-top:35px;" value="submit"> 19 20
后台处理数据控制器Controller:
1 public class TransportDataController : Controller 2 { 3 // 4 // GET: /TransportData/ 5 6 public ActionResult Index() 7 { 8 9 return View(); 10 11 } 12 13 public string GetFrontViewData(string frontViewData) 14 { 15 16 //handle frontViewData code 17 18 return frontViewData; 19 20 } 21 }
2.数据存储模型Model:此方式用到数据存储模型Model
1 public class Model 2 { 3 4 public string rtoNumber { set; get; } 5 6 public string approver { set; get; } 7 8 public string modifier { set; get; } 9 10 public string comment { set; get; } 11 12 }
前台接收显示数据视图View:
1"container"> 2 3"table"> 4 5
18 19 "submit" type="button" value="submit"/> 20 216 7 16 17"rtoNumber" /> 8 9"approver" /> 10 11"modifier" /> 12 1314 15
1
后台处理数据控制器Controller:
1 public class TransportModelDataController : Controller 2 { 3 // 4 // GET: /TransportModelData/ 5 6 public ActionResult Index() 7 { 8 return View(); 9 } 10 11 public ActionResult getModelInfo(Listmodel) 12 { 13 14 string rtoNumber = model[0].rtoNumber; 15 16 string modifier = model[0].modifier; 17 18 string comment = model[0].comment; 19 20 string approver = model[0].approver; 21 22 return Content(""); 23 } 24 }
3.传递数组到后台
1 //前台请求 2 $(function () { 3 var value = ["C#", "JAVA", "PHP"]; 4 $("input[type='button']").click(function () { 5 $.ajax( 6 { 7 url: "/Home/List", 8 type: "Get", 9 data: { valuelist: value }, 10 traditional: true, //必须设置该属性,否则控制器中获取不到值 11 success: function (data) { 12 alert("Success"); 13 } 14 }); 15 }); 16 17 }); 18 19 //后台代码 20 public ActionResult List(List<string> valuelist) 21 { 22 return View(); 23 }
4. 传递单个Model
1 // 前台code 2 @using (Html.BeginForm()) 3 { 4class="form-group"> 5 @Html.LabelFor(model => model.Name, new { @class = "control-label col-md-2" }) 611 12class="col-md-10"> 7 @Html.EditorFor(model => model.Name) 8 @Html.ValidationMessageFor(model => model.Name) 910class="form-group"> 13 @Html.LabelFor(model => model.Price, new { @class = "control-label col-md-2" }) 1419 20class="col-md-10"> 15 @Html.EditorFor(model => model.Price) 16 @Html.ValidationMessageFor(model => model.Price) 1718class="form-group"> 21 @Html.LabelFor(model => model.Color, new { @class = "control-label col-md-2" }) 2227 28class="col-md-10"> 23 @Html.EditorFor(model => model.Color) 24 @Html.ValidationMessageFor(model => model.Color) 2526class="form-group"> 2933 34 } 35 36 //实体code 37 public class Products 38 { 39 40 public int Id { get; set; } 41 42 [DisplayName("产品名称")] 43 [Required(ErrorMessage = "此项不能为空")] 44 public string Name { get; set; } 45 46 47 [DisplayName("产品价格")] 48 [Required(ErrorMessage = "此项不能为空")] 49 public string Price { get; set; } 50 51 [DisplayName("产品颜色")] 52 [Required(ErrorMessage = "此项不能为空")] 53 public string Color { get; set; } 54 55 } 56 57 //后台code 58 public ActionResult Add(Products product) 59 { 60 return View(); 61 }class="col-md-offset-2 col-md-10"> 30 "submit" value="提交" class="btn btn-default" /> 3132
5. 传递多个Model
//前台code $("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"); } }); }); //后台code public ActionResult List(Listvaluelist) { return View(); }