1

これは私のjson関数です

  function getMessage(userId) {
                    $.ajax(
                         {

                             url: "@Url.Action("_getMessage")",
                             //url: "/Message/_getMessage",
                             type: "POST",
                             dataType: "json",
                             traditional: true,
                             data: { "recieverUsersId": userId },
                             success: function (response) {
                                 //alert("clicked");
                                 //loop the data.. how do I loop json?
                                 var div = $('#msg');
                                 var recieverImgDiv = $('.reciever')
                                 div.html("");
                                 div.html(response);
                                 alert('Success');
                         },
                         error: function (req, status, error) {
                             var div = $('#comment');
                             div.html("");
                             div.append('Error');
                             alert('Error');
                         }
                     });
         }

これが私のコントローラーです

 public ActionResult _getMessage(long recieverUsersId)
        {
            ViewBag.sentTime = "";

            long userId = WebSecurity.GetUserId(User.Identity.Name);

            var model = new ListModelForUserDetail();
            model.userDetails = db.UserInfos.Where(x => x.UserID == userId).ToList();
            model.messages = db.Messages.Where(x => x.SenderUserId == userId && x.MultipleReceiversId == recieverUsersId || x.SenderUserId == recieverUsersId && x.MultipleReceiversId == userId).ToList();

            var searchs = from m in db.MessageThreads select m;
            searchs = searchs.Where(x => x.MessageStarterUserId == 1);


            ViewBag.model = searchs.ToString();

            return PartialView("_getMessage", model);
        }

これは、アクションメソッドをレンダリングしているメインビューです

 @{Html.RenderAction("_getMessage", new {recieverUsersId=0});}

部分ビューは次のように記述されます

@foreach (var item in Model.messages)
{
   if (@item.SenderUserId != ViewBag.loginUserId && @item.FileAttachedURL == "") {
                                <li id="reply" class="bg-color-white"><b class="sticker sticker-right sticker-color-white"></b><p class="text fg-color-blue" id="sendtext @item.MessageId + "> + @item.MessageText + </p><div class="date place-right fg-color-blue" style="margin-top:-10px; font-size:12px;"> + Hour + ':' + minute + ' ' + ampm + ' (' + strDate + ')</div></li>;

                             }
                             else if (@item.SenderUserId != ViewBag.loginUserId && @item.FileAttachedURL != "") {
                               <li id="reply" style="padding:4px 4px 24px 4px;" class="bg-color-white"><b class="sticker sticker-right sticker-color-white fg-color-blue" id="sendtext @item.MessageId + "></b><p class="text fg-color-blue" style="height:auto;">@item.MessageText + </p><br /><img class="fg-color-red" src="@item.FileAttachedURL + " style="height:auto; width:100%; margin-left:auto; margin-right:auto; "/><div class="date place-right fg-color-blue" style="margin-top:0px; font-size:12px;"> + Hour + ':' + minute + ' ' + ampm + ' (' + strDate + ')</div></li>
                             }
                             else if (@item.SenderUserId == ViewBag.loginUserId && @item.FileAttachedURL == "") {
                                <li id="reply" class="bg-color-blue"><b class="sticker sticker-left sticker-color-blue"></b><p class="text fg-color-white"  id="sendtext @item.MessageId + '">@item.MessageText + </p><br/><div class="date place-right fg-color-white" style="margin-top:-10px; font-size:12px;">' + Hour + ':' + minute + ' ' + ampm + ' (' + strDate + ')</div></li>;

                             }
                             else if (item.SenderUserId == (ViewBag.loginUserId) && item.FileAttachedURL != "") {
                                <li id="reply" style="padding:4px 4px 24px 4px;" class="bg-color-blue"><b class="sticker sticker-left sticker-color-blue fg-color-white" id="sendtext @item.MessageId + "></b><p class="text fg-color-white" style="height:auto;"> + @item.MessageText + </p><br /><img class="fg-color-red" src=" + item.FileAttachedURL + " style="height:auto; width:100%; margin-left:auto; margin-right:auto; "/><div class="date place-right fg-color-white" style="margin-top:0px; font-size:12px;">' + Hour + ':' + minute + ' ' + ampm + ' (' + strDate + ')</div></li>
                             }
                             //$('#recieverId').val(item.UserID)

                             //$("#sendtext" + item.MessageId).emoticons();

}

メイン ビューの部分ビューで目的の結果が得られません。どうすればよいですか?

4

0 に答える 0