Question

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());
    }
Was it helpful?

Solution

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".

OTHER TIPS

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>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top