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}">
Related
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>
I was wondering if is it OK to put the controller name as a string, like in the following example.
Also, should I put the action as a string instead?
Is there any better approach?
My concert is that if I change the controller's name for any reason, my best option is to do a "find-replace" in the whole project as it is now.
Many thanks!
In general, we directly use the name of the controller and action as strings, as shown below:
(However, this wording cannot be changed automatically after you change the controller name, you need to modify manually)
<a class="nav-link text-dark" asp-controller="Operation" asp-action="Index"> </a>
1. According to your current wording, you can update the code as follows:
(This will change dynamically as you change the name of the controller)
<a class="nav-link text-dark" asp-controller="#nameof(OperationController).Replace("Controller","")" asp-action="#nameof(OperationController.Index)"> link </ a>
2. As an alternative solution, you can add the Route attribute to the action specified by the a tag and set the route name.
In the a tag, you only need to use asp-route to link the corresponding action.
Please refer to this.
<a class="nav-link text-dark" asp-route="MyRouteName"> link </a>
Controller:
public class OperationController: Controller
{
[Route ("CustomControllerName/Index", Name="MyRouteName")]
public IActionResult Index ()
{
return View ();
}
}
Yes, that is exactly how you use the anchor tag helpers. At least to me that asp-action seems convoluted and I would simply have written it as:
<a asp-controller="Operation" asp-action="Index">...</a>
If you do change your controller name (which should not happen very often), then you would have to update the links.
For further reference see https://learn.microsoft.com/en-us/aspnet/core/mvc/views/tag-helpers/built-in/anchor-tag-helper?view=aspnetcore-3.1: "If the asp-controller attribute is specified and asp-action isn't, the default asp-action value is the controller action associated with the currently executing view. [...] If the asp-action attribute value is Index, then no action is appended to the URL, leading to the invocation of the default Index action. The action specified (or defaulted), must exist in the controller referenced in asp-controller."
With MVC5 (and older MVC frameworks), I was able to write my form without specifying the controller/action method:
#using (Html.BeginForm())
{
}
This way, I was able to reuse the form with different URL. If the form was called from the route "/books/2/edit", the HTML generated would be:
<form action="/books/2/edit"></form>
And if I call the form using the URL "/books/add", the HTML generated would be:
<form action="/books/add"></form>
How can I do the same with the tag helper syntax? I have tried all kind of syntaxes but it always generate an empty action attribute:
<form></form>
<form asp-route=""></form>
<form asp-controller="" asp-action=""></form>
Result:
<form></form>
<form action></form>
<form action></form>
When using HTML helpers, values that are not supplied explicitly default to the route values that are in the current request. That is the reason why you can specify BeginForm with no parameters.
When using tag helpers, this default logic no longer applies - the values must be provided explicitly. There are no defaults.
Option 1 - form tag
The simplest way to mimic what the HTML helper does with a form tag is:
<form action="#Url.RouteUrl(this.ViewContext.RouteData.Values)" method="post">
</form>
Option 2 - Html.BeginForm
Note that your current syntax is also still valid in ASP.NET Core MVC:
#using (Html.BeginForm())
{
}
But since you had to ask this question, I would say that it is absolutely not clear how the URL is being generated when using this syntax, which means you should probably change to using Url.RouteUrl to make it more readable despite being a bit more to write.
Option 3 - Tag Helper
Here is an example of how you could use a tag helper to achieve this, although it is a bit ugly.
There is a form tag helper attribute asp-all-route-values that allows you to pass all of the route values in a single parameter. However, according to asp-all-route-data must be IDictionary<string,object> or RouteValueDictionary, it is not possible to pass a RouteValueDictionary to this attribute, you would need to convert it to an IDictionary<string, string>. One way to do that is to build an extension method to make the conversion.
public static class RouteValueDictionaryExtensions
{
public static IDictionary<string, string> ToTagHelperValues(this RouteValueDictionary routeValueDictionary)
{
var result = new Dictionary<string, string>();
foreach (var kvp in routeValueDictionary)
{
result.Add(kvp.Key, kvp.Value as string);
}
return result;
}
}
You can then use a tag helper to generate the current URL as follows:
<form asp-all-route-data="#this.ViewContext.RouteData.Values.ToTagHelperValues()">
</form>
Option 4 - No action attribute
It is also possible to use a form tag with no action attribute. If you omit the action attribute, the default behavior in most (if not all) browsers is to use the current URL.
<form method="post">
</form>
WARNING: It is not standards compliant to use this option and technically the behavior of browsers do not have to default to the expected behavior of using the current URL if it is not supplied.
In the end, which method you use is a matter of preference.
I want to pass ID from query string to my action class, and want to use that id in my action class. I tried as:
<li>
Next
</li>
but I am getting exception. I have aaded list to sessionMap and want to retrive that value from sessionMap as queryString parameter.
Can anybody provide me solution for this problem
Thanks.
If you kept in session there is no need to send as request parameter again.You can directly access that session variable in Action class.
Generally speaking, in Struts2 request parameters are automatically available in the action if:
You have a getter and setter in your action for a variable with the same name (eg int getRecordID() setRecordID(int id) in your case)
You have the ParametersInterceptor in the action's interceptor stack. [1]
[1] See http://struts.apache.org/release/2.2.x/docs/interceptors.html
Try the code below
<li>
<a href="NextPageData.action?recordID="+<s:property value="%{sessionMap.get(recordID[0])}/> >Next</a>
</li>
Hope this helps
Well, I'm not a Struts expert, but I tried the following my Struts 2 Live project and its working fine.
String paramValue = ServletActionContext.getRequest().getParameter("paramName");
Simply put this code on action class.
String paramValue = ServletActionContext.getRequest().getParameter("recordID");
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
}