arame3333
arame3333

Reputation: 10193

Help needed with syntax over how to make a database update from javascript

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.

Upvotes: 0

Views: 58

Answers (2)

BritishDeveloper
BritishDeveloper

Reputation: 13359

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
}

Upvotes: 1

Yakimych
Yakimych

Reputation: 17752

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

Upvotes: 1

Related Questions