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 9233197
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T06:29:42+00:00 2026-06-18T06:29:42+00:00

My javascript function is calling my MVC 4 controller, but the parameter is always

  • 0

My javascript function is calling my MVC 4 controller, but the parameter is always null. This seems to be a common problem, and I’ve tried a few things I’ve researched, but nothing is working. Any idea why it’s always null?

My javascript GetEntries() function correctly creates an alert that shows the value:

function GetEntries(firstLetter) {
    alert(firstLetter);
    $.post('/Home/GetEntries',
           firstLetter,
           EntriesReceived());
}

My controller method breakpoint gets hit:

public void GetEntries(string firstLetter)
{
    Debug.WriteLine(firstLetter);
}

However, firstLetter is always null. I’m not sure what to do.

Failed attempts:

I tried posting using JSON.stringify.

function GetEntries(firstLetter) {
    alert(firstLetter);
    var firstLetterAsJson = JSON.stringify(firstLetter);
    $.post('/Home/GetEntries',
           { jsonData: firstLetterAsJson },
            EntriesReceived());
}

I tried adding the HttpPost attribute to my controller:

[HttpPost]
public void GetEntries(string firstLetter)
{
    Debug.WriteLine(firstLetter);
}

I tried changing the parameter name to “id” to match my route mapping:

[HttpPost]
public void GetEntries(string id)
{
    Debug.WriteLine(id);
}
  • 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-18T06:29:43+00:00Added an answer on June 18, 2026 at 6:29 am

    The following should work

    function GetEntries(firstLetter) {
        $.post('/Home/GetEntries', { firstLetter: firstLetter }, EntriesReceived);
    }
    

    Also notice how the EntriesReceived callback is passed to the $.post function as third argument. In your code you seem to be calling the function (EntriesReceived()) instead of passing it as callback. Here I assume that this function is defined like this:

    function EntriesReceived(result) {
        // handle the result of the AJAX call here
    }
    

    And if you want to send it as a JSON request you should use the $.ajax method which allows you to specify the proper request content type:

    function GetEntries(firstLetter) {
        $.ajax({
            url: '/Home/GetEntries',
            type: 'POST',
            contentType: 'application/json; charset=utf-8',
            data: JSON.stringify({ firstLetter: firstLetter }),
            success: function(result) {
                // handle the result of the AJAX call here
            }
        });
    }
    

    Another problem I see with your controller action is that you defined it as void. In ASP.NET MVC the common established convention is that all controller actions must return instances of the ActionResult class. But if you don’t want to return anything to the client then use the specific ActionResult for this case – EmptyResult:

    [HttpPost]
    public ActionResult GetEntries(string firstLetter)
    {
        Debug.WriteLine(firstLetter);
        return new EmptyResult();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is related to a question I've asked previously: Calling Javascript function after loading
I'm calling the JavaScript function onsubmit of form but form has submit before finished
Possible Duplicate: calling ASP function from javascript okay running this code : <script type=text/javascript>
Hi have always trouble with calling JavaScript function with parameters values as values bonded
Possible Duplicate: Calling dynamic function with dynamic parameters in Javascript javascript array parameter list
I am calling a Javascript function in my html page from a Flash movie
Here i am calling a javascript function on a button click and i need
I have a javascript file which has a function calling a server (url) to
I have a javascript function for checking errors which I am calling on OnClicentClick
I'm calling a utility called Highslide in a javascript function. Embedding the whole thing

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.