1

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>
Jack.D
  • 21
  • 1
  • 6
  • Why are you returning `Partial View` in `json result` ?. – mmushtaq Aug 18 '16 at 16:18
  • what's the correct way to do it? – Jack.D Aug 18 '16 at 17:04
  • Where is your code for the click event ? How are you calling this action method and finally your current code is not returning partial view! – Shyju Aug 18 '16 at 17:12
  • 1
    You just `return PartialView("_search", model)`. However, you are not making an AJAX call -- you are telling the browser to navigate away. Full example in this [answer](http://stackoverflow.com/questions/19392212/how-to-use-jquery-or-ajax-to-update-razor-partial-view-in-c-asp-net-for-a-mvc-p/19410973#19410973). – Jasen Aug 18 '16 at 17:33

1 Answers1

2

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

Community
  • 1
  • 1
Omid Nasri
  • 179
  • 1
  • 10
  • not exactly what I'm looking for ,I have a view with textbox for search and list of data from the db I need when I click search link to rerfresh the view with result – Jack.D Aug 18 '16 at 18:23
  • ok, i found the sample in my github account: follow following link:https://github.com/omidnasri/RenderPartialView – Omid Nasri Aug 18 '16 at 19:51