views:

145

answers:

1

Hello everybody.

I am trying to learn some ASP.NET MVC. Please bear with me but asp.net mvc is the first mvc framework i ever tried to learn. I do plan to learn more about mvc framework in different languages, but for now i got asp.net mvc.

I am fairly good with asp.net forms and love them, but would like to have another asp.net tool that allows me more freedom with HTML and JavaScript and Ajax.

The question i have is how partialViews are used ? Another question is as far as i understand from watching all the videos is that model for database, controler for action and business logic and view is for the display.

However i have a code that doesn't work, the View is pointing to the Model and not to what returning from controler (Trying to return partial view) I would really appreciate if somebody can point me in right direction and explain whats goes on.

Bellow is my simple code that doesn't work:

PartialView

<%@ Control Language="VB" Inherits="System.Web.Mvc.ViewUserControl(Of IEnumerable (Of MvcPortfolio.Defaultdb))" %>
    <p>
        <%=Html.ActionLink("Create New", "Create")%>
    </p>

    <table>
        <tr>
            <th></th>
        </tr>

    <% For Each item In Model%>

        <tr>
            <td>
                 <%= item. %>
            </td>
        </tr>

    <% Next%>

    </table>

Model

Public Function getProjects() As List(Of portfolio_project)
        Using myPortfolio As New PortfolioDataContext
            Try
                Dim projects = (From p In myPortfolio.portfolio_projects _
                                Select p).Take(5)

                Return projects
            Catch ex As Exception
                Return Nothing
            End Try
        End Using
    End Function

Controler: Function Index() As ActionResult

    Dim myPortfolio As New Defaultdb ' my controler
    Dim projectsList As List(Of portfolio_project) = myPortfolio.getProjects() 'getting list of all the projects in news

    Return PartialView(projectsList) 'returning partial view.hopefully will work
End Function

and Main page:

<%@ Page Language="VB" MasterPageFile="~/Views/Shared/Main.Master" Inherits="System.Web.Mvc.ViewPage" %>
<%@ Import Namespace="MvcPortfolio" %>
<asp:Content ID="projectsContent" runat="server" ContentPlaceHolderID="newsContent">
    <%Html.RenderPartial("Indexprojects")%>
</asp:Content>

Can somebody please explain me how to make this code to work, so i will have a normal working example. I want to use partial View because i want repeating stuff to be put in user control like pieces.

Thanks in advance.

+2  A: 

Dmitris,

Have a look at this question: http://stackoverflow.com/questions/1107547/how-to-use-two-instances-of-the-same-ascx-in-the-same-page-in-asp-net-mvc. I think you will find some insight there.

Also, you should spend some time and go through the NerdDinner tutorial.
http://weblogs.asp.net/scottgu/archive/2009/04/28/free-asp-net-mvc-nerddinner-tutorial-now-in-html.aspx

Partial views are called from the view, like this:

<%= Html.RenderPartial("PartialViewName") %>

They are not generally called from the controller.

Robert Harvey
I went trough all the videos on asp.net web site, and watched the video about nerddiner too. But didnot find good example of PartialViews
Dmitris
Partial views are called from the view, like this: <%= Html.RenderPartial("PartialViewName") %>They are not generally called from the controller.
Robert Harvey
More info on Partial Views: http://blog.codeville.net/2008/10/14/partial-requests-in-aspnet-mvc/
Robert Harvey