北城已荒凉 2016-08-18 16:03 采纳率: 0%
浏览 47

重新加载部分视图Ajax [重复]

This question already has answers here:
                </div>
            </div>
                    <div class="grid--cell mb0 mt4">
                        <a href="/questions/19392212/how-to-use-jquery-or-ajax-to-update-razor-partial-view-in-c-asp-net-for-a-mvc-p" dir="ltr">How to use jquery or ajax to update razor partial view in c#/asp.net for a MVC project</a>
                            <span class="question-originals-answer-count">
                                (3 answers)
                            </span>
                    </div>
            <div class="grid--cell mb0 mt8">Closed <span title="2016-08-19 00:29:52Z" class="relativetime">3 years ago</span>.</div>
        </div>
    </aside>

I have a a partial view "_search" that display a list of data I added a textbox for search and a link search

when I click on the link I get an error page undefined. how can I execute the search method and display th same partial view in my search method I return json

        return Json(
        {
            Data = base.RenderPartialView("_search", model),
                        }, JsonRequestBehavior.AllowGet);

search view I changed <a> with a button

<script type="text/javascript">
    document.getElementById("myButton").onclick = function () {
        location.href = '@Url.Action("Search")'
    };
</script>

<button id="myButton" >search</button>
</div>
  • 写回答

1条回答 默认 最新

  • weixin_33714884 2016-08-18 17:41
    关注

    You must repair code because you can't return partialview with json type and .... The best way that i suggest you, before send question searched in google website that how to implement this task.

    The following link help you to understand how to implement this work:

    https://www.google.com/search?q=reload+partial+in+mvc&ie=utf-8&oe=utf-8

    also you can see the example of how to ' Auto Refresh Partial View in ASP.NET MVC ' in the following link:

    https://www.mindstick.com/Articles/1132/auto-refresh-partial-view-in-asp-dot-net-mvc

    Example:

    So, say you have your View with PartialView, which have to be updated by button click:

    <div class="target">
        @{ Html.RenderAction("UpdatePoints");}
    </div>
    <input class="button" value="update" />
    

    There are some ways to do it. For example you may use jQuery:

    <script type="text/javascript">
        $(function(){    
            $('.button')click(function(){        
                $.post('@Url.Action("PostActionToUpdatePoints", "Home")').always(function(){
                    $('.traget').Load('/Home/UpdatePoints');        
                })        
            });
        });        
    </script>
    

    PostActionToUpdatePoints is your Action with [HttpPost] attribute, which you use to update points

    If you use logic in your action UpdatePoints() to update points, maybe you forgot to add [HttpPost] attribute to it:

    [HttpPost]
    public ActionResult UpdatePoints()
    {    
        ViewBag.points =  _Repository.Points;
        return PartialView("UpdatePoints");
    }
    

    I hope this help you.

    Reference:

    Updating PartialView mvc 4

    评论

报告相同问题?

悬赏问题

  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程
  • ¥20 模型在y分布之外的数据上预测能力不好如何解决