這篇文章給大家分享的是有關(guān).NET MVC從視圖傳參到控制器的形式有幾種的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過來看看吧。
成都創(chuàng)新互聯(lián)主要為客戶提供服務(wù)項(xiàng)目涵蓋了網(wǎng)頁視覺設(shè)計(jì)、VI標(biāo)志設(shè)計(jì)、全網(wǎng)整合營銷推廣、網(wǎng)站程序開發(fā)、HTML5響應(yīng)式成都網(wǎng)站建設(shè)、手機(jī)網(wǎng)站制作、微商城、網(wǎng)站托管及成都網(wǎng)站維護(hù)公司、WEB系統(tǒng)開發(fā)、域名注冊(cè)、國內(nèi)外服務(wù)器租用、視頻、平面設(shè)計(jì)、SEO優(yōu)化排名。設(shè)計(jì)、前端、后端三個(gè)建站步驟的完善服務(wù)體系。一人跟蹤測(cè)試的建站服務(wù)標(biāo)準(zhǔn)。已經(jīng)為人造霧行業(yè)客戶提供了網(wǎng)站設(shè)計(jì)服務(wù)。
1. 傳遞數(shù)組
$(function () { var value = ["C#", "JAVA", "PHP"]; $("input[type='button']").click(function () { $.ajax( { url: "/Home/List", type: "Get", data: { valuelist: value }, traditional: true, //必須設(shè)置該屬性,否則控制器中獲取不到值 success: function (data) { alert("Success"); } }); }); }); public ActionResult List(Listvaluelist) { return View(); }
調(diào)試效果:
2. 傳遞單個(gè)Model
@using (Html.BeginForm()) {@Html.LabelFor(model => model.Name, new { @class = "control-label col-md-2" })
@Html.EditorFor(model => model.Name) @Html.ValidationMessageFor(model => model.Name)
@Html.LabelFor(model => model.Price, new { @class = "control-label col-md-2" })
@Html.EditorFor(model => model.Price) @Html.ValidationMessageFor(model => model.Price)
@Html.LabelFor(model => model.Color, new { @class = "control-label col-md-2" })
@Html.EditorFor(model => model.Color) @Html.ValidationMessageFor(model => model.Color)
}
public class Products { public int Id { get; set; } [DisplayName("產(chǎn)品名稱")] [Required(ErrorMessage = "此項(xiàng)不能為空")] public string Name { get; set; } [DisplayName("產(chǎn)品價(jià)格")] [Required(ErrorMessage = "此項(xiàng)不能為空")] public string Price { get; set; } [DisplayName("產(chǎn)品顏色")] [Required(ErrorMessage = "此項(xiàng)不能為空")] public string Color { get; set; } } public ActionResult Add(Products product) { return View(); }
調(diào)試效果:
3. 傳遞多個(gè)Model
$("input[type='submit']").click(function () { var promodes = []; promodes.push({ Id: "0", Name: "手機(jī)", Color: "白色",Price:"2499" }); promodes.push({ Id: "1", Name: "耳機(jī)", Color: "黑色", Price: "268" }); promodes.push({ Id: "2", Name: "充電器", Color: "黃色",Price: "99" }); $.ajax( { url: "/Home/List", type: "Post", data: JSON.stringify(promodes), //必須對(duì)數(shù)組進(jìn)行序列化 contentType:"application/json", //設(shè)置contentType的值為"application/json",默認(rèn)為"application/json" success: function (data) { alert("Success"); } }); });
public ActionResult List(Listvaluelist) { return View(); }
調(diào)試效果:
感謝各位的閱讀!關(guān)于“.NET MVC從視圖傳參到控制器的形式有幾種”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!