ASP.NET MVC: Stack overflow error when calling Html.RenderPartial() - asp.net-mvc

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");%>

Related

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>

Why did my custom HtmlHelpers stop working after I upgraded to MVC3?

I just finished going through this checklist to upgrade my asp.net mvc2 site to mvc3: http://www.asp.net/learn/whitepapers/mvc3-release-notes#upgrading
Everything is compiling, but when I run the application I get errors in the views.
Example View:
<%# Page Title="" Language="C#" MasterPageFile="~/Views/Shared/site.Master" Inherits="System.Web.Mvc.ViewPage<Genesis.Domain.Entities.StreamEntry>" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
<%: Model.seTitle %>
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="MetaContent" runat="server">
<%: Html.GetMetaTag("description", Model.seDescription )%>
<%: Html.GetMetaTag("keywords", Model.seKeywords )%>
</asp:Content>
<asp:Content ID="Content4" ContentPlaceHolderID="HeadlineContent" runat="server">
<%: Model.seHeadline %>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<p>
<%//: Html.AddThis() %>
</p>
<p>
Created: <br /><strong><%: Model.seDateCreated %></strong><br />
Last Modified: <br /><strong><%: Model.seDateModified %></strong>
</p>
<%: MvcHtmlString.Create(Model.seBody) %>
<% Html.RenderAction("Comments", "Comments", new { streamEntryID = Model.seID, allowComments = Model.AllowComments }); %>
</asp:Content>
Error text:
Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.
Compiler Error Message: CS1061: 'System.Web.Mvc.HtmlHelper<Genesis.Domain.Entities.StreamEntry>' does not contain a definition for 'GetMetaTag' and no extension method 'GetMetaTag' accepting a first argument of type 'System.Web.Mvc.HtmlHelper<Genesis.Domain.Entities.StreamEntry>' could be found (are you missing a using directive or an assembly reference?)
Source Error:
Line 6:
Line 7: <asp:Content ID="Content3" ContentPlaceHolderID="MetaContent" runat="server">
Line 8: <%: Html.GetMetaTag("description", Model.seDescription )%>
Line 9: <%: Html.GetMetaTag("keywords", Model.seKeywords )%>
Line 10: </asp:Content>
How do I get my views to recognize my custom html helpers again?
Try adding an Import directive on the top of the view to see if it makes any difference:
<%# Import Namespace="Namespace.Of.The.Class.Containing.The.Helper" %>
Also you could add this namespace to the <namespaces> section of web.config.

Client side validation script is not generated for partial views fetched using AJAX

I am trying to setup client side validation using MicrosoftMvcJQueryValidation
to work with ajax submitted forms.
It works perfectly fine if the partial view is rendered directly from a view.
However when I try to fetch it over XHR, for example to show it in a JQuery Dialog,
client validation javascript is not generated for the output html.
Any ideas?
Working code - partial view is rendered using Html.RenderPartial:
View:
<%# Page Title="" Language="C#" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<% Html.RenderPartial("New"); %>
</asp:Content>
Partial View:
<%# Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<Product>" %>
<% Html.EnableClientValidation();%>
<% Html.BeginForm();%>
<%= Html.EditField(m => m.price)%>
<%= Html.ValidationMessageFor(m => m.price)%>
<% Html.EndForm();%>
Not working code - partial view is fetched with JQuery load() function.
View:
<%# Page Title="" Language="C#" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
....
$("#dialog").load('~/Product/New/');
$("#dialog").dialog("open");
....
<div id="dialog" title=""></div>
</asp:Content>
Relevant controller action:
public PartialViewResult New(int id)
{
return PartialView(service.GetProduct());
}
Thanks.
This will happen for all partial views which return JavaScript inserted into the DOM via AJAX. The problem is that the JavaScript returned with the partial view is not executed/interpreted when it is inserted into the document.
Unfortunately, because what you are dealing with is generated JavaScript I can explain why you are experiencing the problem but I'm at a loss for a solution. Were this a function you wrote I'd suggest adding it to the OnComplete. All I can offer is that this is a JavaScript limitation and to start looking there.
BTW: this looks promising http://adammcraventech.wordpress.com/2010/06/11/asp-net-mvc2-ajax-executing-dynamically-loaded-javascript/
After reading the blog ARM has mentioned it seems the solution really depends
on how one is loading the partial view.
What eventually worked for me is to call __MVC_EnableClientValidation
after view has been assigned to an element:
$.get('~/Product/New/', function(data) {
$("#dialog").html(data);
// extracted from MicrosoftMvcJQueryValidation.js
var allFormOptions = window.mvcClientValidationMetadata;
if (allFormOptions) {
while (allFormOptions.length > 0) {
var thisFormOptions = allFormOptions.pop();
__MVC_EnableClientValidation(thisFormOptions);
}
}
},'html');

ASP.NET MVC Using Multiple user controls on a single .aspx(view)

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.

Resources