Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8959255
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T15:23:39+00:00 2026-06-15T15:23:39+00:00

I’m trying to post a JSON array to an MVC controller. But no matter

  • 0

I’m trying to post a JSON array to an MVC controller. But no matter what I try, everything is 0 or null.

I have this table that contains textboxes. I need from all those textboxes it’s ID and value as an object.

This is my Javascript:

$(document).ready(function () {

    $('#submitTest').click(function (e) {

        var $form = $('form');
        var trans = new Array();

        var parameters = {
            TransIDs: $("#TransID").val(),
            ItemIDs: $("#ItemID").val(),
            TypeIDs: $("#TypeID").val(),
        };
        trans.push(parameters);


        if ($form.valid()) {
            $.ajax(
                {
                    url: $form.attr('action'),
                    type: $form.attr('method'),
                    data: JSON.stringify(parameters),
                    dataType: "json",
                    contentType: "application/json; charset=utf-8",
                    success: function (result) {
                        $('#result').text(result.redirectTo)
                        if (result.Success == true) {
                            return fase;
                        }
                        else {
                            $('#Error').html(result.Html);
                        }
                    },
                    error: function (request) { alert(request.statusText) }
                });
        }
        e.preventDefault();
        return false;
    });
});

This is my view code:

<table>
        <tr>
            <th>trans</th>
            <th>Item</th>
            <th>Type</th>
        </tr>

        @foreach (var t in Model.Types.ToList())
        {
            {
            <tr>
                <td>                  
                    <input type="hidden" value="@t.TransID" id="TransID" />
                    <input type="hidden" value="@t.ItemID" id="ItemID" />
                    <input type="hidden" value="@t.TypeID" id="TypeID" />
                </td>
            </tr>
           }
        }
</table>

This is the controller im trying to receive the data to:

[HttpPost]
public ActionResult Update(CustomTypeModel ctm)
{


   return RedirectToAction("Index");
}

What am I doing wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T15:23:40+00:00Added an answer on June 15, 2026 at 3:23 pm

    There are lots of issues with your code. Let’s start with the markup. You have a table and inside each row of this table you are including hidden fields. Except that you have hardcoded the id attribute of those hidden elements meaning that you could potentially end up with multiple elements with the same id in your markup which results in invalid markup.

    So let’s start by fixing your markup first:

    @foreach (var t in Model.Types.ToList())
    {
        <tr>
            <td>                  
                <input type="hidden" value="@t.TransID" name="TransID" />
                <input type="hidden" value="@t.ItemID" name="ItemID" />
                <input type="hidden" value="@t.TypeID" name="TypeID" />
            </td>
        </tr>
    }
    

    Alright, now you have valid markup. Now let’s move on to the javascript event which will be triggered when some submitTest button is clicked. If this is the submit button of the form I would recommend you subscribing to the .submit event of the form instead of the .click event of its submit button. The reason for this is because a form could be submitted for example if the user presses the Enter key while the focus is inside some input field. In this case your click event won’t be triggered.

    So:

    $(document).ready(function () {
        $('form').submit(function () {
            // code to follow
    
            return false;
        });
    });
    

    Alright, next comes the part where you need to harvest the values of the hidden elements which are inside the table and put them into a javascript object that we will subsequently JSON serialize and send as part of the AJAX request to the server.

    Let’s go ahead:

    var parameters = [];
    // TODO: maybe you want to assign an unique id to your table element
    $('table tr').each(function() {
        var td = $('td', this);
        parameters.push({
            transId: $('input[name="TransID"]', td).val(),
            itemId: $('input[name="ItemID"]', td).val(),
            typeId: $('input[name="TypeID"]', td).val()
        });
    });
    

    So far we’ve filled our parameters, let’s send them to the server now:

    $.ajax({
        url: this.action,
        type: this.method,
        data: JSON.stringify(parameters),
        contentType: 'application/json; charset=utf-8',
        success: function (result) {
            // ...
        },
        error: function (request) { 
            // ...
        }
    });
    

    Now let’s move on to the server side. As always we start by defining a view model:

    public class MyViewModel
    {
        public string TransID { get; set; }
        public string ItemID { get; set; }
        public string TypeID { get; set; }
    }
    

    and a controller action that will take a collection of this model:

    [HttpPost]
    public ActionResult Update(IList<MyViewModel> model)
    {
        ...
    }
    

    And here’s the final client side code:

    $(function() {
        $('form').submit(function () {
            if ($(this).valid()) {
                var parameters = [];
                // TODO: maybe you want to assign an unique id to your table element
                $('table tr').each(function() {
                    var td = $('td', this);
                    parameters.push({
                        transId: $('input[name="TransID"]', td).val(),
                        itemId: $('input[name="ItemID"]', td).val(),
                        typeId: $('input[name="TypeID"]', td).val()
                    });
                });
    
                $.ajax({
                    url: this.action,
                    type: this.method,
                    data: JSON.stringify(parameters),
                    contentType: 'application/json; charset=utf-8',
                    success: function (result) {
                        // ...
                    },
                    error: function (request) { 
                        // ...
                    }
                });
            }
            return false;
        });
    });
    

    Obviously if your view model is different (you haven’t shown it in your question) you might need to adapt the code so that it matches your structure, otherwise the default model binder won’t be able to deserialize the JSON back.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
This could be a duplicate question, but I have no idea what search terms
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have an array which has BIG numbers and small numbers in it. I
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I have an MVC Razor view @{ ViewBag.Title = Index; var c = (char)146;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.