tags:

views:

26

answers:

2

I am trying to get my head around MVC at the moment. What I want to do is this;

        function TestClickFunc(userId) {

            $.post("/Users/UpdateEmailDistributionListFlag", { id: userId });
        }

However I cannot use JQuery directly in a javascript method like this. I have been hunting around for examples, but now I need to refer to you. How do I ready JQuery for this Javascript method? I want to call the UpdateEmailDistributionListFlag method on the Users controller and send a parameter userId.

+1  A: 

Dont use onclick="xxx". Horrible! Let jQuery set it all up:

$(function(){
  $('input:checkbox').change(function() {
    var userId = $(this).attr('userId');
    $.post("/Users/UpdateEmailDistributionListFlag", { id: userId });
  });
});

This will post a request to your action when the checkbox is clicked. I don't know exactly how you DOM works and I made that up on the spot just to give you the gist. By the way you sound like someone who could do with some jQuery IntelliSense (aren't we all?!)

This is assuming you use something like:

<%: Html.CheckBoxFor(x => x.EmailDistributionListFlag, new { userId = Model.UserId })%>

I take it you have an action like this ready and waiting:

[HttpPost]
public void UpdateEmailDistributionListFlag(int userId)
{
  //log something
}
BritishDeveloper
You are right about the Method being in place on the UsersController.The Javascript method is called from the click event on a checkbox. This checkbox is rendered in a partial view within a foreach loop, hence we have a grid and this is why I have done it like this;<%: Html.CheckBoxFor(x => x.EmailDistributionListFlag, new { id = "checkBoxEmailDistributionListFlag", onclick="TestClickFunc("+ Model.UserId + ")" })%>
arame3333
It should be simple to resolve, I get $ is not defined in FireBug. Now I am picking up the CDN for JQuery in my master page; <script src="http://ajax.microsoft.com/ajax/jquery-1.4.1.min.js" type="text/javascript"></script>However I am sure there is something obvious that needs changing.
arame3333
ive updated....
BritishDeveloper
Great advice about replacing onclick! Much rather use JQuery.
arame3333
+1  A: 

The link to the js file is broken (http://ajax.microsoft.com/ajax/jquery-1.4.1.min.js), so you actually don't have jQuery included. Try this one instead: http://code.jquery.com/jquery-1.4.1.min.js

Yakimych