久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

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

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

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

在"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)頁(yè)面沒有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)鏈接

標(biāo)簽: ASP.NET
相關(guān)文章:
主站蜘蛛池模板: 静宁县| 楚雄市| 长顺县| 东源县| 祁东县| 黄陵县| 常宁市| 郁南县| 中江县| 招远市| 偃师市| 牙克石市| 托克托县| 定西市| 广安市| 仙游县| 卓尼县| 万安县| 张家口市| 郁南县| 比如县| 建德市| 曲松县| 湖口县| 通城县| 桓仁| 霍州市| 大石桥市| 广汉市| 教育| 阿尔山市| 正镶白旗| 广宗县| 彩票| 鹤庆县| 尤溪县| 日喀则市| 佛山市| 郓城县| 侯马市| 年辖:市辖区|