How to replace a partial view by another?
Asked Answered
O

2

5

I want to show a list of "Latest news" articles and, on click on "Read more" link, to show the full content of the clicked article. For that, I use a partial view in my Index.cshtml view. The first time the Index view is displayed, the partial view is populated with the list of articles. What I want is: to reuse the same partial view to show the full article, on click.

For some reason I can't manage to do that and I can't see why. Using the debugger and Firebug, I can trap no error. Furthermore, the application goes through all steps and it seems even to display the _Details partial view, but actually, on the screen is still the list.

Thank you for pointing me out the error.

Edit:

I have all the necessary scripts referenced in _Layout.cshtml: jquery-1.8.2.min.js, jquery-ui-1.8.24.custom.min.js, jquery.ui.core.min.js, jquery.validate.min.js, jquery.validate.unobtrusive.min.js and jquery.unobtrusive-ajax.js and also I have "UnobtrusiveJavaScriptEnabled"="true" in web.config.

Index.cshtml

@model MyApp.ViewModels.NewsViewModel

<div id="content" class="content_style"> 
   <text>Some static content</text>
   <div class="active_part">
        @Html.Partial("_List", Model.NewsList)
   </div><!-- end active_part -->
</div><!-- end content -->

_List.cshtml

@model IEnumerable<MyApp.Models.News>
@using (Ajax.BeginForm(new AjaxOptions { UpdateTargetId = "active_part", InsertionMode = InsertionMode.Replace }))
{
    foreach (var item in Model)
    {
    <h2 class='title">
    @Ajax.ActionLink(item.Title, "Details", "News", new { id = item.News_ID }, new AjaxOptions { UpdateTargetId = "active_part" }, null)
        </h2>

        <div class="body">
            @Html.Raw(item.Body)
            @Ajax.ActionLink("Read more", "Details", "News", new { id = item.News_ID }, new AjaxOptions { UpdateTargetId = "active_part" }, null)
    </div>
    }
}

_Details.cshtml

@model MyApp.Models.News
@using (Ajax.BeginForm(new AjaxOptions{ UpdateTargetId = "active_part", InsertionMode = InsertionMode.Replace }))
{

    <div class="single">
        <h2 class="title">@Model.Title</h2>
        <div class="entry-content">
            @Html.Raw(Model.Body)
        </div>
    <div class="clear"></div>    
    </div>
}

ViewModel

public class NewsViewModel
{
    // Properties
    public News CurrentNews { get; set; }
    public List<News > NewsList {get; set;}

    // Constructor
    public NewsViewModel() { }

    public NewsViewModel(News pCurrentNews , List<News> pNewsList)
    {
        CurrentNews = pCurrentNews ;
        NewsList = pNewsList;
    }
}

Controller

    public ViewResult Index()
    {
        return View( new NewsViewModel(null, repository.FindAllNews()));
    }

    public ActionResult Details(long id)
    {
        News article = repository.FindAnArticleByID(id);
        return PartialView("_Details", article);
    }


    public ActionResult ListNews()
    {
        return PartialView("_List", repository.FindAllNews());
    }
Opposition answered 4/10, 2012 at 15:48 Comment(1)
If you put a breakpoint in your controller is the Details action definitley being called?Blacksmith
A
8

In your Index.cshtml view:

<div class="active_part">

should be:

<div id="active_part">

When you specify UpdateTargetId = "active_part" in your AjaxOptions, it is looking for a DOM element with id="active_part", not class="active_part".

Ackley answered 4/10, 2012 at 16:12 Comment(1)
Thank you Darrin, you were right. Wow, what a goof! I must be more tired than I thought...Opposition
B
1

Make sure you're importing the proper .js scripts in your page.

I've had a lot more luck with jquery scripts as opposed to Microsoft Ajax scripts:

<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
Blacksmith answered 4/10, 2012 at 16:6 Comment(2)
Sorry, posted this at the same time you updated with your edit :)Blacksmith
Thank you for your suggestion, indeed I was editing at the same time, because I had just realized that jquery scripts would be the first thought of anyone willing to help. As a matter of fact, I have several other views with partial views inside them, but this was the only one that didn't behave. It was driving me crazy.Opposition

© 2022 - 2024 — McMap. All rights reserved.