国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

ASP.NET MVC前臺(tái)動(dòng)態(tài)添加文本框并在后臺(tái)使用FormCollection接收值

瀏覽:653日期:2022-06-08 13:44:26

在"MVC批量添加,增加一條記錄的同時(shí)添加N條集合屬性所對(duì)應(yīng)的個(gè)體"中,對(duì)于前臺(tái)傳來(lái)的多個(gè)TextBox值,在控制器方法中通過(guò)強(qiáng)類型來(lái)接收。使用FormCollection也可以接收來(lái)自前臺(tái)的多個(gè)TextBox值。實(shí)現(xiàn)效果如下:

動(dòng)態(tài)添加TextBox:

后臺(tái)使用FormCollection接收來(lái)自前臺(tái)的TextBox值,再以TempData把接收到的值返回:

當(dāng)頁(yè)面沒(méi)有TextBox,點(diǎn)擊"移除",提示"沒(méi)有文本框可被移除":

在HomeController中,先獲取前臺(tái)用來(lá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中,通過(guò)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ù)量賦值到用來(lái)計(jì)數(shù)隱藏域$("#TextBoxCount").val(newCount);//把焦點(diǎn)轉(zhuǎn)移到新添加的文本框中來(lái)$("#textbox" + newCount).focus();    });    //點(diǎn)擊移除    $("#remove").click(function() {//從隱藏域中獲取當(dāng)前文本框的數(shù)量var currentCount = parseInt($("#TextBoxCount").val(), 10);if (currentCount == 0) {    alert("已經(jīng)沒(méi)有文本框可以被移除了~~");    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)鏈接

標(biāo)簽: ASP.NET
相關(guān)文章:
主站蜘蛛池模板: 乳山市| 安丘市| 桑植县| 莲花县| 济阳县| 巩义市| 新晃| 平南县| 进贤县| 会同县| 安多县| 宕昌县| 崇仁县| 泗洪县| 昌江| 灌云县| 湄潭县| 颍上县| 太康县| 琼结县| 广河县| 介休市| 华亭县| 黄梅县| 漾濞| 大同市| 蒙山县| 绿春县| 巨鹿县| 清丰县| 连南| 海盐县| 凤庆县| 玉田县| 天门市| 马边| 永修县| 眉山市| 涟水县| 元江| 肇源县|