简体   繁体   English

使用Ajax将消息的状态从未读更新为已读-MVC

[英]Updating Status of Message From unread to read using Ajax - MVC

I'm trying update status of every each Message from unread to read and when i click on button of first message in the list updating status going to work,but when i click on button of second or Third message in the list updating status NOT going to work. 我正在尝试从未读到读取的每个消息的更新状态,并且当我单击列表中的第一条消息的按钮时更新状态将起作用,但是当我单击列表中的第二条或第三条消息的按钮时更新状态不起作用上班。 Can anyone please help me or point me in the right direction! 任何人都可以请我帮忙或指出正确的方向!

Thanks in advance :) 提前致谢 :)

View: 视图:

 <div class="content">
    @{ int i = 0;}
    foreach (var item in Model.Comment_Lists.GroupBy(l => l.MSG).Select(g => .First()))    {
            if (item.se == "Kunde")
            {
              <div  class="bs-callout bs-callout-success">
              <div class="col-xs-3 text-right">
              <button data-toggle="collapse"  data-target="#ShowMSG_@i" id="btnChangestu" class="btn btn-sm btn-success btn-icon"></button></div>
              <span class="text-muted"><small> @item.Date.ToString("dd/MMMM/yyy")</small></span><br />
              <span class="text-muted"><small> @item.MSGType</small></span>
              <input type="hidden" name="IDMSG" id="IDMSG" value="@item.id" />
              <input type="hidden" name="ChangeStu" id="ChangeStu" value="Read Message" />
              <div class="collapse" id="ShowMSG_@i">
                  <p><span>@item.MSG </span></p>
              </div>
              </div>
              }
                i++;
           }
         </div>

JavaScript: JavaScript的:

<script>

    $(document).ready(function () {    
        $("#btnChangestu").click(function (e) {
            e.preventDefault();    
            $.ajax({
                type: "POST",
                url: "/Account/UpdateMSGStatus",
                data: {
                    IDMSG: $("#IDMSG").val(),
                    ChangeStu: $("#ChangeStu").val()
                },
                dataType: 'json',
                success: function (result) {    
                    if (!$.trim(result)) {
                        alert("What follows is blank: ");
                    }
                    else {                            
                        result.ID = IDMSG;
                        result.MSGType = ChangeStu;    
                        console.log("Send");    
                    }    
                },
                error: function () {
                    console.log('something went wrong - debug it!');
                }
            })    
        });
    });
</script>

Controller: 控制器:

public JsonResult UpdateMSGStatus(int? IDMSG , string ChangeStu)
{
    var u = db.Besked.Where(a => a.ID == IDMSG).FirstOrDefault();
    if (u != null)
    {
        u.MSGType = ChangeStu;
        u.ID = IDMSG;
        db.SaveChanges();
    }
    return Json(u, JsonRequestBehavior.AllowGet);
}

Duplicate id attributes are invalid html, and your jQuery selectors ( IDMSG: $("#IDMSG").val() etc) will only ever select the first element with that id . 重复的id属性是无效的html,并且您的jQuery选择器( IDMSG: $("#IDMSG").val()等)将只会选择具有该id的第一个元素。

There is no reason to use hidden inputs in this case. 在这种情况下,没有理由使用隐藏的输入。 Instead, ad the values as data attributes and read then in the .click() event, and use a class name for the button instead of an id . 而是将这些值作为data属性进行广告,然后在.click()事件中进行读取,然后为按钮使用class名而不是id

Note I have only included the IDMSG , not ChangeStu since you have hard coded that value 请注意,我只包括IDMSG ,不ChangeStu既然你硬编码的价值

<button data-id="@item.id" class="btn btn-sm btn-success btn-icon edit" data-toggle="collapse" data-target="#ShowMSG_@i></button>

Then the script becomes 然后脚本变成

$('.edit.).click(function (e) {
    e.preventDefault(); 

    // read the value
    var id = $(this).data('id');

        $.ajax({
            type: "POST",
            url: '@Url.Action("UpdateMSGStatus", "Account"), // don't hard code urls
            data: {
                IDMSG: id,
                ChangeStu: 'Read Message' 
            },
            dataType: 'json',
            success: function (result) {    

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM