MVC3 doesn't recognize MvcContrib namespace in Razor View - asp.net-mvc-3

I'm trying to paginate something with MvcContrib's Html.Pager(), but my razor views can't reference the right namespace.
Controller is ok:
using MvcContrib.Pagination;
...
public ActionResult List(int? page)
{
return View(new UserRepository().GetUserList().AsPagination(page ?? 1, 10));
}
But, the view can't make sense of either:
#using MvcContrib
OR
#Html.Pager((IPagination)Model)
I installed MvcContrib via NuGet. I tried adding MvcContrib, MvcContrib.UI and MvcContrib.UI.Html namespaces to <pages><namespaces> in web.config with no luck. Did I miss something?

Contrary to WebForms, Razor doesn't use the <namespaces> section in ~/web.config. It uses the <namespaces> in ~/Views/web.config:
<system.web.webPages.razor>
<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<pages pageBaseType="System.Web.Mvc.WebViewPage">
<namespaces>
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Routing" />
<add namespace="MvcContrib"/>
<add namespace="MvcContrib.UI.Grid"/>
<add namespace="MvcContrib.UI.Pager"/>
</namespaces>
</pages>
</system.web.webPages.razor>
and then:
#model MvcContrib.Pagination.IPagination<SomeViewModel>
#Html.Pager(Model)
or you could also add the proper namespace to your view if you prefer:
#model MvcContrib.Pagination.IPagination<SomeViewModel>
#using MvcContrib.UI.Pager
#Html.Pager(Model)

After adding MvcContrib.dll reference, try this code.
#using MvcContrib.UI.Pager
#using MvcContrib.Pagination
#model IPagination
#Html.Pager(Model)
I posted MvcContrib Grid paging,filtering + MVC3 Razor sample article to my blog.

Related

Compiler Error Message: CS0103: The name 'ViewBag' does not exist in the current context

I have changed service providers, and since then as soon as i goto www.greptech.co.za i get the above error.
The service provider is using .NET 4.0 and does have the latest MVC3 installed and up to date.
What else can be the issue ? I have googled this, and i have done the above checks. The service provider doesnt know what else is wrong.
I havent changed my Web.config file, for it was working with my old service provider.
Site has been down for a long time now. Please can anyone assist
Here is my web.config file :
<?xml version="1.0" encoding="UTF-8"?>
<!--
For more information on how to configure your ASP.NET application, please visit
http://go.microsoft.com/fwlink/?LinkId=152368
-->
<configuration>
<connectionStrings>
<add name="MySqlServer" connectionString="Datasource=431.186.904.538;Port=3306;Database=grep;uid=user;pwd='XXXX';Allow User Variables=true;check parameters=false;logging=true" providerName="MySql.Data.MySqlClient" />
</connectionStrings>
<system.net>
<mailSettings>
<smtp deliveryMethod="Network" from="support#greptech.co.za">
<network host="mail.greptech.co.za" userName="support#greptech.co.za" password="XXXX" port="25" />
</smtp>
</mailSettings>
</system.net>
<appSettings>
<add key="ENV" value="PROD"/>
<add key="webpages:Version" value="1.0.0.0" />
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
</appSettings>
<system.web>
<!-- <customErrors mode="On"/> -->
<trace enabled="true" pageOutput="false" requestLimit="40" localOnly="false" />
<compilation debug="true" targetFramework="4.0">
<assemblies>
<add assembly="System.Web.Abstractions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<add assembly="System.Web.Helpers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<add assembly="System.Web.Routing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<add assembly="System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<add assembly="System.Web.WebPages, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
</assemblies>
</compilation>
<authentication mode="Forms">
<forms loginUrl="~/Account/LogOn" timeout="2880" />
</authentication>
<pages>
<namespaces>
<add namespace="System.Web.Helpers" />
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Routing" />
<add namespace="System.Web.WebPages" />
</namespaces>
</pages>
<customErrors mode="Off"/>
</system.web>
<system.webServer>
<validation validateIntegratedModeConfiguration="false" />
<modules runAllManagedModulesForAllRequests="true" />
<directoryBrowse enabled="true" />
<defaultDocument>
<files>
<clear />
<add value="Default.htm" />
<add value="Default.asp" />
<add value="index.htm" />
<add value="index.html" />
<add value="iisstart.htm" />
<add value="default.aspx" />
<add value="index.php" />
</files>
</defaultDocument>
</system.webServer>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="3.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
Ok, thanks for the reply .. Here is the ~/Views/Web.config
<?xml version="1.0"?>
<configuration>
<configSections>
<sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
<section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
<section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
</sectionGroup>
</configSections>
<system.net >
<mailSettings>
<smtp deliveryMethod="Network" from="support#greptech.co.za">
<network host="mail.greptech.co.za" userName="support#greptech.co.za" password="XXXX" port="25" />
</smtp>
</mailSettings>
</system.net>
<system.web.webPages.razor>
<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<pages pageBaseType="System.Web.Mvc.WebViewPage">
<namespaces>
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Routing" />
</namespaces>
</pages>
</system.web.webPages.razor>
<appSettings>
<add key="webpages:Enabled" value="false" />
</appSettings>
<system.web>
<httpHandlers>
<add path="*" verb="*" type="System.Web.HttpNotFoundHandler"/>
</httpHandlers>
<!--
Enabling request validation in view pages would cause validation to occur
after the input has already been processed by the controller. By default
MVC performs request validation before a controller processes the input.
To change this behavior apply the ValidateInputAttribute to a
controller or action.
-->
<pages
validateRequest="false"
pageParserFilterType="System.Web.Mvc.ViewTypeParserFilter, System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
pageBaseType="System.Web.Mvc.ViewPage, System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
userControlBaseType="System.Web.Mvc.ViewUserControl, System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
<controls>
<add assembly="System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" namespace="System.Web.Mvc" tagPrefix="mvc" />
</controls>
</pages>
</system.web>
<system.webServer>
<validation validateIntegratedModeConfiguration="false" />
<handlers>
<remove name="BlockViewHandler"/>
<add name="BlockViewHandler" path="*" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" />
</handlers>
</system.webServer>
</configuration>
I ended up going back to original Service Provider
I think I might have an idea what's going on looking at the debug output - this part doesn't look right:
Line 12: namespace ASP {
Line 13: using System;
Line 14: using System.Collections.Generic;
Line 15: using System.IO;
Line 16: using System.Linq;
Line 17: using System.Net;
Line 18: using System.Web;
Line 19: using System.Web.Helpers;
Line 20: using System.Web.Security;
Line 21: using System.Web.UI;
Line 22: using System.Web.WebPages;
Line 23: using System.Web.WebPages.Html;
Line 24: using WebMatrix.Data;
Line 25: using WebMatrix.WebData;
Line 26:
Line 27:
Line 28: public class _Page_index_cshtml : System.Web.WebPages.WebPage {
First your page inherits WebPages.WebPage - how did that happen? Then above.. notice the lack of System.Web.Mvc ? That's the reason you also get errors for missing 'BeginForm' and 'Partial' methods, because they are all part of the System.Web.Mvc namespace.
Here's how a fresh MVC 3 project output looks for me:
Line 12: namespace ASP {
Line 13: using System;
Line 14: using System.Collections.Generic;
Line 15: using System.IO;
Line 16: using System.Linq;
Line 17: using System.Net;
Line 18: using System.Web;
Line 19: using System.Web.Helpers;
Line 20: using System.Web.Security;
Line 21: using System.Web.UI;
Line 22: using System.Web.WebPages;
Line 23: using System.Web.Mvc;
Line 24: using System.Web.Mvc.Ajax;
Line 25: using System.Web.Mvc.Html;
Line 26: using System.Web.Routing;
Line 27:
Line 28:
Line 29: public class _Page_Views_Home_Index_cshtml : System.Web.Mvc.WebViewPage<dynamic> {
The config you listed above is perfectly fine. However the config in /Views is what affects the generation of views and my best guess is there's something wrong there. This is the bit that I noticed makes or breaks the view output (namespaces here are directly reflected in the generated code, similar to Problem recognizing html helpers in asp.net mvc 3 razor ):
<system.web.webPages.razor>
<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<pages pageBaseType="System.Web.Mvc.WebViewPage">
<namespaces>
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Routing" />
</namespaces>
</pages>
</system.web.webPages.razor>
At this point it seems like a good idea if you can also add the ~/Views/Web.config in case this is not the issue.
Ended up going back to original Service Provider

register dll in mvc razor view

In web forms we can write this code above the page:
<%# Register Assembly="xxx.CaptchaGenerator"
Namespace="xxx.CaptchaGenerator" TagPrefix="mycaptcha" %>
Then it can be used:
<mycaptcha:CaptchaControl ID="ccJoin"
runat="server" CaptchaHeight="31" CaptchaLength="5" />
How can I do this in mvc3, razor? I use this syntax #Using xxx.CaptchaGenerator and add this lines in my web config :
<pages>
<namespaces>
<add namespace="xxx.CaptchaGenerator" />
<add namespace="System.Web.Helpers" />
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Routing" />
<add namespace="System.Web.WebPages"/>
</namespaces>
</pages>
but it did not work.
You could add namespaces to the ~/Views/web.config, not ~/web.config. For example:
<system.web.webPages.razor>
<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<pages pageBaseType="System.Web.Mvc.WebViewPage">
<namespaces>
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Routing" />
<add namespace="xxx.CaptchaGenerator" />
</namespaces>
</pages>
</system.web.webPages.razor>
as i know cant use the server control in view(razorengine)..
you can use aspx file and then call it in your parent view with html.partial(aspx fileName)
i know this not a good solution and maybe not correct answer but its useful dear mohsen
شاید راه دیگه ایم باشه

Html.BeginForm() with Razor Generator

I am making a Helper with razor generator to build a cutom control in which there will be two radio buttons (yes/no)
I want to use Html.BeginForm for this.
But can't do it.
Do you know how to do it or another way to do it ?
Thanks.
Edit :
When I put this code
#using (Html.BeginForm()) {
<input type="radio"/>
<input type="radio"/>
}
I get this error
'System.Web.WebPages.HtmlHelper' does not contain a definition for BeginForm and no extension method
'BeginForm' accepting a first argument of type 'System.Web.WebPages.Html.HtmlHelper'could be found
It's most likely a namespacing or reference issue.
First, check to make sure that System.Web.WebPages shows up in your project references. Then add
#using System.Web.WebPages
to the top of your view. If that fixes it, you can move the reference into the web.config per this answer on the same type of topic
Add this code to your helper function when your helper functions are located in the App_Code folder.
var Html = ((System.Web.Mvc.WebViewPage)WebPageContext.Current.Page).Html;
var Ajax = ((System.Web.Mvc.WebViewPage)WebPageContext.Current.Page).Ajax;
Don't forget to include the right namespaces in the file: #using ....
For me it was a missing namespace, yes, but not the one mentioned by eouw0o83hf:
#using System.Web.Mvc.Html;
Please post your code when you ask a question so we can see what you're doing wrong. Try this:
#using (Html.BeginForm()) {
<input type="radio"/>
<input type="radio"/>
}
I ran into the same issue and just solved it. I changed version in the web.config file under the views folder.
Here is my previous code
<system.web.webPages.razor>
<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, **Version=5.0.2.2**, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<pages pageBaseType="System.Web.Mvc.WebViewPage">
<namespaces>
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Optimization"/>
<add namespace="System.Web.Routing" />
<add namespace="CMSSol" />
</namespaces>
</pages>
and here is my new code
<system.web.webPages.razor>
<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, **Version=5.0.0.0**, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<pages pageBaseType="System.Web.Mvc.WebViewPage">
<namespaces>
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Optimization"/>
<add namespace="System.Web.Routing" />
<add namespace="CMSSol" />
</namespaces>
</pages>

MVC extension method error

Hi i've got an extension method in my PagingHelpers class:
namespace SportsStore.WebUI.HtmlHelpers
{
public static class PagingHelpers
{
public static MvcHtmlString PageLinks(this HtmlHelper html,
PagingInfo pagingInfo,
Func<int, string> pageUrl)
{
StringBuilder result = new StringBuilder();
for (int i = 1; i < pagingInfo.TotalPages; i++)
{
TagBuilder tag = new TagBuilder("a");
tag.MergeAttribute("href", pageUrl(i));
tag.InnerHtml = i.ToString();
if (i == pagingInfo.CurrentPage)
tag.AddCssClass("selected");
result.Append(tag.ToString());
}
return MvcHtmlString.Create(result.ToString());
}
}
}
here i call extension method in the List.cshtml:
#Html.PageLinks(Model.PagingInfo, x => Url.Action("List", new {page = x}))
And I got this error:
'System.Web.Mvc.HtmlHelper'
does not contain a definition for 'PageLinks' and no extension method
'PageLinks' accepting a first argument of type
'System.Web.Mvc.HtmlHelper'
could be found (are you missing a using directive or an assembly
reference?)
I added the namespace in the web.config inside the Views Folder:
<pages>
<namespaces>
<add namespace="System.Web.Helpers" />
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Routing" />
<add namespace="System.Web.WebPages"/>
<add namespace="SportsStore.WebUI.HtmlHelpers"/>**
</namespaces>
</pages>
Please help me, I don't know how could I solve this problem
Try adding
#using SportsStore.WebUI.HtmlHelpers;
to the top of your .cshtml file
your namespace approach should work as well, so try to shut down the server rebuild your solution and run again
It appears you have added the namespace reference to the root web.config file.
If you are using MVC3 with the Razor view engine, you have to add the namespace reference to the Views\web.config file. Then it will be globally available to all views within the View folder.
<system.web.webPages.razor>
<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<pages pageBaseType="System.Web.Mvc.WebViewPage">
<namespaces>
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Routing" />
<add namespace="SportsStore.WebUI.HtmlHelpers"/>
</namespaces>
</pages>
</system.web.webPages.razor>
It seems you did everything correctly.
Did you compile your web project before trying to use the Html helper?
You need to add in your List.cshtml above the code this line
#model SportsStore.WebUI.Models.ProductsListViewModel

How to register assembly in Razor view engine

How can i insert this in razor view page
<%# Register Assembly="System.Web.Silverlight" Namespace="System.Web.UI.SilverlightControls"
TagPrefix="asp" %>
<asp:ScriptManager runat="server" ID="MainScriptManager" />
You can put it in the Web.Config that exists in your Views folder. It took me a while to figure this one out hope this helps.
<system.web>
<controls>
<add assembly="System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" namespace="System.Web.Mvc" tagPrefix="mvc" />
<add assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" tagPrefix="ajaxToolkit" />
</controls>
</system.web>
You cannot. You are using ASPX markup in your example. In razor you can write:
#using System.Web.Silverlight;
btw. check this syntax quickref:
you can try #using Namespace; where Namespace is what you need
To add new register in MVC, you can put in the web.config:
<configuration>
<system.web>
<pages>
<controls>
<add assembly="System.Web.Silverlight" namespace="System.Web.UI.SilverlightControls" tagPrefix="asp" />
</controls>
</pages>
</system.web>
</configuration>

Resources