How do I read Querystring variables in a Kooboo Module? - asp.net-mvc

I need to be able to pass an ID to my Kooboo module.
I was hoping to add {id} to the page route and then somehow pass it to the module put I can't seem to find how.
I want the Querystring variable to be passed into the MVC Controller action.
Any Suggestions?

Managed to figure it out
Add a route to a controller in Module.Config
Use
#Url.ModuleUrl().Action("ActionName")
or
#Url.ModuleUrl().Action("ActionName", new { parameterName = 0 })"
To get an url to another action/controller within you module

Related

Routing values to the MVC Player function?

In a Composite C1 application, I am trying to pass values from the URL to the MVC Player function, but I have trouble because the values are part of the path and not in the query string.
The URL looks like this:
/AuctionDetailsGallery/3624734/Test-Versteigerung-2
AuctionDetailsGallery is a Composite C1 Page which includes the MvcPlayer function.
3624734 is the (dynamic) ID, "Test-Versteigerung-2" is a userfriendly name
The MvcPlayer is then supposed to call
/AuctionViewer/FilterGalleryPositions
("AuctionViewer" is the controller and "FilterGalleryPositions" the action.)
The ID has to be passed to the action, but under a different name ("SelectedAuctions").
So essentially, if the user calls
/AuctionDetailsGallery/3624734/Test-Versteigerung-2
I want to render the MVC action
/AuctionViewer/FilterGalleryPositions?SelectedAuctions=3624734
How can I do this?
I set the MvcPlayer path to "/AuctionViewer/FilterGalleryPositions" and played around with the routes, but I always get the message
The controller for path '/3624734/Test-Versteigerung-2' was not found
or does not implement IController.
That's because the Render function checks for PathInfo and replaces the Path I set with the PathInfo if available. I guess it would be more useful if the PathInfo was appended, but I am unsure how to route my values with the current MVC Player implementation.
If I understand your question correctly I believe you are asking about routes.
With Attribute routing simply declare the route over the controller action (assuming controller name is AuctionViewerController):
[Route("AuctionDetailsGallery/{selectedAuctions}/Test-Versteigerung-2")]
public ActionResult FilterGalleryPositions(int selectedAuctions)
{
...
}
With a routetable you might define something like this:
routes.MapRoute(
name: "AuctionDetails",
url: "AuctionDetailsGallery/{selectedAuctions}/Test-Versteigerung-2",
defaults: new { controller = "AuctionViewer", action = "FilterGalleryPositions" }
);

How to pass parameters in ASP.NET MVC for attributed routes

Here is my model:
Company-->Projects
I created my company which has id = 1. Now, I want to add a project to it. Using MVC attribute routing I am able to go this URL fine: http://example.com/companies/1/projects/create
When I fill in the fields for the project and submit it using HTTPPOST I want to send the user to http://example.com/companies/1/projects/edit/9 <-- 9 being the project which just got created from the create method.
If I do this:
return RedirectToAction("{companyid}/projects/edit", "companies", new {companyid = id, id= project.ID });
it goes to here and blows up: http://example.com/companies/%7Bcompanyid%7D/projects/edit/9?companyid=1
I want it to go to http://example.com/companies/1/projects/edit/9
Can anyone help me figure out the RedirectToAction() for this please?
The first argument to RedirectToAction is an action name (so the name of the method that will get called on your CompaniesController), not the route.
You can either substitute your string "{companyid}/projects/edit" for the action name or use the RedirectToRoute method and pass in the name of the route as set up in your routing tables.

Pass relative URL ASP.NET MVC3

I'm trying to pass a list of URL's with Id attributes from a controller to a view.
I can pass a <a href=...> link back but I don't think writing a 'localhost' absolute path is a clean way of approaching this. I cant pass an ActionLink back as it returns the full string. Is ther a simple solution to this problem? Thanks in advance.
Using this overload of the UrlHelper.Action() method and Request object you can get a complete URL including the route parameters such as IDs and the actual hostname of the application.
string url = Url.Action("action", "controller",
new System.Web.Routing.RouteValueDictionary(new { id = id }),
"http", Request.Url.Host);
UrlHelper is available in the controller via its Url property.
You can then pass such URL into your view.
It is also possible to use UrlHelper directly inside your view to create URLs for controller actions. Depends if you really need to create them inside the controller.
Edit in response to comments:
Wherever you need to place the URLs, this "URL builder" you are looking for is still the UrlHelper. You just need to pass it (or the generated URLs) where you need it, being it inside the controller, view or custom helper.
To get the links inside the unsorted list HTML structure you mention, you need to put anchors inside the list items like this:
<ul>
<li>Link</li>
...
</ul>
Then again you just need to get the URLs from somewhere and that would be from UrlHelper.
Simple and easy.
text
the route id = the parameter that is going to be inserted into your method.
eg.
function Details(int id) {
//id has the value of my_var_id
}

ASP.Net MVC redirecttoaction not passing action name in url

I have a simple create action to receive post form data, save to db and redirect to list view.
The problem is, after redirecttoaction result excutes, the url on my browser lost the action section. Which it should be "http://{hotsname}/Product/List" but comes out as "http://{hotsname}/Product/".
Below is my code:
[HttpPost]
public ActionResult Create(VEmployee model, FormCollection fc)
{
var facility = FacilityFactory.GetEmployeeFacility();
var avatar = Request.Files["Avatar"].InputStream;
var newModel = facility.Save(model, avatar);
return RedirectToAction("List");
}
The page can correctly render list view content, but since some links in this view page use relative url, the functions are interrupted. I am now using return Redirect("/Employee/List") to force the url. But I just wonder why the action name is missing. I use MVC3 and .Net framwork 4.
I am new to ASP.Net MVC, thanks for help.
Your route table definitely says that "List" action is default, so when you redirect to it as RedirectToAction("List") - routing ommits the action because it is default.
Now if you remove the default value from your routes - RedirectToAction will produce a correct (for your case) Url, but you'll have to double check elsewhere that you are not relying on List being a default action.
Well, Chris,
If you get the right content on http://{hotsname}/Product/ then it seems that routing make that URL point to List either indirectly (using pattern like {controller}/{action}) and something wrong happens when resolving URL from route or {action} parameter is just set wth default value List. Both URLs can point to the same action but the routing engine somehow takes the route without explicit action name.
You should check:
Order in which you define your routes
How many routes can possibly lead to EmployeeController.List()
Which one of those routes has the most priority
Default values for your routes
Just make the route with explicit values: employee/list to point to your List action and make sure that is the route to select when generating links (it should be most specific route if possible).
It would be nice if you provide your routes mappings here.
but since some links in this view
page use relative url, the functions
are interrupted.
Why do you make it that way? Why not generate all the links through routing engine?
When using the overload RedirectToAction("Action") you need to be specifying an action that is in the same controller. Since you are calling an action in a different controller, you need to specify the action with the alternate overload e.g. RedirectToAction("List", "Employee").

How can I implement Dynamic Routing with my choice of URL format?

My URL requirement is countryname/statename/cityname. For this I'm writing my own RouteHandler and adding one new route into the routecollection like this:
routes.Add(new Route("{*data}",
new RouteValueDictionary(new
{
controller = "Location",
action = "GetLocations"
}),
new MyRoutehandler()));
Now my question is: How do I generate this type of URL?
I tried Html.ActionLink() but it's asking for an action name and controller name. However, in my URL format I don't have any action name or controller name. How do I solve this?
Is there a reason you can't just use the following with the built-in route handler:
routes.MapRoute("LocationRoute",
"{countryname}/{statename}/{cityname}",
new { controller = "Location", action = "GetLocations" });
That may conflict with the default "{controller}/{action}/{id}" route, but that would happen with your current system anyway (unless you make a custom Route, inheriting from System.Web.Routing.RouteBase, rather than a custom Route Handler). The Route Handler is for handling what to do AFTER the route data has been extracted. Since you're still using Controller and Action, you should still be able to use the MvcRouteHandler. If you want to customize how the Route Data is extracted, you probably want a custom Route.
Btw, if you want to use a route which doesn't involve Controller and Action names, use Html.RouteLink. ActionLink is just a wrapper which puts the controller and action into the RouteValuesDictionary and calls the same internal helper as RouteLink.
Although in this case, the route still has a Controller and Action ("Location" and "GetLocations"), so you can still use ActionLink. I think ActionLink lets you specify a Route Name, so if you give your route a name you can specify that name in ActionLink and it will use that route to generate the URL (I may be wrong about that and if so you can still use RouteLink and manually add the controller and action route values)

Resources