How to register assembly in Razor view engine - asp.net-mvc-3

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>

Related

Telerik RadUpload.Net2 null reference when using asp:input

I've been trying to use RadUpload.Net2 from Telerik using a simple .aspx page, but i'm getting a null reference for the next line of code:
UploadedFile file = RadUploadContext.Current.UploadedFiles[File1.UniqueID];
when using the following controls:
<input id="File1" type="file" runat="server" />
<asp:Button ID="Button1" runat="server" Text="Button" Enabled="true" OnClick="Button1_Click"/>
<radUpload:RadProgressManager runat="server" ID="Radprogressmanager"/>
<radUpload:RadProgressArea ID="RadProgressArea1" runat="server" />
I'm using IIS 7.5 with the DefaultAppPool, .Net 4.0 Integrated. The WebConfig is as follows:
<configuration>
<appSettings>
<add key="Telerik.WebControls.RadControlsDir" value="~/Resources/RadControls/" />
<add key="Telerik.WebControls.RadUpload.TempFolder" value="D:\Projects\ODW\Temp\Upload" />
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.0">
<assemblies>
<add assembly="RadUpload.Net2, Version=2.4.1.0, Culture=neutral, PublicKeyToken=B4E93C26A31A21F0" />
<add assembly="System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A" />
<add assembly="System.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" />
</assemblies>
</compilation>
<pages enableEventValidation="false" validateRequest="false" controlRenderingCompatibilityVersion="3.5" clientIDMode="AutoID" />
<httpRuntime maxRequestLength="30240" executionTimeout="10" />
<!--<httpModules>
<add name="RadUploadModule" type="Telerik.WebControls.RadUploadHttpModule, RadUpload.Net2, Version=2.3.0.0, Culture=neutral, PublicKeyToken=b4e93c26a31a21f0" />
</httpModules>
<httpHandlers>
<add verb="*" path="Telerik.RadUploadProgressHandler.aspx" type="Telerik.WebControls.RadUploadProgressHandler, RadUpload.Net2, Version=2.3.0.0, Culture=neutral, PublicKeyToken=b4e93c26a31a21f0" validate="false" />
</httpHandlers>-->
<trace enabled="false" pageOutput="false" requestLimit="40" localOnly="false" />
<authorization>
<allow users="*" />
</authorization>
<trust level="Full" />
</system.web>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true">
<add name="RadUploadModule" type="Telerik.WebControls.RadUploadHttpModule, RadUpload.Net2, Version=2.4.1.0, Culture=neutral, PublicKeyToken=b4e93c26a31a21f0" preCondition="integratedMode" />
</modules>
<validation validateIntegratedModeConfiguration="false" />
<handlers>
<add name="Telerik_RadUploadProgressHandler_ashx" verb="*" path="Telerik.RadUploadProgressHandler.aspx" type="Telerik.WebControls.RadUploadProgressHandler, RadUpload.Net2, Version=2.4.1.0, Culture=neutral, PublicKeyToken=b4e93c26a31a21f0" preCondition="integratedMode" />
</handlers>
</system.webServer>
</configuration>
The handler is registered in IIS. I've tried it on classic .Net app pools an it's still not working.
The RadUpload.Net2 is a very archaic control (pre-2007), you should be using the Telerik.Web.UI assembly if your project is on .NET 4

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

MVC3 doesn't recognize MvcContrib namespace in Razor View

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.

Resources