将值从多个文本框传递给控制器 [英] Pass values from multiple textboxes to controller

查看:74
本文介绍了将值从多个文本框传递给控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好



在我的mvc表单中,我有很多复选框和文本框,这些复选框和文本框都是以dinamycally方式生成的。它适用于仅通过选中的复选框。但对于文本框,它传递了所有值。当我在我的控制台中调试时,我看到了数组[45,55],我的意思是它只获得了填充的文本框,但是在控制器中传递了所有值,即使是那些有0的文章。任何想法?谢谢



我尝试了什么:



我的观点



Hello

In my mvc form I have many checkboxes and textboxes which are being dinamycally generated. It works perfect for the checkboxes it passes only that are selected. But for the textboxes it passes all the values. When I debug in my console I see Array [45,55] , I mean it gets only the textboxes that are filled but in controller passes all values even those with 0. Any idea? thank you

What I have tried:

My View

@foreach (var item in Model.SubCategory.Where(x => x.CategoryID == "05"))
               {

                       <div class="row">
                           <div class="col-sm-6">
                               <div class="form-group">
                                   <input class="col-sm-1" type="checkbox" id="@item.SubCategoryID" name="Subcategory" value="@item.SubCategoryID" />

                                   <label class="col-sm-10" for="optionId">@item.SubCategoryDescription</label>

                               </div>
                           </div>
                           <div class="form-group">
                               @Html.LabelFor(model => model.Details.Ammount, htmlAttributes: new { @class = "control-label col-md-2" })
                               <div class="col-md-2">
                                   @Html.TextBoxFor(model => model.Details.Ammount, new { @class = "ammount", Name = "ammount" } )
                                   @Html.ValidationMessageFor(model => model.Details.Ammount, "", new { @class = "text-danger" })
                               </div>
                           </div>
                       </div>





我的srcipt





My srcipt

<script type="text/javascript">

        $(document).ready(function () {
            $('#postBtn').on('click', function (e) {
              
                var ammounts = [];

                $('input[name="Subcategory"]').each(function () {
                    if ($(this).is(':checked') === true) {
                        var amountField = $(this).parent().parent().next().find("input[name='ammount']");
                        if ($(amountField).val() !== '') {
                            if (parseInt($(amountField).val()) > 0) {
                                ammounts.push(parseInt($(amountField).val()));
                            }
                        }
                    }
                        
                    
                });
               
               
                console.log(ammounts);
                
                $.ajax({
                    url: '@Url.Action("Create", "Budgets")',
                    type: "POST",
                    data: JSON.stringify({
                            Subcategory: [],
                            ammount: ammounts
                        }),
                    dataType: "json",
                    traditional: true,
                    success: function () {
                        alert("ajax request to server succeed");
                    },
                    
                });
            });
        });
       
</script>





我的控制器





My controller

public JsonResult Create(IEnumerable<int> Subcategory, IEnumerable<decimal> ammount)

推荐答案

document )。ready( function (){
(document).ready(function () {


' #postBtn')。 on(' 点击'功能( e){

var ammounts = [];
('#postBtn').on('click', function (e) { var ammounts = [];


' input [name =Subcategory]')。each( function (){
if
('input[name="Subcategory"]').each(function () { if (


这篇关于将值从多个文本框传递给控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆