What I'd like is for all urls to contain a certain parameter without having to pass it to the views and add it to ActionLink through the routevalues param. I have a section in my site that I want to keep track of a "return param" for all links. It works fine for forms since Html.BeginForm sets the action to the exact current url already.
So, if the page I'm at is
/MyController/MyAction/300?ReturnTo=100
and I output
Html.ActionLink("Next Screen", "MyOtherAction")
I'd like to see
Next Screen
without having to do
Html.ActionLink("Next Screen", "MyOtherAction", new {ReturnTo = Model.ReturnTo})
You could write your own HtmlHelper extension that does this by calling Html.ActionLink, and combining the query strings. Use the ViewContext to get at the current query string.
Try this:
Html.ActionLink("Next Screen", "MyOtherAction", new {Model.ReturnTo})
The link will now appear the way you want it to be.
Related
I have a hidden field in the layout page
#Html.Hidden("idmember", "123456");
I want to access this value in my controller's action
public ActionResult Index(string idmember){
//value for test parameter is null
}
When I do viewsouce on the page - the value is there.
What is the right way to access the value from layout page? Thanks,
UPDATED. based on the help from others, I released in order to pass value to the action I need to do a form post (and it works) but I have an actionlink
example:
#Html.ActionLink("View Member", "index", "member",new{idmember="12345"}, null)
This does work and my URL is /member/index?idmember=12345 But I wanted to make the link cleaner instead of passing the value in the querystring I wanted to somehow pass the value as hidden field? or another way to the action? Is it possible?
Regarding your edit: you shouldn't want to. You pass parameters either in the URL or as request body variables (e.g. POST body).
You can circumvent this for example by using the session to store the ID, but this is going to cause unexpected behavior and will break your web application. Session timeouts, users using multiple tabs and users wanting to share links are going to cause problems.
In short: use the URL for what it's meant to do; don't care about arbitrary "cleanliness".
I've spent a couple of hours trying to work out a way to do this. It's such a simple requirement that I can't believe I need to create view models or anything so grandiose.
Very simply, using MVC5, I have a View, let's call it Page1, on which there is a hyperlink. When the user clicks that hyperlink, I want them to be taken to a new View, Page2. On Page2 is an iframe. I want to set the src attribute of that iframe to the URL of Page1, the View that the user was redirected from.
So, if I could maybe use the ViewBag (which people on here don't seem to recommend), to pass the URL of Page1 from that View to the Controller for Page2, and then use Razor in my markup on Page2 to define the src attribute of the iframe, I imagine that'd work fine. Unfortunately, people don't seem to like using ViewBag, and also, I can't find an end-to-end description of how to actually achieve this in code.
Can somebody give me an appropriately straightforward solution to this problem?
A ViewModel for a View can simply be a string - so if you only need to pass a single value in to the view (the URL to use as the src), then in Page2.cshtml, you just define the model at the top like so:
#model string
To use that as the src attribute, you just do e.g.
<iframe src="#Model" .... >
And from the controller, you pass the url string as the model to the view like so:
return View("Page2", myUrlStringVariable);
So, to recap, a ViewModel doesn't necessarily have to be a new class you create - for the most basic scenarios like this, you can just use e.g. a string as the ViewModel. As soon as you start having multiple bits of data to pass in to a View, that's typically when you'd look to create a specific ViewModel class.
Another option may be a JavaScript solution. Something like...
document.getElementById('myIframe').src = document.referrer;
I have the following code:
Approve
This calls an Action method in my controller updates the approval status of the record and then:
return RedirectToAction("Index");
As expected, the page always refreshes to the top. Is there a way to maintain the scroll position to where I clicked the hyperlink. I have checked similar posts, but the ones I have seen referred to forms. Ultimately I will use JQuery/Ajax. However at present I want to see if there was a simple method that could be used like an HTML attribute.
Many thanks.
You can name the anchor tag and refer to the name in the href using the hash notation:
Approve
And then in your action method:
string redirectUrl =
string.Format(
"{0}#{1}",
Url.Action("Index"),
id);
return new RedirectResult(redirectUrl);
Just make sure your id is a value HTML id (nearly anything can be a valid id in HTML5, however rules apply for the naming of ids for earlier HTML versions). If in doubt, prefix it with something like 'aid-' (approval id), etc. before using in the HTML.
Otherwise, like you've already identified, your best bet is to use an AJAX call to make the approval. This way you stay exactly where you are on the page.
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
}
In my View, I have a table pulling various data in. Initially it only shows the rows actionable by that user. However, there is also an option to show all rows. To achieve that so far, I have two hyperlinks above the table, with hrefs of "?isRequiredAction=true" and "?isRequiredAction=false". In the Controller, I have the following:
public ActionResult Index(bool? isRequiredAction){
string userId = User.Identity.Name;
bool ira = true;
if (isRequiredAction != null)
{
ira = Convert.ToBoolean(isRequiredAction);
}
...
return View(model);
So, right now the Controller is getting its parameter from the querystring created by clicking those links. I'm not satisfied with this approach since I don't want to dirty up the URL with this query. Is there a simpler way of achieving what I'm asking? We would like to avoid turning the links into form objects if possible. Thanks.
This MSDN article should help.
From view to controller, you can do a HttpPost or you can pass the data as parameters.
You can create two distinct custom routes, for example:
www.yourapp.com/home
www.yourapp.com/home/showall
After that, you can make both routes target the same action or create a distinct action for each of these routes. Your choice!
Technically, I don't believe there is very appropiriate solution that fits your need because you don't want to make the link form object, nor "dirtying up" the URL. I suggest you to stick with your first approach because there is nothing wrong with dirtying the URL. By "dirtying" your URL, you can make link bookmarkable. If you are really concerned with it, then you can have two choices:
(1) Use Ajax. This way your URL will be unaffected; however, you lose bookmarkability.
(2) Use URL rewriting to make your URL "pretty". This approach, under the hood, equals to "dirtying up" the URL, but in a "pretty" way.