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.
Related
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>
I'm having a minor annoyance with VS 2013 when working in ASP.NET MVC Razor views. When typing something like #Html.TextBoxFor(m => m.[PropertyName]), as soon as I type the first m, intellisense pops up a drop down with mbox selected. Then when I press the space key it inserts mbox into my code. Is there anything I can do, besides turning off intellisense, so that I can type my code without having to dismiss the intellisense suggesetion? If I type #Html.TextBoxFor() first, and then go back and fill in the expression, I get the desired behavior, but that is no more convenient.
Edit
I don't have anything imported directly in this view. In the web.configs I have the following:
<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="EPiServer.Shell.Web.Mvc.Html" />
<add namespace="EPiServer.Framework.Web.Mvc.Html" />
<add namespace="EPiServer.Web.Mvc.Html" />
<add namespace="LGMVCWeb.ViewModels"/>
<add namespace="LGMVCWeb.Models.Pages.OE"/>
<add namespace="LGMVCWeb.ViewModels.Pages.OE"/>
<add namespace="LGMVCWeb.ViewModels.Shared.OE"/>
I have the following string extension method:
public static string Truncate(this string value, int maxLength = 75)
{
/* ... */
}
I want to use it in views, and it works properly with literal strings. But when I have a model instance (let's call it object) in ViewBag and try to Truncate one of its string properties, I get a RuntimeBinderException:
"string" does not contain a definition for "Truncate"
Why does it happen? Can I fix that?
Thanks.
Because the ViewBag is a dynamic type, extension methods will not be picked up on their properties without casting to the appropriate type first.
For example:
#(((string)ViewBag.stringProperty).Trunctate())
Personally I limit my usage of ViewBag to avoid having to cast everywhere (amongst other reasons). Instead I use strongly typed view models for each view.
For sake of completeness, this error could also be encountered if the namespace that contains your extension is not referenced in your view.
You can add the namespace in the web.config that sits in your views folder:
Add namespace to the following section:
<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="MyProject.Extensions" />
</namespaces>
</pages>
</system.web.webPages.razor>
Alternatively, you can import the namespace in the razor view itself:
#using MyProject.Extensions
I do something like (clean)
#{
string areaName = ViewBag.AreaName;
}
then...
<h4>#areaName.ExtensionMethod()</h4>
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>