如何从angularjs控制器检索多个动态文本框值到c#web方法

问题描述 投票:0回答:1

Here i have textboxes that are dynamic when i add another product. I only give input into the textbox, after entering the values into the textbox and clicking on save button, the textbox values should be stored as an array into the controller. The controller should fetch the multiple textbox values and store as an array and should pass into the c# web method.

Product.aspx

这里我有一些动态的文本框,当我添加另一个产品。我只在文本框中输入输入,在将值输入文本框并单击保存按钮后,文本框值应作为数组存储到控制器中。控制器应该获取多个文本框值并存储为数组,并应传递给c#web方法。

<tr data-ng-repeat="PR in ProductList | orderBy:'productid'">
                           <th data-width="5%">
                                <h2>{{PR.productid}}</h2>   
                            </th>
                            <th data-width="15%">
                                <h2>{{PR.productname}}</h2>
                            </th>
                            <th data-width="15%">
                                 <input type="text" name="productprice" data-ng-model="productprice[$index]" value="{{PR.productprice}}"/>
                            </th>
                        </tr>
                    </tbody>
                </table>
                <div class="form-actions">
                <button type="submit" data-ng-click="Save()" class="btn btn-primary glyphicon glyphicon-plus-sign"><span>Save</span></button>
                    </div>

productcontroller.js

我试过的是,我使用ng-model productprice创建了一个文本框值的数组。我将整个数组存储到变量products2中并传递给数据。

var app = angular.module("myapp", [])
    app.controller("ProductController", function ($scope, $http) {
 $scope.Save = function () {
            $scope.productprice = [
                {
                    productprice: 'productprice'
                }
            ]
            var httpreq = {
                method: 'POST',
                url: 'Product.aspx/save',
                headers: {

                    'Content-Type': 'application/json; charset=utf-8',
                    'dataType': 'json'
                },
                data: {'products2': $scope.productprice }
            }
            $http(httpreq).then(function (data) {
                if (data = "true") {
                    alert("added successfully.");
                }
            }, function () { alert("data not added") });
        };
    });

Product.aspx.cs

在这里我尝试的是我获取“products2”,这是一个来自控制器的数组,并尝试填充c#web方法。

  1. 使用交易

当我单击“保存”按钮时,所有文本框值都应保存到具有不同行的同一列的数据库表中。假设我有一个名为productprice的列,此列应将上述文本框值逐个包含到不同的行中。

[System.Web.Services.WebMethod()]
    public static void save(decimal[] products2) //This "products2" is an array value fetched from the controller. 
    {
        decimal[] productprice = products2; //i again stored this into variable.
        using(NpgsqlConnection con = new NpgsqlConnection(connectionString))
        {
            con.Open();
            NpgsqlCommand cmd = new NpgsqlCommand();
            cmd.Connection = con;
            NpgsqlTransaction myTrans;         
            myTrans = con.BeginTransaction();
            cmd.Transaction = myTrans;
            try
            {
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = "insert into quotation_details2(productprice)" + "values (,@productprice);";      

                cmd.Parameters.AddWithValue("@productprice", productprice.ToString());
                cmd.ExecuteNonQuery();
                myTrans.Commit();
                Console.WriteLine("All records are written to database.");
            }
            catch (NpgsqlException)
            {
                myTrans.Rollback();

                Console.WriteLine("Neither record was written to database.");
            }
            finally
            {
                if (con != null)
                {
                    con.Close();
                }
            }  
        }
    }
public class ProductInfo
{
    public decimal productprice { get; set; }
}
c# arrays angularjs postgresql webmethod
1个回答
0
投票

你的控制器应该是这样的

 $scope.save = function () {

            var res1 = [];
            for (var i = 0; i < $scope.ProductList.length; i++) {
                var res = { productid: parseInt($scope.ProductList[i].productid), productprice: $scope.ProductList[i].productprice };
                res1.push(res);
            }
            console.log(res1);
            var requestInfo = { products2: res1 };
            var httpreq = {
                method: 'POST',
                url: 'Product.aspx/save',
                headers: {
                    'Content-Type': 'application/json; charset=utf-8',
                    'dataType': 'json'
                },
                data: {
                    qid: $scope.qid,
                    vendorname: $scope.vendorname,
                    from: $scope.from,
                    to: $scope.to,
                    products2: res1
                }
            }
            $http(httpreq).then(function (response) {
                swal("Added Successfully", "", "success");    // try this sweetalert        
            }).catch(function (error) {
                $.alert({
                    title: 'Alert!',
                    content: 'Not Successfull',
                    position: {
                        my: "left top", at: "left bottom", of: window
                    }
                });    
            });
        }; 

## .cs file ##

您需要修改您的cs文件

public class ProductData
{
    public int qid { get; set; }
    public string vendorname { get; set; }
    public string from { get; set; }
    public string to { get; set; }
    public List<InitialInfo> productInfo { get; set; }
}

public class InitialInfo
{
    public int qid { get; set; }    
    public int productid { get; set; }
    public string productname { get; set; }
    public string productprice { get; set; } 
}
 public static void save(int qid, string vendorname, DateTime from, DateTime to, ProductInfo[] products2)
    {

        using (NpgsqlConnection conn = new NpgsqlConnection(connectionString))
        {
            conn.Open();

            NpgsqlTransaction myTrans = conn.BeginTransaction();


            try {

            using (NpgsqlCommand cmd = new NpgsqlCommand("insert into public.quotation2(qid, vendorname, from_date, to_date)"
                                   + " values (@qid, @vendorname, @from, @to);", conn, myTrans))
            {
                cmd.CommandType = CommandType.Text;
                cmd.Parameters.AddWithValue("@qid", qid);
                cmd.Parameters.AddWithValue("@vendorname", vendorname);
                cmd.Parameters.AddWithValue("@from", from);
                cmd.Parameters.AddWithValue("@to", to);
                cmd.ExecuteNonQuery();
            }
            using (NpgsqlCommand cmd1 = new NpgsqlCommand("insert into quotation_details2(qid,productid,productprice) values(@qid,@productid,@productprice)"
                                                       , conn, myTrans)) 
            {
                cmd1.CommandType = CommandType.Text;

                cmd1.Parameters.Add(new NpgsqlParameter("@productid", NpgsqlDbType.Integer));
                cmd1.Parameters.Add(new NpgsqlParameter("@qid", qid));
                cmd1.Parameters.Add(new NpgsqlParameter("@productprice", NpgsqlDbType.Numeric));
                    for (int i = 0; i < products2.Length; i++)
                    {
                        var pID = 0; var pPrice = "";
                        pID = products2[i].productid;
                        pPrice = products2[i].productprice;
                        cmd1.Parameters[0].Value = pID;
                        cmd1.Parameters[2].Value = pPrice;
                    NpgsqlDbType.Json.ToString()).Value = products2[i].productid.ToString();
                      NpgsqlDbType.Json.ToString()).Value = products2[i].productprice.ToString();
                        cmd1.ExecuteNonQuery();
                    }
                }
                myTrans.Commit();
                Console.WriteLine("records are written to database.");
            }
            catch (NpgsqlException e)
            {
                myTrans.Rollback();
                Console.WriteLine(e.ToString());
                Console.WriteLine("Neither record was written to database.");
            }
            finally
            {
                conn.Close();
            }
        }}
© www.soinside.com 2019 - 2024. All rights reserved.