We want to use Orchard for a website. We are creating a custom module/widget for that cms and in that module we want to use a GridView from DevExpress to show data. We got most of it working, but we can't get callbacks to work. With that i mean things like navigating through pages, sorting rows and moving columns.
If we look in the console we can see that the javascript and ajax callbacks are never executed, we can't figure out why that is so. I have found some topics on the DevExpress site and this site about using DevExpress with Orchard, but i couldn't find anything usefull (for my case) in those. We also noticed that the methods of our controller are never called, but cannot figure out why not.
I found that sometimes jQuery can cause problems for DevExpress controls, so i tried removing all jQuery scripts, but that didn't make a difference. Someone also suggested to put a callbackpanel around te gridview, but that didn't work either. I have tried many more things (which i mostly forgot already) but nothing worked so far.
I have also asked the same question on the DevExpress website end the Orchard forums but i'm not getting any answers there, so i thought i'd try my luck here.
I have made an example project in case you want to see what i'm trying to do. The file is 40MB because i added the entire cms to itwith example daabase, including our module. The module is called GridViewTest You can find the source here:http://www.obec.nl/download/Orchard-DevExpress.zip.
I have finally found a solution. It turned out to be a pretty simple one (like usual) and i want to share it here, in case other people want to use DevExpress with Orchard:
In your Orchard module, you have to create a Routes.cs file (in the root of the module). There you have to add this:
using System.Collections.Generic;
using System.Web.Mvc;
using System.Web.Routing;
using Orchard.Mvc.Routes;
namespace CentralStationDataView
{
public class Routes : IRouteProvider
{
public void GetRoutes(ICollection<RouteDescriptor> routes)
{
foreach (var routeDescriptor in this.GetRoutes())
{
routes.Add(routeDescriptor);
}
}
public IEnumerable<RouteDescriptor> GetRoutes()
{
return new[]
{
new RouteDescriptor
{
Priority = 5,
Route = new Route(
"AreaName",
new RouteValueDictionary
{
{ "area", "AreaName" },
{ "controller", "ControllerName" },
{ "action", "ActionName" }
},
new RouteValueDictionary(),
new RouteValueDictionary
{
{ "area", "AreaName" }
},
new MvcRouteHandler())
}
};
}
}
}
You can make the AreaName up as you like, it doesn't matter (as far as i know) what you call it. Make sure that you don't add the "Controller" suffix to the ControllerName.
Then, in your GridView settings you have to add this:
settings.CallbackRouteValues = new { area = "AreaName", Controller = "ControllerName", Action = "ViewDataPartial" };
These values have to all be exactly the same as the values in the Routes.cs file. The "area" property was critical for me, i already had the Routes.cs file and everything, but i didn;t add the area property to the CallbackRouteValues.
The second part of the solution is that you have to make a partial view with only and i stress, only, the GridView inside it. So no scripts, no extra html elements, no text, nothing.
Related
My team is building a simple MVC site for very low end "feature" phones. One problem we have is that certain phone aggressively cache HTML, so what the user actually gets to see is not what we intend. An extreme example is, a user registers and gets a "thank you page". Another user on the same device then tries to register. The phone simply serves the cached page without creating a new account.
In the past I've dealt with this by adding a "cache buster" querystring to things I don't want cached, eg all pages will be served in the format
http://domain.com/controller/route?cb=somerandomstringofnumbers
In this case we'd need to do this for all URLs in the site - this includes the URLs auto-generated by Controller actions such as RedirectToAction or Redirect and also the Razor Url.Action, Html.BeginForm, Html.ActionLink etc.
Now obviously I could decorate the Razor HTML helpers (or extend them) and add the argument to an controller action, but it seems to me that because the actual URLs generated by these built in methods are auto-generated from the Controller/Action params passed in, there should be a way to hijack that process.
Unfortunately the MS classes are protected - I'm mostly looking in System.Web.Routing.Routes.
I've tried a few things I've found online but they are not MVC5 (dating back to 2008) and it seems the framework has changed significantly.
eg, from
http://forums.asp.net/t/1216840.aspx?Append+value+to+all+urls+built+by+RouteCollection+GetUrl
public class SessionAppendingRouteHandler : IRouteHandler
{
public IHttpHandler GetHandler(RequestContext context)
{
SessionAppendingHttpHandler handler = new SessionAppendingHttpHandler();
handler.RequestContext = context;
return handler;
}
}
public class SessionAppendingHttpHandler : MvcHandler
{
public override ProcessRequest(RequestContext context)
{
//append your sid here
}
}
// and in the route setup
RouteTable.Routes.Add( new Route
{
Url = "/[controller].mvc/[action]/",
Defaults = new { action = "index" },
RouteHandler = typeof(SessionAppendingRouteHandler)
});
This I cant get to work as the framework has changed too much, but it looks very close to what I would like to achieve.
I feel like I'm in the right area, but I've hit a brick wall.
Any suggestions?
This is quite old, but let me answer based on how I solved a similar problem:
Instead of having it as query string, have the cb as a route value just as action and controller are route values. You can do this by registering a route; for instance:
routes.MapRoute(
name: "CB",
url: "{cb}/{controller}/{action}/{id}",
defaults: new { cb = "3600", area = "", id = UrlParameter.Optional }
);
If the value for cb is not a constant, then you can find a convenient point to set the cb for each user session. A good place will be after a successful login. With this, you'll now need to provide just two custom methods for RedirectToAction and ActionLink. Your implementation will simply package a RouteValueDictionary and then pass it in to MVC's own implementation using the appropriate overloads.
In an ASP.NET MVC 4 project, I've added an MVC Controller with scaffolding from an EF class. I.e, CRUD operations. They all work. If I add action methods by hand, add an Html.ActionLink() to a view pointing to them, I get 404 errors.
For example, in my controller I add the AddImage method:
public ActionResult AddImage(int id)
{
var car = db.Cars.Find(id);
return View("AddImage",car);
}
This just returns a view to add images associated with the Car object. In the corresponding Index.cshtml, I add:
#Ajax.ActionLink("Add Image", "AddImage", new { id = item.CarId }, new AjaxOptions { UpdateTargetId = "modal",OnSuccess="showDialog" })
When it renders, in the console, I see a 404 when that link is clicked. Another weird thing is that when I run in the debugger, a breakpoint set in the method is hollow with a little warning icon, says no executable code is associated with this line.
I have added nothing to the RouteConfig: it just has the Default, which should work. I had an overload with an HttpPost attribute, but even without that, I still get a 404.
In previous projects, I can Action methods with impunity and they all work. So what is the problem here?
Please help, Stackoverflow, you're my only hope.
With the help of David in the comments, I think I figured out what I did to get my project in a weird state. To be brief: I added stuff to my model including adding a migration without calling Update-Database. I tried running the app without the database being up to date.
I wasn't getting an error that said as much, but after restarting VS, deleting the bin and obj folders for the project, changing the output path to just bin/ instead of bin/Debug (found on many Stackoverflow questions), I finally got the right error message.
Then I ran my update, et voila! Working project.
write this
public ActionResult AddImage(int id)
{
var car = db.Cars.Find(id);
return View(car);
}
and your link must be 'AddImage' without empty space
#Ajax.ActionLink("AddImage", "AddImage", new { id = item.CarId }, new AjaxOptions { UpdateTargetId = "modal",OnSuccess="showDialog" })
I used this great guide to localize my ASP.NET MVC 2 application, which I followed almost to the letter. The app is mainly form based.
I was wondering if there was an easy way to be able to switch between languages in the middle of filling out a form without clearing the whole form, and having to start over? If not, could you suggest a way of localizing an application that would support this?
Maybe that's not a thing...
The approach used in the article is not the best one to keep the localisation I guess.
But what you can do is the following:
Handle event when user clicks on the language link.
Change language via ajax preventing browser to go to the actual link.
Submit the form that user is editing adding parameter saying "make sure you don't save".
The server would re-render the form as normally with the data posted, but in the new language.
JavaScript pseudocode:
var submitCurrentForm = function() {
$("form:last").submit({
data { dontSave: "True"} // this is just meta, you can use QueryString or hidden input
});
}
var switchLanguage = function(href, done) {
$.post(href).success(done); // using jQuery deferred
}
$("a.lang").click(function(e) {
e.preventDefault();
switchLanguage(this.href, submitCurrentForm);
});
Controller pseudocode:
public ActionResult Create(YourStuff stuff, bool dontSave = false) {
if (!dontSave)
ProcessTheStuff();
return View(stuff);
}
Not the best solution, but the easiest one you can go with ATM.
I have only tried this in single project areas. So if anyone tries this in a multi-project areas solution please let us know.
Area support was added to MVC2. However the views for your controllers have to be in your main Views folder. The solution I present here will allow you to keep your area specific views in each area. If your project is structured like below, with Blog being an area.
+ Areas <-- folder
+ Blog <-- folder
+ Views <-- folder
+ Shared <-- folder
Index.aspx
Create.aspx
Edit.aspx
+ Content
+ Controllers
...
ViewEngine.cs
Add this code to the Application_Start method in Global.asax.cs. It will clear your current view engines and use our new ViewEngine instead.
// Area Aware View Engine
ViewEngines.Engines.Clear();
ViewEngines.Engines.Add(new AreaViewEngine());
Then create a file named ViewEngine.cs and add the code below.
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web.Mvc;
namespace MyNamespace
{
public class AreaViewEngine : WebFormViewEngine
{
public AreaViewEngine()
{
// {0} = View name
// {1} = Controller name
// Master Page locations
MasterLocationFormats = new[] { "~/Views/{1}/{0}.master"
, "~/Views/Shared/{0}.master"
};
// View locations
ViewLocationFormats = new[] { "~/Views/{1}/{0}.aspx"
, "~/Views/{1}/{0}.ascx"
, "~/Views/Shared/{0}.aspx"
, "~/Views/Shared/{0}.ascx"
, "~/Areas/{1}/Views/{0}.aspx"
, "~/Areas/{1}/Views/{0}.ascx"
, "~/Areas/{1}/Views/Shared/{0}.aspx"
, "~/Areas/{1}/Views/Shared/{0}.ascx"
};
// Partial view locations
PartialViewLocationFormats = ViewLocationFormats;
}
protected override IView CreatePartialView(ControllerContext controllerContext, string partialPath)
{
return new WebFormView(partialPath, null);
}
protected override IView CreateView(ControllerContext controllerContext, string viewPath, string masterPath)
{
return new WebFormView(viewPath, masterPath);
}
} // End Class AreaViewEngine
} // End Namespace
This will find and use the views you have created in your areas.
This is one possible solution that allows me to keep views in the specified area. Does anyone else have a different, better, enhanced solution?
Thanks
I'm sorry to be the one to tell you this, but you must be missing something. I currently have your scenario working out of the box with ASP.NET MVC 2 RC.
I assume you have all the register routes and have the correct web.config files inside your area's view folder?
Maybe have a look at this walk through, especially the part about creating the areas.
HTHs,
Charles
EDIT:
Ok, so you're not happy about putting in the extra new { area = "blog' }, null - fair enough, I'll admit its niggly... but what else are you going to do?
What happens when you have two controllers with the same name? One in your root project and one in an area or two controllers with the same name in two different areas? How is it going to find the correct view?
Also, I do see a problem with your ViewLocationFormats. All of the area view locations have no reference to their area... e.g. ~/Areas/{1}/Views/{0}.ascx - how does it know what area?
If you are suggesting that all the different area's views and all thrown into the Areas folder under their controller name and then found under Views and Views/Shared - I would highly recommend against that... It'll become a mess very quickly.
So where does that leave you? It really leaves you needing to specify the area when creating the route. It really boils down to the fact that although it's niggly having to specify the area, there really is no other way.
This solution works well in Mvc2. It is not necessary in Mvc3.
I am building a product catalog for a customer website under ASP.NET using .NET Framework 3.5 SP1. Each product has a part number and an OEM part number (all globally unique).
For SEO purposes I would like the OEM part number to be as close as possible to the actual domain name.
How do I build a routing rule that allows me to do this:
http://www.myDomain.com/oemPartNumber
http://www.myDomain.com/myPartNumber
while still being able to do this:
http://www.myDomain.com/welcome
http://www.myDomain.com/products
http://www.myDomain.com/services
http://www.myDomain.com/contact
I would also love to hear your other SEO suggestions (we are primarily interested in Google) if you have any.
Thanks.
IMPORTANT: This not an MVC site, so I don't have controllers.
You should be able to specify something like http://www.mydomain.com/oempartnumber/oem and http://www.mydomain.com/mypartnumber/pn. There must be something in the url that allows you to choose the controller you want to use and further more allow you to distinguish between a part number and an oem part number (unless those are also unique against one another. If there will never be overlap between oem and pn then you could have http://www.mydomain.com/{partnumber}/pn.
RouteTable.Routes.Add(new Route
{
Url = "[query]/pn",
Defaults = new { controller="PartNumber", action = "Details" },
RouteHandler = typeof(MvcRouteHanderl)
});
You could use some trickery with a route like this:
routes.MapRoute(
"Part number",
"{partNumber}",
new { controller = "Part", action = "Display" },
new
{
partNumber = #"\d+" // part number must be numeric
}
);
But the problem here is that an OEM part number that is not actually a part number (such as "ave-345") would not match!
UPDATE: In reading I noticed that you said "this is not an MVC site so I don't have controllers!"...OH! That changes things. In that case you can check to see if the directory exists where you pass in http://www.mydomain.com/1234 and if not you can test it for a product number. This would have to be done in a HttpModule though so you can catch it before your page is executed. Then on the server side you can direct the page to http://www.domain.com/productdetails?pid=1234.
Take a look here to understand that: http://www.15seconds.com/Issue/020417.htm
For this you will have a class that inherits from IHttpModule. Then you can specify an Init method
public void Init(HttpApplication application)
{
//let's register our event handler
application.PostResolveRequestCache +=
(new EventHandler(this.Application_OnAfterProcess));
}
This then points to your Applicaton_OnAfterProcess method:
private void Application_OnAfterProcess(object source, EventArgs e)
{
HttpApplication application = (HttpApplication)source;
HttpContext context = application.Context;
...
Inside of here you can specify some rules about what you are looking for.
I usually do something along the lines of
if (!System.IO.File.Exists(application.Request.PhysicalPath)) //doesn't exist
{
//you test for your product ID here
...
//if you find it stuff it into a ProductID variable for later...
Once you isolate your product ID you can then rewrite the URL (server side) and direct the user to the proper productDetails.aspx page.
context.RewritePath("~/products/productDetails.aspx?ProductID=" + ProductID.ToString());
So while the user and google sees http://www.mydomain.com/1234 your application will see http://www.mydomain.com/products/productdetails.aspx?productid=1234 and you can code against it as usual.
I hope this is what you were looking for instead!
If there are specific formats to the part numbers you can use regex constraints on the route like this:
routes.MapRoute(
"Part number",
"{partNumber}",
new { controller = "Part", action = "Display" },
new
{
partNumber = #"\d+" // part number must be numeric
}
);
Text like "welcome" won't match the regex and so will ignore this route.
I see that you have already accepted an answer, but let me post a new one that is simpler.
This not an MVC site, so I don't have
controllers.
If you use Web Forms, this is for you: Using Routing with Web Forms
Basically, you have to create your own IRouteHandler and set up your routes to it. Then, you can use it to map as many URLs as you want to your Web Forms. This saves you from the old URL rewriting method and such dirty tricks. (The download link on that blog entry doesn't work anymore, but this is pretty simple to imlement. Anyways, if you need a working example, I can provide you with one.)