简体   繁体   English

如何在MVC4中将部分视图发送到Ajax

[英]How to send partial view to ajax in mvc4

I have an ajax call function. 我有一个ajax调用功能。 Inside that I'm calling a partial view. 在其中,我称局部视图。 This view is for displaying comments. 该视图用于显示评论。 How can I refresh this view using ajax? 如何使用Ajax刷新此视图? I don't like json in my context, because my linq queries are set up with models. 我在上下文中不喜欢json,因为我的linq查询是使用模型设置的。 So these models with partial view should be send to ajax method. 因此,这些具有部分视图的模型应该发送给ajax方法。 Ajax method should replace my div. Ajax方法应该替换我的div。 Note that before ajax call, this view should be rendered at first as the page loads. 请注意,在ajax调用之前,应在页面加载时首先呈现此视图。 I am not getting this. 我不明白这一点。 What is my fault? 我怎么了

 $.ajax({
        url: '/Home/Item',
        type: 'POST',
        data: { itemid: itemid },
        success: function (data) {

       $('.mycontainer').html(data);

        }
    });

Controller 控制者

        public ActionResult Item(int itemid)
    {

        FoodContext db = new FoodContext();
        ViewBag.FoodItems = db.FoodItems.Where(row => row.itemid == itemid);
        List<ImageComment> comments = (from user in db.TxtComments
                                       join o in db.Logins on user.username equals o.username
                                       where user.itemid == itemid
                                       select new ImageComment
                                       {
                                           ImageUrl = o.imgurl,
                                           Comment = user.txtcmt,
                                           ImgCmntUrl = user.imgurl,
                                           Cmntdate = user.cmtdate,
                                           Username = user.username,

                                       }).OrderByDescending(x => x.Cmntdate).ToList();

        ViewModel vm = new ViewModel { ImageComments = comments };
        return PartialView("_Comments", vm);




    }

Partial View 部分视图

      @model ViewModel

     @foreach (ImageComment comment in Model.ImageComments)
     {
    <table width="100%" height="152" border="0">
  <tr>
    <td width="101" rowspan="2" valign="top"><img src="@comment.ImageUrl" width="100%" height="100%" /></td>
    <td height="27" colspan="3" valign="middle"><p> @comment.Username Commented On   On @comment.Cmntdate</p></td>
  </tr>
  <tr>

    <td colspan="2" rowspan="2"><div style="width:70%;">  

    @if (@comment.ImgCmntUrl != null)
    {
     <img src="@Url.Content(comment.ImgCmntUrl)" width="100%" height="100%" />
    }
  </div>     
   <div style="background-color:#E3EEFA;width:68%;min-height:50px;padding:5px;">@comment.Comment</div></td>
    <td width="209" height="29">&nbsp;</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td height="23">&nbsp;</td>
    <td>Like this.</td>
    <td>Unlike this</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td height="23">&nbsp;</td>
    <td width="303">&nbsp;</td>
    <td width="588">&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
</table>  

     }

My view 我的观点

<div class="mycontainer">
</div>

You could use the jquery load function: 您可以使用jquery加载功能:

<div class=".mycontainer"></div>

<script>
    function ReloadComments(){
        $(".mycontainer").load("@Url.Action("Item", new { itemId = Model.Id })");
    }

    $(function(){
        setInterval(ReloadComments, 10000);
        ReloadComments();
    });
</script>

Your action method and partial view don't need to be edited. 您的操作方法和局部视图无需编辑。

We have did something similar in our project. 我们在项目中做了类似的事情。 first of all we have two action methods one is to get the data (doesnot have any view related things) and pass it to other one which have view , it will do nothing but get the input and render view directly. 首先,我们有两种操作方法,一种是获取数据(不包含任何与视图相关的东西),然后将其传递给其他具有视图的方法,它什么也不做,只能直接获取输入并渲染视图。

The context is we have a timer which will call the ajax request in particular interval and get the data pass it to action (which has view) that will update the view in the background. 上下文是我们有一个计时器,该计时器将在特定间隔内调用ajax请求并将数据传递给操作(具有视图),该操作将在后台更新视图。

Hope i made it clear. 希望我说清楚。

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

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