Passing parameters with an Action link on an image - c#

I have been looking everywhere for this answer. I can't find any solution. I am using MVC3 ASP.NET. I am trying to make it so that when a user clicks on their profile it directs them to their profile using their id. So far nothing has worked.
<a href=#Html.ActionLink("",
"Profile",
"Followers",
new { id = (new UserService().GetUserByUserName(User.Identity.Name)).Id})>
<img src="blah.jpg" />
</a>
This does not seem to work. Any suggestions?

Html.ActionLink returns a complete <a> tag, not a URL.
You want to call Url.Action.

Related

Using Response.Redirect with Friendly URLS in ASP.NET

I am designing a website using friendly urls in ASP.NET. I am encountering an interesting bug when I am using response's redirect function with variables. When the page is redirected with a variable, the page you land on seems to think that the page you are on is in fact the home directory, destroying all links in the process. For example, if I redirect to a page and pass a variable like so:
Response.Redirect("nextpage/variable",false);
If I have an image on the next page, the link changes from:
<img src="images/foo.png">
to
<img src="nextpage/images/foo.png">
This happens no matter what the original path is. I have tried to link the image to the home directoty, ie:
<img src="./images/foo.png">
<img src="~/images/foo.png">
Nothing has worked. It now thinks the home directory begins at the new page (I assume because it sees the name of the friendly url and thinks its a folder)
To any of those looking for an answer, the trick is not to use './' or "~/", but simple to use the forward slash '/' , like so:
<img src="/images/foo.png">
The forward slash goes back to the root for all links and images. :)

Redirect from an asp.net mvc application to a .aspx file?

I have a button in an asp.net MVC 5 web application and I'm trying to redirect the user to an .aspx file.
So that when the user clicks on a button from asp.net MVC (the code of that button is in HTML), he will be redirected to a web form (.apsx).
I tried this code but it doesn't work.
<button id="Login" runat="server" OnClientClick="window.open('Login.aspx', 'Login');">Login </button>
You have to call .aspx page in your Action method.
public ActionResult Login()
{
return Redirect("~/Login.aspx");
}
<a href="#Url.Content("~/Login.aspx")">
<button type="button">Login</button>
</a>
You can embed a button in a hyperlink, so that it appears like a button but works like a link. The trick is getting the URL set correctly. In mixed Web Forms/MVC projects I use the Url.Content helper because it respects the virtual root. You might do well to create a route class however.
public class WebFormsRoutes
{
public const string LoginPage = "~/Login.aspx";
}
With the appropriate namespaces imported, you can then do:
<a href="#Url.Content(WebFormsRoutes.LoginPage)">
<button type="button">Login</button>
</a>
You might also consider looking at the routing documentation. You can register your own Web Forms routes for friendlier URL's
Your code does not look like a Razor View Code.
You can do it multiple ways
1.<button onclick="window.open('Login.aspx')">Login</button>
2. Using Razor
<button onclick="location.href='#Url.Action("Login","Login")'">Login</button>
Thanks
Partha

making a page as child page of another page

i was finding code for making a page as sub page of some other page which will be known as parent page
like in mvc www.abc.com/home/photos.aspx like photo is a sub page of it . if we write it with home page it opens or something like this can any one help i want to do it in web forms
please give me any idea..........
<input type="button" value="Open a new window"
onclick="OpenWindow()" id="Button1" />
function OpenWindow()
{
window.open("NewWindow.aspx","MyWindow","height=450,width=300");
}
it is not what i am finding like i have two pages i want to open two pages like www.abc.com/firstpage/secondpage.aspx
example.com/home is a folder which contains a so called default page, in ASP.NET that would be Default.aspx. So you could see the same page calling example.com/home/Default.aspx.
example.com/home/photo is yet another folder within the home-folder, containing it's own Default.aspx. You could also call example.com/home/photo/Default.aspx.
What you want are folders and default pages, not "child pages".
Please read this article: ASP.NET Web Project Folder Structure
<form>
<input type="button" value="Open Window"
onClick="window.open('http://www.example.com/firstpage/secondpage.aspx')"/>
</form>
Fiddler
Or
Refer this .Hope it helps you.

Remove browser autocompletion in MVC

I am currently trying to remove the form autocompletion done by the user's browser which can cause some critical behavior since it fills the password field. I have already added the autocompletion attribute to all of my textbox fields but when I try with firefox it stills load my current login information into the fields.
Does anyone know how to resolve this issue?
EDIT: Since it's not clear, I have already added the aucompletion attribute with the value set to "off".
There is an autocomplete=off property in html.
It is used in the top right search box on this very page, inspect the html you'll see:
<input autocomplete=​"off" name=​"q" class=​"textbox" placeholder=​"search" ..... />
See this MDN article: https://developer.mozilla.org/en/How_to_Turn_Off_Form_Autocompletion
In MVC you would implement this at the form or for a textbox like so:
Html.BeginForm(
action, controller, FormMethod.Post, new {autocomplete="off"})
OR
Html.TextBoxFor(model => model.EmployerNumber, new {autocomplete="off"})
If you check HERE, setting autocomplete="off" on the form should do the trick.
The HTML5 have a add-on syntax for form/input elements, it is called autocomplete="off".
See http://www.w3schools.com/html5/att_form_autocomplete.asp
You can randomize id and name attributes of your textboxes - this will make browser autocomplete functions not working.
My implementation
In view:
<%
var guidString = Guid.NewGuid().ToString();
%>
<%=Html.TextBox(guidString, String.Empty)%>
<%=Html.Hidden("NameGuid", guidString) %>
in Controller:
string userName = Request[model.NameGuid];
...

how to insert image in html action link? asp.net mvc

I have navigation and many link on my webproject from html action links.
They are ugly with underline. I would like to insert some image with name or play with styles of action link.
Is it possible? How to do that?
Thanks and take care,
Ragims
You could use css to remove the underlines or place a backgroundimage,
otherwise you could also just create the link like so:
<img src="yourimg.jpg" />
Html.ActionLink and Url.Action return the same URL. The difference is that the first creates an HTML element while the second returns just the URL to that action.
Another option is to use Url.RouteUrl or Html.RouteLink to create a link based off your route (to an action) instead of directly to an action.
One solution is to create an HtmlHelper extension method for creating an image-specific action link. A detailed tutorial can be found here.
If You are on MVC 3-4 with razor view engine then this may help you-
#Html.ActionLink("your link Name", "Action Method", "Controller",
new { style = "background-image:url('.././Images/imageyouwanttoshow.png')" },null)
Instead of using #Html.ActionLink("linkname","action","controller")
you can use following
<a href='#Url.Action("action", "controller")'>
<img src='#Url.Content("~/images/imageName.png")' />
"images" is my folder for storing the images.
#Url.Content() is to know the path.
You can pass your action and the controller for that action to #Url.Action().
#Url.Action() works similar to the #Html.ActionLink().
Now your link will get replaced by the image.

Categories