如何在mvc中刷新局部视图 [英] how to refresh partial view in mvc

查看:113
本文介绍了如何在mvc中刷新局部视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个详细视图,其中会话,甚至会话中的评论框。我在局部视图中有一个评论框。但是在决定刷新关于创建评论的对话时遇到了问题。

我知道我可以在局部视图中绑定代码。但我很难为它调用动作方法。

因此它将有详细信息查看代码然后在代码下面



< div class =row> 
< div class =panel-groupid =accordion>
@foreach(模型.TicketConversation.OrderByDescending中的var convo(x => x.LastUpdatedTimestamp))
{
< div class =col-md-10 col-md-offset -1style =padding-removed 5px;>
< div class =panel panel-default>
< div class =panel-heading removePaddingBottomdata-toggle =collapsedata-target =#accord-convo-@convo.idstyle =background-color:inherit; ID = accordHeading-@convo.id >
< a style =text-decoration:none!important; color:inherit; width:100px;>
< span style =font-size:16px; font-weight:bold; vertical-align:middle!important;> 的&NBSP;&NBSP; @ convo.Subject< /跨度>
< / a>
@ {var lastmessage = convo.ConversationComments.First(); }
< div class =row>
< div id =accord-lastmsg-@convo.idstyle =margin-removed 15px;>
< div class =@ lastmessage.DisplayCssClass>
< div class =row>
< div class =col-md-offset-1 col-md-10 removePaddingstyle =margin-removed 5px; margin-removed 10px;>
@ lastmessage.TypeOfContact
< / div>
< div class =col-md-offset-1 col-md-10 removePaddingstyle =margin-removed 10px;>
@ lastmessage.Message
< / div>
< / div>
< div class =row>
< div class =col-md-offset-6 col-md-5 text-right text-mutedstyle =margin-removed 10px;>
< small>
发布者:@ lastmessage.CommenterName @@ @ lastmessage.MessageTimestamp
< / small>
< / div>
< / div>
< / div>
< / div>
< / div>
< / div>
< div id =accord-convo-@convo.idclass =panel-collapse collapse>
< div class =panel-bodystyle =padding:0;>
@if(convo.IsReadOnly == false @ *&& Model.IsClosed == false * @)
{
< div class =row>
@ *< div class =col-md-12style =padding-removed 3px; padding-removed 3px;>
< a href =@ Url.Action(replyquery =,new {cid = convo.id})class =pull-right btn btn-sm btn-default>  回复< / a>
< / div> * @

----这里我正在为评论框调用部分视图---
}
@foreach(var item在convo.ConversationComments)
{
< div>
< div class =message-border @ item.DisplayCssClass>
< div class =row>
< div class =col-md-offset-1 col-md-10 removePaddingstyle =margin-removed 5px;>
@ item.TypeOfContact
< / div>
< div class =col-md-offset-1 col-md-10 removePaddingstyle =margin-removed 10px;>
@ item.Message
< / div>
< / div>
< div class =row>
< div class =col-md-5 text-right text-muted>
< small>
发布者@ item.CommenterName @@ @ item.MessageTimestamp
< / small>
< / div>
< / div>
< / div>
< / div>
}
< / div>
< / div>
< / div>
< / div>
}
< / div>
< / div>





可以调用哪种方法来获取会话列表?我是否需要编写一个新的操作方法。

解决方案

刷新局部视图的最佳方法是使用ajax。



创建新的动作,返回部分视图并在需要时调用它。



< pre lang =c#> protected ActionResult MyAction( string param)
{
/ * 某些逻辑* /
返回 PartialView( NameOfPartialView,modelForPartialView);
}





和页面上的内容如下:



 


.ajax({
url: MyActionUrl
data:


#partial-view-form)。serialize(),
type: POST
dataType: html
})
.success( function (result,status){


i'm having a detail view in which there will be a conversation, and even comment box in conversation. I had a comment box in partial view. but had an issue when decided to refresh just the conversation on creating comment.
I know i can bind below code in partial view. but i have difficulty in calling action method for it.
So it will have details view code and then below code

<div class="row">
        <div class="panel-group" id="accordion">
            @foreach (var convo in Model.TicketConversation.OrderByDescending(x => x.LastUpdatedTimestamp))
            {
                <div class="col-md-10 col-md-offset-1" style="padding-removed 5px;">
                    <div class="panel panel-default">
                        <div class="panel-heading removePaddingBottom" data-toggle="collapse" data-target="#accord-convo-@convo.id" style="background-color: inherit;" id="accordHeading-@convo.id">
                            <a style="text-decoration: none !important; color: inherit; width: 100px;">
                                <span style="font-size: 16px; font-weight: bold; vertical-align: middle !important;">  @convo.Subject</span>
                            </a>
                            @{ var lastmessage = convo.ConversationComments.First(); }
                            <div class="row">
                                <div id="accord-lastmsg-@convo.id" style="margin-removed 15px;">
                                    <div class="@lastmessage.DisplayCssClass">
                                        <div class="row">
                                            <div class="col-md-offset-1 col-md-10 removePadding" style="margin-removed 5px; margin-removed 10px;">
                                                @lastmessage.TypeOfContact
                                            </div>
                                            <div class="col-md-offset-1 col-md-10 removePadding" style="margin-removed 10px;">
                                                @lastmessage.Message
                                            </div>
                                        </div>
                                        <div class="row">
                                            <div class="col-md-offset-6 col-md-5 text-right text-muted" style="margin-removed 10px;">
                                                <small>
                                                    Posted by @lastmessage.CommenterName @@ @lastmessage.MessageTimestamp
                                                </small>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div id="accord-convo-@convo.id" class="panel-collapse collapse">
                            <div class="panel-body" style="padding: 0;">
                                @if (convo.IsReadOnly == false @*&& Model.IsClosed == false*@)
                                {
                                    <div class="row">
                                        @*<div class="col-md-12" style="padding-removed 3px; padding-removed 3px;">
                                            <a href="@Url.Action(" replyquery=", new { cid = convo.id })" class="pull-right btn btn-sm btn-default"> Reply</a>
                                        </div>*@
                                      
                                        ---- here i'm calling partial view for comment box---
                                }
                                @foreach (var item in convo.ConversationComments)
                                {
                                    <div>
                                        <div class="message-border @item.DisplayCssClass">
                                            <div class="row">
                                                <div class="col-md-offset-1 col-md-10 removePadding" style="margin-removed 5px;">
                                                    @item.TypeOfContact
                                                </div>
                                                <div class="col-md-offset-1 col-md-10 removePadding" style="margin-removed 10px;">
                                                    @item.Message
                                                </div>
                                            </div>
                                            <div class="row">
                                                <div class="col-md-5 text-right text-muted">
                                                    <small>
                                                        Posted by @item.CommenterName @@ @item.MessageTimestamp
                                                    </small>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                }
                            </div>
                        </div>
                    </div>
                </div>
            }
        </div>
    </div>



which method can be called to get conversation list?? do i need to write a new action method.

解决方案

Best thing for refreshing partial view is to use ajax.

Create new action where you return your partial view and call it when you need it.

protected ActionResult MyAction(string param)
{
    /*Some logic*/
    return PartialView("NameOfPartialView", modelForPartialView);
}



and on page something like this:


.ajax({ url: "MyActionUrl", data:


("#partial-view-form").serialize(), type: "POST", dataType: "html" }) .success(function (result, status) {


这篇关于如何在mvc中刷新局部视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆