0

I need to pass json in the form of List to controller in Angular JS Post.

I have tried to send data in form of Json to get it as list in the MVC controller. But when I did it , I get only row count at MVC Controller. The variables in the list are null

AngularJS Controller

$scope.SaveTaxRange = function (){
    var formdata = $("#frmTaxSetup").serializeArray();
    var uniqueKey = { name: "AngularJS", value: "SaveTaxRange" };
    formdata.push(uniqueKey);
    var tableData = [];

    try {
        $('#tablTaxRange tbody tr').each(function (index, element) {
            //tableData = {};
            tableData.push({
                Min_amt: $(this).find('th:eq(0) input').val(),
                Max_amt: $(this).find('th:eq(1) input').val(),
                Tax_percent: $(this).find('th:eq(2) input').val(),
                Tax_type: $(this).find('th:eq(3) input').val(),

            });
            tableData.push({
                Min_amt: $(this).find('th:eq(0) input').val(),
                Max_amt: $(this).find('th:eq(1) input').val(),
                Tax_percent: $(this).find('th:eq(2) input').val(),
                Tax_type: $(this).find('th:eq(3) input').val(),

            });
        });

    } catch (e) { alert(e); }

    uniqueKey = { name: "lstTaxRangeSave", value: tableData };
    formdata.push(uniqueKey);
    var data = {};           

    $(formdata).each(function (index, obj) {
        data[obj.name] = obj.value;
    });
    $http({
            url: "/PayModule/Pay_mas_taxsetup",
            method: "Post",
            data: { "model": data }
    }).then(function(response){});
};

MVC Model

public class TaxSetupMaster
{
    public class TaxRange
    {
        public string Min_amt;
        public string Max_amt;
        public string Tax_percent;
        public string Tax_type;
        public string add;
    }
    public List<TaxRange> lstTaxRangeSave { get; set; }
}

MVC Controller

[AcceptVerbs(HttpVerbs.Get | HttpVerbs.Post)]
    public ActionResult Pay_mas_taxsetup( TaxSetupMaster model)
    {
        var a = model.lstTaxRangeSave ;//Here it shows rowcount 2 but the variables are null. Is it feasible to get the data from html? 
    }

Thanks in advance

9
  • What is 'data' here: { "model": data }? You're creating a 'tableData' array. Aside from that, you should really be using ng-model for your inputs. Commented Jul 25, 2019 at 12:57
  • @ndoes really sorry I have updated my code Please check Commented Jul 25, 2019 at 13:03
  • @ndoes does ng-model post full table as list to mvc controller? Commented Jul 25, 2019 at 13:10
  • 1
    Can you post the data in the XHR request so we can see what it looks like? Commented Jul 26, 2019 at 4:36
  • 1
    @Rob Sorry I forgot to put getter setter Now I am getting the values in MV Controller Thanks mate This is my XHR AngularJS: "SaveTaxRange" lstTaxRangeSave: Array(2) 0: Max_amt: "2" Min_amt: "1" Tax_percent: "3" Tax_type: "4" add1: "" proto: Object 1: Max_amt: "2" Min_amt: "1" Tax_percent: "3" Tax_type: "4" add1: "" proto: Object length: 2 proto: Array(0) tblTaxSetup_length: "25" proto: Object Commented Jul 26, 2019 at 5:01

1 Answer 1

1

Sorry my carelessness mates

public string Min_amt;
public string Max_amt;
public string Tax_percent;
public string Tax_type;
public string add;

yep forgot to add getter and setter

public string Min_amt { get; set; }
public string Max_amt { get; set; }
public string Tax_percent { get; set; }
public string Tax_type { get; set; }
public string add { get; set; }

Now I am getting the values in the MVC controller Thanks to everybody for spending your valuable time

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.