Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I avoid adding @using in my cshtml tags?

Tags:

asp.net-mvc

I am learning ASP.NET MVC with Razor. I noticed that sometimes some cshtml files already have some namespaces imported (example for NopCommerce, which I am using to learn)

@model ProductListModel
@using Telerik.Web.Mvc.UI

Or

@{
    Layout = "~/Views/Shared/_ColumnsOne.cshtml";

    Html.AppendScriptParts(@Url.Content("~/Scripts/jquery.fileupload.js"));
    Html.AppendScriptParts(@Url.Content("~/Scripts/jquery.lightbox-0.5.min.js"));
    Html.AppendCssFileParts(@Url.Content("~/Content/Style/jquery.fileupload-ui.css"));
    Html.AppendCssFileParts(@Url.Content("~/Content/Style/jquery.lightbox-0.5.css"));
}

(Html.AppendScriptsParts is declared in the nop.Web.Framework.UI) If I make my own cshtml file, I need to add the "using" line or reference with the like this:

@using System.Linq;
@using Nop.Web.Framework.UI

@model Nop.Admin.Models.Proposal.ProposalListModel

Is there any trick I am missing to reference these items? Why are the first 2 examples simpler and do not need the explicit reference?

Thanks!

like image 253
JSBach Avatar asked Sep 22 '12 20:09

JSBach


1 Answers

Add your namespace here ...\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="Your.Namespace" />  
      </namespaces>
    </pages>
  </system.web.webPages.razor>
like image 60
webdeveloper Avatar answered Nov 09 '22 09:11

webdeveloper