I have a question related to the Html.Action in MVC 4
I want to pass some Querystring Variables with it to the Details view
The code I have now is
System.Text.StringBuilder MobileData = new System.Text.StringBuilder();
MobileData.AppendFormat("<a style=\"text-align:left;\" data-role=\"button\" onclick=\"window.location='" + #Url.Action("Taken_Detail", new { id = tk.ID }) + "';\" data- ajax=\"true\" data-icon=\"alert\"><span class=\"AgenItems\">{1:dd-MM-yyyy}</span>", tk.ID, tk.Datum);
The problem is he would redirect me to localhost/PROJECTNAME/Home/Taken_Detail/2
what I want is Home/Taken_Detail?id=2
what am I missing here I am just starting to learn MVC 4, Every tip is welcome.
This is because your routes contain the id parameter. Remove it from the routes and Url.Action will change the URL and add your parameter to the query string.
Example:
routes.MapRoute("Default", "{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional });
The id parameter will be put after the last slash if you specify it with Url.Action.
If you remove it:
routes.MapRoute("Default", "{controller}/{action}",
new { controller = "Home", action = "Index" });
The resulting URL will have a query string containing the id.
Related
I have an MVC Action Link:
#Html.ActionLink("Update Information", "Index", "Performance",
new { performanceid = item.PerformanceId }, null)
This action link's href looks like this: /Performance/Index?performanceid=100
In my RouteConfig.cs I have the following routes in the following order:
routes.MapRoute(
"ShowPerformanceOptions",
"Performance/{performanceid}/Index",
new { controller = "Peformance", action = "Index" }
);
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
I do not want a querystring added to the end of the URL, I would instead like the URL to look like this: /Performance/360/Index
I have been through a variety of different options including adding the route parameters and optional url parameters and changing the way I write my ActionLink. Nothing seems to work.
Any ideas anyone?
To generate URL based on route name, use Html.RouteLink() method
#Html.RouteLink("Update Information", "ShowPerformanceOptions", new { performanceid = item.PerformanceId })
A good read What's the difference between RouteLink and ActionLink in ASP.NET MVC?
As #Satpal pointed out, the ActionLink wasn't working because of the typo in the route itself:
routes.MapRoute(
"ShowPerformanceOptions",
"Performance/{performanceid}/Index",
new { controller = "**Peformance**", action = "Index" }
);
routes.MapRoute(
"ShowPerformanceOptions",
"Performance/{performanceid}/Index",
new { controller = "**Performance**", action = "Index" }
);
I'm asking how to do a link with #Url.Action in a Razor view to make a link like
Controller/Action/123
I already made #Url.Action("Action","Controller", new { #ViewBag.ID }) but it makes me a link like
Controller/Action?ID=123
How do I make a URL without the querystring in the razor view?
Try:
#Url.Action("actionname", "controllername", new { id = ViewBag.Id})
I think the problem is just that you haven't specified that the value in your route parameters collection is the "id". Of course, I'm assuming that you're using the default route configuration in RegisterRoutes.
Tip: you can also use Html.ActionLink() which saves you the trouble of creating an <a> tag yourself:
#Html.ActionLink("linkText", "actionName", "controllerName", new { id = ViewBag.ID }, null);
This will generate an <a> tag with the linkText and the same url as Url.Action() which you can see in Jeff's answer.
Note: don't forget to add null as the last parameter, otherwise it will use the wrong overload and use the anonymous type as htmlAttributes.
Use Url.RouteUrl(String, Object) and notUrl.Action()
Use the default route name.. which must be Default
so your code should be :
#Url.RouteUrl("Default", new {controller = "SomeControler", action = "SomeAction" , id = #ViewBag.ID })
Doing that will give you url as follows : SomeController/SomeAction/5 (assuming ID was 5)
This happens because of the by default the project mvc template contains a Default route as follows :
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
You can create more fancy urls if you wish or if you need more parameters, by adding more routes into routing table
here's the description : http://msdn.microsoft.com/en-us/library/dd505215(v=vs.108).aspx
#Url.Action("Action/" + #ViewBag.ID,"Controller")
I am working in Asp.net mvc3 application.I have created url for product detail page like this
routes.MapRoute(
"ProductDetail",
"{category}/{title}-{id}",
new { controller = "ProductDetail", action = "Index" }
);
for other controller using this
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
Following code creating this url
www.example.com/Shoes/blackshoes-100
Now Problem is that i want to use this url for ProductDetail page if add any other controller and action name it will redirect to ProductDetail page like this
www.example.com/Home/Index-100
How can i restrict this url for ProductDetail Page?
is this right way to do this?
I wan to hide Controller and Action of Productdetail page.
Category,title and id values are changing for every product.
You have to define routes for any other page you have and map those routes before you map your peoduct detail route. Then the route maching mechanism will find them first and use them.
Of course you do not have to map route for every single action. You can create some prefixes for example for diffrent controllers like example below, to catch all routes for Home controller actions:
routes.MapRoute(
"Home",
"Home/{action}/{id}",
new { controller = "Home", action = "Index", id = "" }
);
The C# MVC 3 Routing i have a controller name Category.
There are 2 Sub Method of the Controller
1)Index
2)Detail
now my detail routing URl coming like this "Category/name" ok but i also want to Add "category/Name-for-all" hard code "-for-all"
want to add this hard code how ?
routes.MapRoute(
"categorie", // Route name
"Category/{id}/{no}", // URL with parameters
new { controller = "Category", action = "details", id = "id",no=UrlParameter.Optional } // Parameter defaults
);
routes.MapRoute(
"categories", // Route name
"Category/{id}/{action}", // URL with parameters
new { controller = "Category", action = "action", id = "id" } // Parameter defaults
);
html page
<div class="grid_3" >
<a class="companyanchor" href="/category/#Model.name/">#Model.Name</a>
</div>
Its hard to understand your question, as I understand you asking about hard coded part of url? Something like this:
routes.MapRoute(null,
"{category}/Page{page}", // Matches /Football/Page567
new { controller = "Product", action = "List" }, // Defaults
new { page = #"\d+" } // Constraints: page must be numerical
);
I have not worked with MVC recently, but isn't this the answer?
routes.MapRoute("categories", // Route name
"Category/{id}"-for-all/{action}", // URL with parameters
new { controller = "Category", action = "action", id = "id" } // Parameter defaults
);
... not sure where "Name" comes from in your example.
I am trying to do what SO does for its Question controller.
/Posts/{id}/{title} when viewing a post (action name not shown)
/Posts/New when you are posting something new.
/Posts/Delete/10 etc....
I have two routes set up (well, one if you don't count the default). What appears to be happening is all actions in the Post controller are being routed through the first one.
What is that? I obviously have it wrong, but I can't figure this out.
routes.MapRoute("ViewPosts",
"Posts/{postid}/{title}",
new { controller = "Posts", action = "View", postid = "", title = "" });
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
The first route handles all your requests which starts from /Posts.
You need to use constraints to allow {postid} be only number:
routes.MapRoute("ViewPosts",
"Posts/{postid}/{title}",
new { controller = "Posts", action = "View", postid = "", title = "" },
new { postid= #"\d+" });
In this case only if numeric Id is provided this route will handle it, otherwise "Default" route will handle.
All routes are going through the first because you have not specified that the postid field can only be numeric, or defined an earlier route that will catch /Posts/New. It is passing New as the postid with the View action.
You can add this route definition before the ones you have now:
routes.MapRoute("NewPost",
"Posts/New",
new{controller="Posts", action="New"});
Or whatever the appropriate controller/action would be.