开发者

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 met开发者_JS百科hod? I want to call the UpdateEmailDistributionListFlag method on the Users controller and send a parameter userId.


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


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
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜