asp.net mvc area namespace broken or something? - c#

i have created a area. after that i created the controller and then created the view as list. but the foreach statement is not able to recognize the Model. it is taking it as object but it should be an ienumarable? do i have to configure namespaces in the area somewhere?
<%# Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Admin.Master" Inherits="System.Web.Mvc.ViewPage<IEnumerable<CentralBanking.Models.Currency>>" %>
<% foreach (var item in Model) { %>// error object doesn't contains the public method for geteumerator
casting the Model will solve the problem but it is not i want to do. everything is working fine outside the area

You can see that the other parts of the Inherits type are fully qualified. You must do the same with IEnumerable<>, which belongs in the System.Collections namespace:
<%# Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Admin.Master" Inherits="System.Web.Mvc.ViewPage<System.Collections.IEnumerable<CentralBanking.Models.Currency>>" %>

Related

Html does not exist in current context. Can I use html class in web forms using Web View syntax?

I am programming an app that has some sections in WebForms and some in MVC. I have a Webform.master and a Site.Master. When using the Html class, My Webform.master in visual studio says "The name html does not exist in the current context." but I don't get that error in site.master file, and html is used many times there.
Both the site.master and webform.master are in the views/shared directory.
EDIT: my purpose is to render a partial view in webforms. I'm unable to do that because html is not recognized in webform.master. If there's an alternate way to render a partial view, I'd use that if possible.
This is the Webform.master.
<%# Master
Language="C#"
MasterPageFile="~/Views/Shared/Root.Master"
AutoEventWireup="true"
CodeBehind="Webform.master.cs"
Inherits="JCIMS_MVC2_EF.WebUI.Views.Shared.Webform"
%>
<%# Register src="SupportPartial.ascx" tagname="SupportPartial" tagprefix="uc1" %>
This is the Site.master.
<%# Master Language="C#" MasterPageFile="~/Views/Shared/Root.Master" Inherits="System.Web.Mvc.ViewMasterPage" %>
The Html methods are all part of the HtmlHelper class which is a feature of MVC.
The first line of site.Master is telling your page to inherit from System.Web.Mvc.ViewMasterPage which will give you access to the MVC namespace and the HtmlHelper you're looking for.
<%# Master Language="C#" MasterPageFile="~/Views/Shared/Root.Master" Inherits="System.Web.Mvc.ViewMasterPage" %>
Your Webform.master page inherits from JCIMS_MVC2_EF.WebUI.Views.Shared.Webform as you can see from the first line. So you will not be able to use the HtmlHelper here.
<%# Master
Language="C#"
MasterPageFile="~/Views/Shared/Root.Master"
AutoEventWireup="true"
CodeBehind="Webform.master.cs"
Inherits="JCIMS_MVC2_EF.WebUI.Views.Shared.Webform"
%>

Change the base class of a razor view in the view code

Note that this is not a duplicated question. I know we can specify a base view type in the razor section of views/web.config. But I want my view1,view2 inherit from baseviewA, and the view3,view4 inherit from baseviewB. In razor how can I do this like that in ASPX engine:
<%# Page Language="C#" Inherits="Test.Myproject.Web.Mvc.ViewBase" %>
<%# Control Language="C#" Inherits="Test.Myproject.Web.Mvc.PartialViewBase" %>
EDIT I don't care about models. In my question baseviewA and baseviewB are totally different classes.
You can change the base class in Razor with the #inherits keyword, your base classes just need to derive from System.Web.Mvc.WebViewPage.
So your sample:
<%# Page Language="C#" Inherits="Test.Myproject.Web.Mvc.ViewBase" %>
Will be
#inherits Test.Myproject.Web.Mvc.ViewBase
Where
public class Test.Myproject.Web.Mvc.ViewBase : System.Web.Mvc.WebViewPage
{
}
Inherits specifies the model type going to be used in the view. Same this can be done in Razor.
<%# Page Language="C#" Inherits="Test.Myproject.Web.Mvc.ViewBase<Test.Models.MyModel>" %
is equivalent to following in Razor
#model Test.Models.MyModel
this is same in both views and partial views,So
<%# Control Language="C#" Inherits="Test.Myproject.Web.Mvc.PartialViewBase<Test.Models.MyModelB>" %>
is equivalent to
#model Test.Models.MyModelB

MVC2 C# - Why I cannot assign a Model.ImageUrl to a img src attribute in a UserControl?

I'm starting with MVC 2 in Visual Studio 2010. The thing is I'm working with a MasterPage where I'm rendering an action which returns a UserControl:
Everything works well, I mean the user control has a lot of other labels and it's rendering other properties... So the problem has nothing to do with anything apart the img tag and the src attribute:
Site.master:
<div class="Test">
<% Html.RenderAction("HeaderDetails", "User"); %>
</div>
HeaderDetails.ascx:
<img src="<%= Model.ImageUrl %>" />
For my surprise I cannot use the Model inside that attribute in a usercontrol, I have the same for a Page.aspx that is working.
The same happens with ViewData["ImageUrl"], I just don't have even intellicense in that attribute. It's like: I cannot do that.
Does anyone know why is that?, or how should I do it?
I kind of got lost towards the end of your question, but in regards to the strongly typed Model in the user control, you'll need your page, masterpage and user control to all inherit with the same type arguments
page:
<%# Page Language="C#" MasterPageFile="Your.Master" Inherits="System.Web.Mvc.ViewPage<YourModelType>" %>
master:
<%# Master Language="C#" Inherits="System.Web.Mvc.ViewMasterPage<YourModelType>" %>
control:
<%# Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<YourModelType>" %>
The type arg of the control and master can be base types of YourModelType, but obviously will need to have the ImageUrl property
Well, I have solved it by creating a Custom HtmlHelper using Extension Methods going through this link.
The only matter is that I couldn't use the namespace MyMVCApp.Helpers, I had to use the System.Web.MVC one, so I'm not sure that would be the best way to do it.
If you have some input on that I will appreciate it.
So, I was missing the Import to MyMVCApp.Helpers inside the user control :D

MVC2 nested master could not load type?

I've just begun experimenting with nested masters in MVC2. I'm not quite sure why I am getting this error.
Parser Error Message: Could not load type 'MySite.Views.Shared.Master'.
I used the add item wizard, created Master.master, and selected it's master to be Site.Master. In one of my views I changed the MasterPageFile from Site.Master to Master.master. I haven't changed any contents. What have I gotten wrong?
It sounds like you've added a Master Page and not a MVC Master Page. You need to make sure that both of your master pages inherit from System.Web.Mvc.ViewMasterPage.
So your master page directives should look like this:
<%# Master Language="C#" Inherits="System.Web.Mvc.ViewMasterPage" %>
AND
<%# Master MasterPageFile="~/Views/Shared/Site.Master" Language="C#" Inherits="System.Web.Mvc.ViewMasterPage" %>
HTHs,
Charles

Strongly typed master pages polymorphism - nested masterpages ignore inherit attribute

I'm currently creating a CMS system and found that the following doesn't work.
I do have a work around that isn't exactly ideal and feels dirty. I'm cool with it for now and not really that interested in a different approach (but don't let that stop you answering). What I am after is some kind of explaination on why it doesn't work - is it a bug in ASP.NET MVC?
It's hard to explain so I'll let my code (minus alot of fluff) do the talking... hope it makes sense!
EDIT: It seems that the compiler totally ignores the second masterpage's 'inherits' attribute - see at the bottom of the question.
ContentViewData.cs - notice it inherits from BaseViewData
public class ContentViewData : BaseViewData
{
public MyCMS.Data.Models.Content ContentItem { get; set; }
}
Site.Master - Notice the strongly typed viewdata of type BaseViewData
<%# Master
Language="C#"
Inherits="System.Web.Mvc.ViewMasterPage<MyCMS.WebSite.ViewData.BaseViewData>" %>
Content.Master - Notice the strongly typed viewdata of type ContentViewData and the fact that it's a child masterpage of Site.Master
<%# Master
Language="C#"
MasterPageFile="~/Views/Shared/Site.Master"
Inherits="System.Web.Mvc.ViewMasterPage<MyCMS.WebSite.ViewData.ContentViewData>" %>
...blah blah blah...
<% Html.RenderPartial("ContentItemImage", Model.ContentItem); %>
ContentItemImage.ascx
<%# Control
Language="C#"
Inherits="System.Web.Mvc.ViewUserControl<MyCMS.Data.Models.Content>" %>
<% if (Model.HasPrimaryPhoto)
{ %>
<img src="/content/photos/<%= Model.GetPrimaryPhoto.ThumbFileName %>"
title="<%= Model.GetPrimaryPhoto.Caption %>" />
<% } %>
Now inside the Content.Master if I try and render the ContentItemImage partial and refer to a property on the ContentViewData object (specifically the 'ContentItem' property) like I have - repeated below.
<% Html.RenderPartial("ContentItemImage", Model.ContentItem); %>
If falls over on that line with the following error
Compilation Error
CS1061: 'object' does not contain a definition for 'ContentItem' and no
extension method 'ContentItem'
accepting a first argument of type
'object' could be found (are you
missing a using directive or an
assembly reference?)
BUT if I change things up like so, it all works fine and dandy.
Content.Master - Notice I'm passing into RenderPartial() the whole Model (ContentViewData object) rather than trying to refer to a property on the ContentViewData object
<%# Master
Language="C#"
MasterPageFile="~/Views/Shared/Site.Master"
Inherits="System.Web.Mvc.ViewMasterPage<MyCMS.WebSite.ViewData.ContentViewData>" %>
...blah blah blah...
<% Html.RenderPartial("ContentItemImage", Model); %>
ContentItemImage.ascx - notice the changed strongly typed viewdata from MyCMS.Data.Models.Content to the ContentViewData class.
<%# Control
Language="C#"
Inherits="System.Web.Mvc.ViewUserControl<MyCMS.WebSite.ViewData.ContentViewData>" %>
<% if (Model.ContentItem.HasPrimaryPhoto)
{ %>
<img src="/content/photos/<%= Model.ContentItem.GetPrimaryPhoto.ThumbFileName %>"
title="<%= Model.ContentItem.GetPrimaryPhoto.Caption %>" />
<% } %>
So yeah, that works but it aint go not alibi.
Thanks in advance,
Charles.
EDIT: Interestingly it seems that the compiler totally ignores the second master page's 'inherits' attribute.
Eg. I can do this and it still compiles without a complaint...
<%# Master
Language="C#"
MasterPageFile="~/Views/Shared/Site.Master"
Inherits="System.Web.Mvc.ViewMasterPage<ThisDoesntExist.AtAll>" %>
Interesting...A resonable guess would be that your ContentViewData object is being upcast to BaseViewData due to some interaction with your nested master page (if that is indeed the case, someone else will need to weigh in as to why).
You could verify trying this:
<% Html.RenderPartial("ContentItemImage", ((MyCMS.WebSite.ViewData.ContentViewData)Model).ContentItem); %>
The reason your workaround "works" is because your partial view is typed for ContentViewData, so when you pass in Model it is downcast to that type.
As you can see from my edit, it seems that the compiler totally ignores the nested master page's 'inherits' attribute.
This leads me to believe that a nested masterpage in ASP.NET MVC will always inherit from it's parent masterpage and as I've witnessed, totally ignore the inherits attribute.
EDIT: There must be some magic going on here... If I remove the 'inherits' attribute it won't compile because it doesn't know about the HtmlHelper class. But if I have the 'inherits' attribute in there with garbage inside it, it does compile.
Doesn't work
<%# Master
Language="C#"
MasterPageFile="~/Views/Shared/Site.Master" %>
Does work
<%# Master
Language="C#"
MasterPageFile="~/Views/Shared/Site.Master"
Inherits="Sysasfdaasdtem.Web.Mvsdfc.ViewMasterPage<ThisDoesntExist.AtAll>" %>
Very odd indeed.
Just try to do the next thing:
<%# Master
Language="C#"
MasterPageFile="~/Views/Shared/Site.Master"
Inherits="System.Web.Mvc.ViewMasterPage<MyCMS.WebSite.ViewData.ContentViewData>" %>
...blah blah blah...
<%= string.Empty %>
...blah blah blah...
<% Html.RenderPartial("ContentItemImage", Model.ContentItem); %>
Don't ask anything, just try to put <%= string.Empty %> in your master page's code and then use your strongly typed Model property.

Categories