views:

581

answers:

4

The data on my page is such:

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

This collection of used Id's is obtained from a Listbox via jQuery on my page.

All I want to do is post this list of UserId's to my controller and display a success message somehere on my page. "Users have been updated."

I am not sure what the signature of my controller should look like and how I should compose the jQuery when I want to pass a list like the one above?

Also, I am wondering if I really need the Controller action has to be an ActionResult?

In the past I have done other posts like so:

$.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  A: 

I'm doing almost exactly the same thing you're doing, 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 is my action:

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

        return Json(true);
    }

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

JMP
What about the $.ajax({ ... what would that look like? I am an new to MVC and jQuery. Thanks
Picflight
I added my jQuery, it's very similar to yours. In your "data" property, you'd want to make sure to send both params: data: { "page" : 5, "projectId": $("#ProjectList").val() }
JMP
Also note that you could use an actual javascript array and make it map to an `IEnumerable<int>` rather than a comma-separated string, if you set the `traditional` parameter to `true` in your .ajax call.
StriplingWarrior
A: 

Greate thanks it's helpful

A: 

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

nikunj
A: 

I seldom use $.ajax because it needs a lot of setup. I am biased towards $.post which works very well for uncomplicated post requests.

Cyril Gupta