I'm Following this article http://timdwilson.github.io/typeahead-mvc-model/
and in the (e) part of this article it uses #Html.AutocompleteFor but my project dosen't recognize it. it has the Error : Html helper dose not contain a definition for autocompleteFor
Appreciate any idea to help. thanks.
#Html.AutocompleteFor() is an extension method for the HtmlHelper class.
Somewhere in the code you have downloaded, you will find a method that will have a signature similar to
public static MvcHtmlString AutocompleteFor<TModel, TValue>(this HtmlHelper<TModel> htmlHelper ...... )
Looking at the images in your link, its most likely in the /Controllers/HtmlHelper.cs file. At the top of that file, beneath any using statements, you will find the name of the namespace
namespace xxxx
{
public static class .....
In the view, add (where xxxx is the name of the namespace)
#using xxxx
If you want to make this available in all your views, you can add it to web.config.csfile so that you do not need the using statement in the view
<system.web>
<pages>
<namespaces>
<add namespace="System.Web.Helpers" />
<add namespace="System.Web.Mvc" />
....
<add namespace="xxxx" /> // add the namespace here
</namespaces>
Related
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>
This question already has an answer here:
Generic Inherited ViewPage<> and new Property
(1 answer)
Closed 2 years ago.
In my MVC apps I normally declare a base view type that all of my views inherit from. I get a parser error when I specify Inherits="MyView" in my Page declaration, but not if I specify Inherits="MyApp.Web.Views.MyView".
Strangely enough, it works fine if I specify a strongly typed view name: Inherits="MyView<T> (where T is any valid type).
Why can I specify a strongly typed view without the full type name, but not a generic view?
My base view class is declared like this:
namespace MyApp.Web.Views {
public class MyView : MyView<object> {
}
public class MyView<TModel> : ViewPage<TModel> where TModel : class {
}
}
UPDATE: Note that I do import MyApp.Web.Views via web.config. If I did not, then the strongly typed approach (Inherits="MyView<T>") wouldn't work either.
From another post: Here's the underlying issue: the ASP.NET page parser does not support generics as a page type
Read more here- Generic Inherited ViewPage<> and new Property
You can add your namespace to the <namespaces> element of your web.config file then you should be able to use Inherits="MyView"
<pages>
<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="MyApp.Web.Views" />
</namespaces>
</pages>
Today I spent a good three hours trying to convert the project MvcContrib.Samples.InputBuilders, included in MVC Contrib to make it work with Spark View Engine, but so far was unable to do so.
Does anybody have a clue why these two just won't get along?
Changes I've made
InputForm.spark:
<viewdata model="SampleInput" />
!{Html.InputForm()}
Global.asax.cs:
...
protected void Application_Start() {
RegisterRoutes(RouteTable.Routes);
InputBuilder.BootStrap();
ViewEngines.Engines.Clear();
ViewEngines.Engines.Add(new SparkViewFactory());
}
Web.config:
...
<spark>
<compilation debug="true"/>
<pages automaticEncoding="true">
<namespaces>
<add namespace="System"/>
<add namespace="System.Collections.Generic"/>
<add namespace="System.Linq"/>
<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.UI.InputBuilder"/>
<add namespace="MvcContrib.UI.InputBuilder.Views"/>
<add namespace="Web.Models"/>
</namespaces>
</pages>
</spark>
(I copied the last three namespaces from the sample project.)
Errors I'm getting
Depending on the order in which I setup Spark/InputBuilder in Global.asax.cs, I get two different exceptions.
If I first setup InputBuilder, then Spark (code shown above):
error CS1061:
'System.Web.Mvc.HtmlHelper' does not
contain a definition for 'InputForm'
and no extension method 'InputForm'
accepting a first argument of type
'System.Web.Mvc.HtmlHelper' could be
found (are you missing a using
directive or an assembly reference?)
If I first setup Spark, then InputBuilder:
The view 'InputForm' or its master
could not be found. The following
locations were searched:
~/Views/Home/InputForm.aspx
~/Views/Shared/InputForm.aspx
~/Views/InputBuilders/InputForm.aspx
~/Views/Home/InputForm.ascx
~/Views/Shared/InputForm.ascx
Change input.spark to:
<use namespace="MvcContrib.UI.InputBuilder"/>
<use namespace="MvcContrib.UI.InputBuilder.Views"/>
<add namespace="Web.Models"/>
<viewdata model="SampleInput" />
!{Html.InputForm()}
Adding to web.config doesn't work in Spark. You can use _global.spark instead.
There is also another problem. In stable Spark, SparkView Html property is of type HtmlHelper, not HtmlHelper<TModel>. Html.InputForm() function works only for HtmlHelper<TModel>, so you will have to download Spark source and use development build, because it was changed recently. You can also download stable sources and change it yourself. Here is some info:
http://groups.google.com/group/spark-dev/browse_thread/thread/618bd44a94368d22/f7df24e52924f4dc?show_docid=f7df24e52924f4dc
Your on the right track with setting up the input builders and then the spark view engine.
You can see from the source file from mvccontrib that you need this namespace defined MvcContrib.UI.InputBuilder.Views for your view to reference the input builders HtmlHelper Extentions.
http://github.com/mvccontrib/MvcContrib/blob/master/src/MVCContrib/UI/InputBuilder/Views/HtmlExtensions.cs
I wrote the input builders but I do not know enough about the spark view engine to know why it is not resolving the reference to the extention methods for the input builders.
I've created an extension method:
namespace MyComp.Web.MVC.Html
{
public static class LinkExtensions
{
public static MvcHtmlString ActionImageLink(this HtmlHelper htmlHelper, string linkText, string imageSource, string actionName)
{
...
}
}
}
I've referenced the assembly from my mvc app, and I've tried importing the namespace in my view:
<%# Import Namespace="MyComp.Web.Mvc.Html" %>
and I've also added it to the web config file:
<pages>
<controls>
...
</controls>
<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"/>
<add namespace="System.Collections.Generic"/>
<add namespace="MyComp.Web.Mvc.Html"/>
</namespaces>
</pages>
In My view if I try to access Html.ActionImageLink I get an error saying that System.Web.Mvc.HtmlHelper does not contain a definition for ActionImageLink accepting a first argument type of System.Web.Mvc.HtmlHelper. I don't see any of the ActionLink extension methods for System.Web.Mvc.HtmlHelper, only for System.Web.Mvc.HtmlHelper, so how does it work for the .net framework, and not for me?
Notice the difference in the case of your namespace when declaring and when importing.
namespace MyComp.Web.MVC.Html
{
}
<%# Import Namespace="MyComp.Web.Mvc.Html" %>
<add namespace="MyComp.Web.Mvc.Html"/>
Namespaces are case-sensitive!
You must add the namespace in the web.config but in the one inside the Views Folder
Try shutting down Visual Studio and opening your Solution again. When things start acting weird, some times this helps.
Close and reopen Visual Studio did the trick!!
Does the VS intellisense autocompletes your extension method? Does it autocompletes standard MVC helpers methods? If not then the view complilation error occured. Make sure you have the proper "Inherits" attribute value in Page tag at the beginning of the view. If you use strongly typed views make sure the "strong type" exists and compiles.
Do you define the extension method in the same project where the view is defined? If not you have to add the reference in the mvc project. Finally check if the assembly with the extension method (MyComp.Web.Mvc.Html.dll?) is in the Bin folder of the application
Try to add the namespace declaration to the pages/namespaces section of the web.config file placed in your Views folder in MVC project (not the main project web.config file).
One of the reasons may be you are returning a MvcHtmlString and not a string.
Include the namespace for class MvcHtmlString. See if it helps.
I am having trouble with Crystal Reports when using charts and images which use CrystalImageHandler.aspx. The image cannot display and I suspect this is due to a problem with MVC routing.
The path image path is similar to this:
src="/CrystalImageHandler.aspx?dynamicimage=cr_tmp_image_a8301f51-26de-4869-be9f-c3c9ad9cc85e.png"
With the URL similar to this:
localhost:01234/ViewCrystalReports.aspx?id=50
The image cannot be found prumably because it's looking in a non-existant directory. How can I change the path CrystalImageHandler.aspx is located at? I think if I were to reference from the root the problem would be solved but anything I change in Web.Config fails to work.
I should mention this is on a conventional aspx page, not a view etc
I solve this problem editing Web.Config file
Insert the following line:
<system.web>
...
<httpHandlers>
<add path="CrystalImageHandler.aspx" verb="GET" type="CrystalDecisions.Web.CrystalImageHandler, CrystalDecisions.Web, Version=13.0.2000.0, Culture=neutral, PublicKeyToken=692fbea5521e1304"></add>
</httpHandlers>
...
*take care with write your number version (Version=xx.x.xxxx.x)
Figured it out. The routing was interfering with the CrystalImageHandler.aspx link that was being generated. Global.aspx has the following line to tell the routing engine to ignore resource files:
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
but this isn't a conventional resource file, it's an aspx file for some reason (anyone know why?)
adding this fixed it:
routes.IgnoreRoute("{resource}.aspx/{*pathInfo}");
public class CrystalImageHandlerController : Controller
{
//
// GET: /Reports/CrystalImageHandler.aspx
public ActionResult Index()
{
return Content("");
}
protected override void OnActionExecuted(ActionExecutedContext filterContext)
{
var handler = new CrystalDecisions.Web.CrystalImageHandler();
var app = (HttpApplication)filterContext.RequestContext.HttpContext.GetService(typeof(HttpApplication));
if (app == null) return;
handler.ProcessRequest(app.Context);
}
}
This controller will invoke the handler. Just add a route to this as CrystalImageHandler.aspx, it can also be used with any sub path you'd like (in this case /reports). Something I could NEVER get the handler to do via configuration.
To view in local machine,you will add the following code in web config
<httpHandlers>
<add verb="GET" path="CrystalImageHandler.aspx" type="CrystalDecisions.Web.CrystalImageHandler, CrystalDecisions.Web,Version=10.2.3600.0, Culture=neutral, PublicKeyToken=692fbea5521e1304" />
</httpHandlers>
...............................
<appSettings>
<add key="CrystalImageCleaner-AutoStart" value="true" />
<add key="CrystalImageCleaner-Sleep" value="60000" />
<add key="CrystalImageCleaner-Age" value="120000" />
</appSettings>
The following code is for displaying in server
<system.webServer>
<handlers>
<add name="CrystalImageHandler.aspx_GET" verb="GET" path="CrystalImageHandler.aspx" type="CrystalDecisions.Web.CrystalImageHandler, CrystalDecisions.Web, Version=10.2.3600.0, Culture=neutral, PublicKeyToken=692fbea5521e1304" preCondition="integratedMode"/>
</handlers>
</system.webServer>
:) I will solve that problem in adding in web config
It's because the routing was interfering with the CrystalImageHandler.aspx. So either in Global.asax or routeConfig file we can ignore route for .aspx extension files. You can ignore .aspx extension route by adding following line.
routes.IgnoreRoute("{allaspx}", new {allaspx=#"..aspx(/.*)?"});