weixin_33736649 2014-08-19 20:16 采纳率: 0%
浏览 15

MVC Ajax jQuery复选框

------------ Updated code ---------------------

@for (int i = 0; i < Model.PeopleCheckBoxes.Count(); i++)
{

  <div class="person">
  <span>@Model.PeopleCheckBoxes.ToArray()[i].Username</span>

  @Html.CheckBoxFor(m => @Model.PeopleCheckBoxes.ToArray()[i].IsTaxExempt,
                    new { data_eventID = Model.catID, 
                          data_peopleID = Model.PeopleCheckBoxes.ToArray()[i].Id, 
                          @class = "PeopleCheckBox" }) 

   </div>

<script type="text/javascript">
    $(document).ready(function () {

        $('.PeopleCheckBox').change(function () {
            if (!$(this).is(':checked')) { // ignore checked items
                var url = '@Url.Action("RemoveMemberFromEvent2", "GiftList")';
                var eID = $(this).data('eventID');
                var pID = $(this).data('peopleID');

                $.post(url, { eventid: eID, peopleID: pid }, function (data) {
                    if (data) {
                        self.closest('.person').remove(); // remove the elements from the DOM
                    } else {
                        alert('Oops'); // the person was not removed
                    }
                });

            }
        });

  [HttpPost]
        public JsonResult RemoveMemberFromEvent2(int eventID, int peopleID) 
        {
             // database updated ..................
             return Json(true);
        }

------------ End Updated code ---------------------

-------- Not using this partial view now; added it just to see if it could be used -----

[HttpPost]
    public PartialViewResult RemoveMemberFromEvent(int eventID, int peopleID)
           {
              // removes checkbox from database :
              var aEvent = (from p in _EventMemberRepository.Table . . .
                _giftListService.DeletePersonOnGiftPage(aEvent);

                // queries database for new updated data : 
                var members2 = from c in _custRepository.Table  ....

                return PartialView("_RemoveMemberFromEvent", vModel);
            }

I want to have a Method run when a Check box is un-checked, and pass in 2 values to it. What is a good way to do it? I would like to use Ajax with jQuery but have no idea how to code it :

@foreach (var item2 in Model.PeopleCheckBoxes)    
{
 @item2.Username 

 @Html.CheckBox("chkPeople", true, new {eventID = Model.catID, peopleID =item2.Id})
 ...

     public class GiftListController : Controller
        { 

     [HttpPost] 
            public ActionResult RemoveMemberFromEvent(int eventID, int peopleID) 
            {
                var aEvent = (from p in _EventMemberRepository.Table
                ............  
                return RedirectToAction("Index");
            }        

Any ideas appreciated. Thanks

  • 写回答

2条回答 默认 最新

  • weixin_33708432 2014-08-19 20:43
    关注

    Your View will look somewhat like this:

    @Html.CheckBox("mycheckbox", new { onclick="customMethod()"})
    

    Your JavaScript method which helps make the Ajax call can go thus:

    function customMethod(){
    $.ajax({
            url: "@Url.Action("ActionMethod", "Controller")",
            type: "POST",
            async: true,
            dataType: "json",
    
            data: {
                variable1: '@Model.catID',
                variable2: '@item2.id'
            },
    
            success: function(data) {
                //If you have a response, it will be in the 'data' object
            }
        });
    }
    

    And then your Controller would take the form below:

    public string ActionMethod (int variable1, int variable2)
        {
            //Use your variables here and return your response, if any.
        }
    
    评论

报告相同问题?

悬赏问题

  • ¥60 最新ukb数据下载方法
  • ¥15 关于#python#的问题:您好可以加您一下联系方式吗,在复现的时候确实有点问题难以解决
  • ¥15 联想电脑重装系统时无法发现硬盘
  • ¥15 MATLAB与UR10e实体机械臂建立通讯
  • ¥15 c++题需要快一点不用opencv
  • ¥15 关于#java#的问题:想要咨询Flowable流程引擎框架的问题
  • ¥15 vscode里面怎么用plaformio强调串口啊
  • ¥20 针对计算后数据做一致性检验可以用Bland Altman法吗
  • ¥15 win32如何自绘编辑框的背景图片(语言-c++|操作系统-windows)
  • ¥15 微信夜间被转走了1w对,当天手机剪切板里就出现了这个乱码,有铁子可以看看是啥吗可以