I have a scenario, where I have to redirect using hyper link in MVC and there I have to add action name, controller name & object name. When I try to enter the object name, It is not finding that object name. How to get the object name in Url.Action?
My Url in UI has to look like this: localhost:00/area/Controller/ActionMethod/ItemId.
Here I need help how to add the Itemid in Url.Action.
#Url.Action is used inside <a> tag.
<a href="#Url.Action("ActionMethod", "Controller", new { itemId = ItemId })" >LinkName</a>
Related
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.
I have an MVC project that lives on server in /root/folder1/, and a domain name pointing there.
So the url, www.site.com/Home is working fine.
However, all my #Html.ActionLinks and #Url.Action, etc. are rendering as www.site.com/folder1/Home
The links still work, but it is ugly, and I don't want the folder name to be known.
Anyone know why it's doing this and how to stop it?
You should be using something like this for your ActionLinks
#Html.ActionLink("Link Text", "ActionName", "ControllerName") 'NOTE YOU DO NOT NEED TO INCLUDE THE Controller part of the controller name. So for say AdminController you could simply set the controllerName in the ActionLink to `Admin`
You can also setup your ActionLinks like the below if you have to pass an Id or other value to the controller.
#Html.ActionLink("Link Text", "ActionName", "ControllerName", New With {.YourVariableName = "SomeValue"})
If you need to specify and HTML attribute for the control being rendered you can set those with one more overload.
#Html.ActionLink("Link Text", "ActionName", "ControllerName", New With {.YourVariableName = "SomeValue"}, New With {.htmlAttributeName = ""})
If you need to Specify the HTML attribute without a need to pass a value to the controller you can simply replace New With{.YourVariableName = "SomeValue"}withNothing`
Hope this works or at least points you in the right direction. A fresh MVC project out of the box has a Controllers folder. Which means server path looks something like root/controllerFolder/controller/action using the above works fine for me and only produces Url's that follow the standard {controller}/{action}/{id}. Say I have a need to call an Edit Action inside the AdminController which needs an ID so it knows what record I want to edit and I also want to add a class to this button so I can style it or anything else. I would do this
#Html.ActionLink("Edit", "Edit", "Admin", NEW WITH {.ID = #currItem.Id}, NEW WITH{.Class = "MyButtonClass"})
The URL that is generated will look like this
www.mysite.com/Admin/Edit/2
maybe this is a little more than you needed but your question appears to actually follow the standard {controller}/{action}/{id}. Just in-case I am missing something I figured I would try to at least point you in the right direction.
I want to pass a parameter to the controller and this is what i did :
<c:out value="${activity.nom_etabl}"/>
The controller :
#RequestMapping(value="/getinfoEtab")
public String getInfoEtablissement(#RequestParam("nomEtab") String nometab,ModelMap model){
model.addAttribute("etabliss", actservice.FicheEtabl(nometab));
return "FicheEtablissement";
}
But the controller with this way doesn't get the parameter.
Is ${pageContext.request.contextPath}/getinfoEtab resolving into the url you are trying to reach?
If your context path is the same as in the url for the page that contains your <a href> tag then you don't need ${pageContext.request.contextPath}. You can directly use <a href="getinfoEtab?nomEtab=. But if its taking you to the controller don't even bother changing it.
Do you see a value in your link where you have <c:out value="${activity.nom_etabl}"/>? If you can see the value, then change your code to <a href="${pageContext.request.contextPath}/getinfoEtab?nomEtab=${activity.nom_etabl}">
Given the following string:
/MyController/MyAction/4
How do I generate a link within another controller's action to link to the appropriate address?
If I do the following:
#Html.ActionLink("click", Url.Action(item.Link))
// where item.Link is where /MyController/MyAction/4 is stored
I get a link that resembles:
www.localhost.com/CurrentController/CurrentController/MyController/MyAction/4
I need to not have the "CurrentController" part (yes, there are two of them - I think that is because I am doing a #Html.ActionLink and an Url.Action).
How would I get my appropriate link?
If you already have the /MyController/MyAction/4 path you need stored in item.Link, could you just build the <a> tag yourself?
Link Text
Use the RouteUrl() method to achieve what you want. For more information, you can check this page as well.
I think what you are wanting is to link to another controller and action?
You need to do this;
#Html.ActionLink("Click", "ActionName", new {Controller = "ControllerName"})
Then you can add some HtmlAttributes onto that to;
#Html.ActionLink("Click", "ActionName", new {Controller = "ControllerName"}, new { #class= "className" })
Edit
If you are passing this string value in, then why not just use;
Click
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
}