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());
}
To create a partial view, right-click on view -> shared folder and select Add -> View option. In this way we can add a partial view. It is not mandatory to create a partial view in a shared folder but a partial view is mostly used as a reusable component, it is a good practice to put it in the "shared" folder.
Another best way is to place Partial View inside shared folder & call same partial View from different controller using Shared Folder. And then call it from controller as mentioned above. That's it.
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"
.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With