How to send data using jQuery and MVC

The data on my page is as follows:

var userIdCol = '3,67,78,37,87';

      

This used id collection is sourced from the Listbox via jQuery on my page.

All I want to do is post this UserId list on my controller and display a success message on my page. "Users updated".

I'm not sure what my controller signature should look like and how should I link jQuery when I want to pass a list like the one above?

Also, I'm wondering if I really need the Controller action to be an ActionResult?

I've done other posts in the past such as:

$.ajax({
 type: "POST",
 url: "/Issue/" + "Index",
 dataType: "html",
 data: {
 //page: 5
 projectId: $("#ProjectList").val()
 },
 success: function(v) {
 RefreshComment(v);
 },
 error: function(v, x, w) {
 //Error
 }
});

public ActionResult Index(int? page, int? projectId)
{
 //
 return View();
}

      

+2


source to share


3 answers


I do pretty much the same as you do, here is my jQuery:

    function saveConfigItemChanges() {
        var formData = $("form:1").serialize();
        $.ajax({
            "dataType":"json",
            "type":"POST",
            "url": "/Admin/PutValidationRules",
            "data":formData,
            "success":saveConfigItemChangesCallback
        });
    }

      

And here's my action:



    [AcceptVerbs(HttpVerbs.Post)]
    public JsonResult PutValidationRules(ConfigItem model)
    {
        Dao.SaveConfigItem(model);

        return Json(true);
    }

      

As you can see, you can return JsonResult and pass Json (true) to your success callback.

+3


source


You can send and receive data using jquery ajax request.You can post data using post, get and json here is full explanation and source code check http://my-source-codes.blogspot.com/2010/10/php- jquery-ajax-post-example.html



+1


source


I rarely use $ .ajax because it needs a lot of customization. I am inclined towards $ .post, which works very well for uncomplicated post requests.

0


source







All Articles