0

I went through many posts but none exactly targeted a Web API Controller in the MVC Framework so I had to write a post regarding this.

I am using C# and my controller action looks something like this:

//[HttpGet]/[HttpPost]/etc.. anything can be here
    public IEnumerable<something> Customers()
    {
     //Code
        return List;
    }

And my script in my cshtml view file looks something like below:

@section scripts
{
<script type="text/javascript">

</script>
}

Now if I want to call a jquery function and/or pass some data to it as well on the client side from my C# code, what would my action and jquery code look like?

1 Answer 1

1

Your MVC controller should look like similar to this...

public class ExampleController
{

    [HttpGet]
    public ActionResult Customers(string nameParameter)
    {
        //Code
        return Json(nameParameter);
    }
}

You need to define the accepted parameters in the functions constructor.

Your Ajax call on the other hand...

$.ajax({
  url: "/Example/Customers?nameParameter=Asbah",
  success: function(html){
    // Returned value
  }
});

The parameters need to match the names you defined in your function constructor. Note in the URL /Example/ it refers to ExampleController.

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.