ASP.NET MVC前臺(tái)動(dòng)態(tài)添加文本框并在后臺(tái)使用FormCollection接收值
在"MVC批量添加,增加一條記錄的同時(shí)添加N條集合屬性所對(duì)應(yīng)的個(gè)體"中,對(duì)于前臺(tái)傳來的多個(gè)TextBox值,在控制器方法中通過強(qiáng)類型來接收。使用FormCollection也可以接收來自前臺(tái)的多個(gè)TextBox值。實(shí)現(xiàn)效果如下:
動(dòng)態(tài)添加TextBox:
后臺(tái)使用FormCollection接收來自前臺(tái)的TextBox值,再以TempData把接收到的值返回:
當(dāng)頁面沒有TextBox,點(diǎn)擊"移除",提示"沒有文本框可被移除":
在HomeController中,先獲取前臺(tái)用來計(jì)數(shù)的隱藏域的值,然后遍歷,根據(jù)前臺(tái)Input的name屬性值的命名規(guī)則獲取到每個(gè)TextBox的值。
public class HomeController : Controller {public ActionResult Index(){ return View();}[HttpPost]public ActionResult Index(FormCollection collection){ var inputCount = 0; //前端文本框的數(shù)量 var inputValues = new List<string>();//前端文本款的值放到這個(gè)集合 if (int.TryParse(collection["TextBoxCount"], out inputCount)) {for (int i = 1; i <= inputCount; i++){ if (!string.IsNullOrEmpty(collection["textbox" + i])) {inputValues.Add(collection["textbox" + i]); }} } TempData["InputResult"] = inputValues; return View();} }
在Home/Index.cshtml中,通過jquery添加或移除TextBox。
@{ ViewBag.Title = "Index"; Layout = "~/Views/Shared/_Layout.cshtml";}<div> @if (TempData["InputResult"] != null) {<ul> @foreach (var item in (List<string>) TempData["InputResult"]) {<li>@item</li> }</ul> }</div>@using (Html.BeginForm("Index", "Home", FormMethod.Post)){ <div><div id="TextBoxesGroup"> <input type="text" id="textbox1" name="textbox1"/></div><hr/>@Html.Hidden("TextBoxCount", 1)<input type="button" value="添加" id="add"/><input type="button" value="移除" id="remove"/><input type="submit" value="提交"/> </div>}@section scripts{ <script type="text/javascript">$(document).ready(function() { //默認(rèn)焦點(diǎn) $("#textbox1").focus(); //點(diǎn)擊添加 $("#add").click(function() {//從隱藏域中獲取當(dāng)前文本框的數(shù)量var currentCount = parseInt($("#TextBoxCount").val(), 10);//文本框數(shù)量加1var newCount = currentCount + 1;//創(chuàng)建新的文本框var newInput = $(document.createElement("Input")).attr({ "type": "text", "id": "textbox" + newCount, "name": "textbox" + newCount});//把新的文本框附加到區(qū)域中$("#TextBoxesGroup").append(newInput);//把當(dāng)前文本框的數(shù)量賦值到用來計(jì)數(shù)隱藏域$("#TextBoxCount").val(newCount);//把焦點(diǎn)轉(zhuǎn)移到新添加的文本框中來$("#textbox" + newCount).focus(); }); //點(diǎn)擊移除 $("#remove").click(function() {//從隱藏域中獲取當(dāng)前文本框的數(shù)量var currentCount = parseInt($("#TextBoxCount").val(), 10);if (currentCount == 0) { alert("已經(jīng)沒有文本框可以被移除了~~"); return false;}//移除當(dāng)前文本框$("#textbox" + currentCount).remove();//把新的文本框計(jì)數(shù)賦值給隱藏域var newCount = currentCount - 1;$("#TextBoxCount").val(newCount); });}); </script>}
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)的支持。如果你想了解更多相關(guān)內(nèi)容請(qǐng)查看下面相關(guān)鏈接
相關(guān)文章:
1. ASP.NET MVC通過勾選checkbox更改select的內(nèi)容2. ASP.NET MVC視圖頁使用jQuery傳遞異步數(shù)據(jù)的幾種方式詳解3. ASP.NET MVC實(shí)現(xiàn)橫向展示購物車4. ASP.NET MVC使用JSAjaxFileUploader插件實(shí)現(xiàn)單文件上傳5. ASP.NET MVC限制同一個(gè)IP地址單位時(shí)間間隔內(nèi)的請(qǐng)求次數(shù)6. ASP.NET MVC增加一條記錄同時(shí)添加N條集合屬性所對(duì)應(yīng)的個(gè)體7. ASP.NET MVC使用異步Action的方法8. ASP.NET MVC使用Boostrap實(shí)現(xiàn)產(chǎn)品展示、查詢、排序、分頁9. ASP.NET MVC實(shí)現(xiàn)區(qū)域或城市選擇10. ASP.NET MVC實(shí)現(xiàn)單個(gè)圖片上傳、限制圖片格式與大小并在服務(wù)端裁剪圖片
