ASP.NET MVC Using Multiple user controls on a single .aspx(view) - asp.net-mvc

I am getting this following error , when i am tring to having two user controls in one page.
The model item passed into the dictionary is of type 'System.Linq.EnumerableQuery1[Data.EventLog]' but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable1[Data.Notes]'.
<%# Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Test
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>Test</h2>
<% Html.RenderPartial("~/Views/Shared/UserControl/Tracking.ascx"); %>
<% Html.RenderPartial("~/Views/Shared/UserControl/Notes.ascx"); %>
</asp:Content>

Your Notes.ascx control is strongly-typed, but you are not passing a model in your call to RenderPartial. Hence, the model for the page is passed instead, which is the wrong type for the user control. You need to pass a model in your call to RenderPartial.
For example, if the event log page model has a property called NotesModel:
<%# Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<MyNamespace.EventLogModel>" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Test
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>Test</h2>
<% Html.RenderPartial("~/Views/Shared/UserControl/Tracking.ascx"); %>
<% Html.RenderPartial("~/Views/Shared/UserControl/Notes.ascx", Model.NotesModel); %>
</asp:Content>
Note that I've:
Changed the Inherits bit of the #Page directive to specify the model type and,
Added the model argument to RenderPartial.
You would of course need to populate Model.NotesModel in your controller. If it's null you'll see the same bug you presently see.

Related

How to include the meta keyword and meta description in the page?

I have the following code:
<%# Page Title="Spam Filter, Free Exchange Spam Filter – Software to Stop Spam" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="ContentBreadCrumb" Runat="Server">
Welcome to > Home
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentBodyMain" Runat="Server">
You can use the <asp:HtmlMeta> control. On the .aspx file you write:
<asp:HtmlMeta ID="descriptionMeta" runat="server" />
<asp:HtmlMeta ID="keywordMeta" runat="server" />
Then, from the code, you can do this to set content etc.
this.descriptionMeta.Name = "description";
this.descriptionMeta.Content = "description of the page or whatever";
this.keywordMeta.Name = "keywords";
this.keywordMeta.Content = "some,keywords,and,things";

Automatically rendering shared view as part of master page?

I have a shared view located in Shared/Header.aspx and I want to render this as part of the HeaderContent ContentPlaceHolder. My master page contains:
<asp:ContentPlaceHolder ID="HeaderContent" runat="server" />
and I want to be able to tell MVC to populate that content place holder with the Shared/Header view, which contains:
<%# Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeaderContent" runat="server">
....
</asp:Content>
At the moment in each of the pages where I want the HeaderContent to be populated (and on some pages I don't want it to be) I do this:
<asp:Content ID="Content2" ContentPlaceHolderID="HeaderContent" runat="server">
<% Html.RenderPartial("Header"); %>
</asp:Content>
Is there any way I can effectively do the above but from the Controller handling the request (or a child class of ViewPage)?
If you want to do this from the controller, then you could pass a property in the ViewData, which could be picked up from the layout page. E.g. in a controller action:
ViewData["ShowHeader"] = true;
Then in the layout page you could say
<% if(ViewData["ShowHeader"] != null && ((bool)ViewData["ShowHeader"]))
{
Html.RenderPartial("Header");
} %>
That way you don't need the extra content placeholder too.

how can a MVC view to populate a contentplaceholder in not direct ancestor Master

i have a Master page and in it another Master and another Master.
I want the view inside the third master to populate some contentPalceHolder in the upper-most Master.
How do I do it?
I used ContentPalceHolder and asp:Content to bubble the string from the inner aspx-view through all masters to the outer Master.
like this:
<asp:Content ContentPlaceHolderID="headerText" runat="server">
<asp:ContentPlaceHolder ID="subTabsHeaderText" runat="server"></asp:ContentPlaceHolder>
</asp:Content>
You have to make that 'anscestor' trickle down through each Master page:
----BigBoss.Master----
<%# Master Language="C#" Inherits="System.Web.Mvc.ViewMasterPage<Some.Type.Here>" MasterPageFile="~/Views/MasterPages/Site.Master" %>
<asp:ContentPlaceHolder id="cphTitle" runat="server"/>
----NotSoBigBoss.Master----
<%# Master Language="C#" Inherits="System.Web.Mvc.ViewMasterPage<Some.Type.Here>" MasterPageFile="~/Views/MasterPages/BigBoss.Master" %>
<asp:Content ContentPlaceHolderId="cphTitle" runat="server"/>
----ReallyLittleBoss.Master----
<%# Master Language="C#" Inherits="System.Web.Mvc.ViewMasterPage<Some.Type.Here>" MasterPageFile="~/Views/MasterPages/NotSoBigBoss.Master" %>
<asp:Content ContentPlaceHolderId="cphTitle" runat="server">
<!-- Stuff here -->
</asp:Content>

How can I automatically refresh a partial view?

I have a simple view:
<%# Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Administration
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>Indexer stats</h2>
<div id="Stats">
<% Html.RenderPartial("IndexerStats", CmsModels.Utilities.BackgroundIndexer.Default); %>
</div>
</asp:Content>
How can I automatically refresh the Partial View "IndexStats" every 10 seconds?
I have found this code, but it doesn't compile with my version of MVC (2 RC).
Damn it, you spend ages looking for an answer, ask the question and then find the answer imediately afterwards.
Revised code is:
Indexer stats
<script src="<%=Url.Content("~/Scripts/jquery-1.3.2.min.js")%>" type="text/javascript"></script>
<script type="text/javascript">
$(function() {
setInterval(function() {
$.get('<%=Url.Action("IndexerStats")%>', {}, function(view) {
$("div#IndexerStats").html(view);
})
}, 5000);
}); </script>
<div id="IndexerStats">
<% Html.RenderPartial("IndexerStats"); %>
</div>
</asp:Content>
However I am open to better ideas, or Html helpers.

ASP.NET MVC: Stack overflow error when calling Html.RenderPartial()

I have a Controller called ActivationController with a LogOn action, which renders the view LogOn.aspx.
LogOn.aspx renders a partial view called LogOn.ascx:
<%# Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
LogOn
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>Account Activation Step 1 - Log On</h2>
<p>
<%Html.RenderPartial("LogOn")<;%>
</p>
</asp:Content>
When calling the action i'm getting a "Stack Overflow" exception:
An unhandled exception of type 'System.StackOverflowException'
occurred in System.Web.Mvc.dll
Any clue?
Thanks in advance!
Don't bother to reply, i found the issue.
The problem was that partial view should have a different name than the view. :P
Thanks anyway!!
This looks wrong:
<%Html.RenderPartial("LogOn")<;%>
it should look like this:
<% Html.RenderPartial("LogOn");%>

Resources