I would like to make a database call in a partial view in asp.net MVC.
I am not sure how to actually go about that.
I am trying to get an instance of the repository so I can make a couple calls to the DB
to build some info on the page. Not sure if I am even close but does anyone have any
ideas ?
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
<%
if (Request.IsAuthenticated)
{
var repos = MyMVC.Models.Repository.IRepository<UserProfile>();
}
%>
Try using MvcContrib. Check out the RenderAction method. It lets you keep the data access in the controller, and the view stuff in the view, without mixing the responsibilities.
Also, RenderAction will be in MVC 2, which makes me very happy. It won’t support a generic implementation, though (when I last checked).
Phil Haack on RenderAction: http://haacked.com/archive/2009/11/18/aspnetmvc2-render-action.aspx