I would like to show google maps in my asp.net MVC application. I tried to use J M Elosegui's solution, but when I implement it in my View I get an error for:
<div style="height: 500px; border: solid 1px #cccccc">
#(Html.GoogleMap()
.Name("map")
.Width((int)ViewData["width"])
.Height((int)ViewData["height"]))
</div>
with the error code:
Error 4 'System.Web.Mvc.HtmlHelper' does not contain a definition for 'GoogleMap' and no extension method 'GoogleMap' accepting a first argument of type 'System.Web.Mvc.HtmlHelper' could be found (are you missing a using directive or an assembly reference?)
This error occurs since GoogleMap extension method could not be resolved.
Add the reference to Jmelosegui.Mvc.Googlemap namespace on top of your view:
#using Jmelosegui.Mvc.Googlemap
Another option (in order to avoid adding this using clause to all your Razor views) to add it to the <namespaces> section of your ~/Views/web.config file:
<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="Jmelosegui.Mvc.Googlemap"/>
</namespaces>
</pages>
</system.web.webPages.razor>
Related
I have MVC project on VS 2012, when i open the view it's not displaying the code correctly, i mean everything [server side code] in black color.
Can anyone tell me why?
Some guesses:
Make sure there is a appsetting key like <add key="webpages:Version" value="3.0.0.0" /> in your web.config file reference. Make sure version is correct
Also make sure you have correctly configured <system.web.webPages.razor> section in web.config file in views folder, it may look like:
<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>
</namespaces>
</pages>
</system.web.webPages.razor>
Hope it'l help.
I'm rendering a dropdownlist that has an integer value and the SelectListItem value attribute only accepts a string thus the need for conversion. .ToString() function cannot be used.
#Html.DropdownList("ddl", Model.recordList.Select(
q => new SelectListItem
{
Text = q.recordName,
Value = SqlFunctions.StringConvert(q.recordId)
}
, "choose one")
// recordId is an Integer
I get assembly reference error on runtime:
The type or namespace name 'Objects' does not exist in the namespace
'System.Data' (are you missing an assembly reference?)
There is a project reference to System.Data.Entity already and #using System.Data.Objects.SqlClient in my View. I'm expecting this too as it doesn't appear in Intellisense. I can use from Controllers but not in Views.
Am I breaking any MVC rule here? Or probably have it misconfigured?
It's a misconfiguration.
To resolve, I had to:
1) add it in the assembly references in the Views/Web.config.
<system.web.webPages.razor>
<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=4.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="System.Data.Entity" />
<add namespace="System.Data.Objects.SqlClient" />
</namespaces>
</pages>
</system.web.webPages.razor>
2) Change the Project > References > System.Data.Entity property Copy Local = True. The assembly was not getting copied to the bin and thus identifies as missing.
I've got MVC3Contrib installed.. strange thing is, the examples show that I could use them to render links in razor pages, something like this:
#( Html.ActionLink<HomeController>(c => c.Index(), "Go home") )
This somehow never works for me.. Although, in my controllers, I can do
return new RedirectToRoute<MyController>(c => c.Index());
just fine.. The error I get is The non generic link ActionLink cannot be used with type arguments
It is as if the contrib isn't installed.. Infact, I don't even see the mvc future action link option in the intellisense
I just can't figure out why it is behaving so, Do I need to do anything extra here?
If you need any other info, please ask, I don't know what else I should be giving out here..
EDIT
Oh btw, I'm using MVC Areas, if that matters, I have about 3 areas and then the main controllers etc..
Make sure that the Microsoft.Web.Mvc namespace is in scope which is where thos extension methods are defined:
or add it to the <namespaces> section of your ~/Views/web.config file in order to bring this namespace in all views:
<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="Microsoft.Web.Mvc" />
</namespaces>
</pages>
</system.web.webPages.razor>
I suppose that in your controller code you have added using MvcContrib.ActionResults; which is why you are able to see the RedirectToRouteResult<T> class available.
I am using the Html.DropDownListFor in my MVC application to allow users to select a Category. I am creating the SelectList dynamically by using Linq's Select method on a collection of objects in my view model. Here is the code:
<%:Html.DropDownListFor(m => m.SelectedCategoryId,
Model.Categories.Select(c => new SelectListItem { Text = c.Name, Value = c.Id.ToString(), Selected = Model.SelectedCategoryId == c.Id}).ToList(),
new{onchange = "this.form.submit();"})%>
It works but I'm wondering why code completion doesn't work and why intelliscense is not picking up the Select method for my List of Category objects. When I run the page I have no errors, but I'm wondering why Visual Studio thinks there are errors with the code.
On a side note, this is a bit more code than I would like in a View-- does it make sense to include the SelectList as part of my view model?
Yes, your view model should contains the SelectListItem list itself; it shouldn't be derived on the view. It sounds like you don't have System.Linq referenced on your view; check your view level web.config and see if you have the following namespace in your file:
<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="System.Linq"/>
</namespaces>
</pages>
Virtually every .aspx page I have in my web site needs to have this at its top to function correctly:
<%# Assembly Name="System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %>
Is there anyway I can avoid having to declare this in the .aspx view for every page? Isn't there some way I can declare this globally for all .aspx views? Maybe something in the web.config?
Add it to assemblies
<assemblies>
<add assembly="System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
</assemblies>
The #Assembly directive correspond to assemblies tag in web.config not namespace tag. Check MSDN reference
You can declare it in web.config in the assemblies section, like this:
<system.web>
<compilation>
<assemblies>
<add assembly="System.Web.Mvc, Version=3.0.0.0 ... "/>
</assemblies>
</compilation>
</system.web>
However, according to the MSDN docs:
Assemblies that reside in your Web application's \Bin directory are automatically linked to ASP.NET files within that application. Such assemblies do not require the # Assembly directive. You can disable this functionality by removing the following line from the section of your application's Web.config file:
<add assembly="*"/>
As others have pointed out, you can declare this in the web.config pages section.
Another alternative (if its available to you) is to use the new Razor View engine. It not only removes this type of code, but also provides cleaner, overall syntax. Of course, I realize this may not be a viable solution as you may be limited by your current technology/customer needs/etc.
An example of what you may see at the top of a Razor page is shown here:
#model Some.StronglyTyped.Model
#using Other.Libraries.To.Import
#{
ViewBag.Title = "Specific Page Title";
}
Put it in the Web.config as a global namespace. It will be available to all your pages there.
<system.web>
<pages>
<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>