How do i open an URL in a new tab / window from MVC controller based on success condition .Any way i can achieve it through the help of c# code without having to write javascript ?
Asked
Active
Viewed 3.7k times
4
-
What does your code currently look like? – TheLazyChap Mar 26 '15 at 03:18
-
possible duplicate of [ASP.Net MVC 3 Controller Action and Open New Window](http://stackoverflow.com/questions/7790355/asp-net-mvc-3-controller-action-and-open-new-window) – Tommy Mar 26 '15 at 03:44
-
@TheLazyChap This is what i am trying achieve from the controller If (user.selectedmethod == "Login") { return JavaScript(""); } – Yoda Mar 26 '15 at 16:42
-
@Yoda Why do not select the answer so that people try the best answer? – Jack Nov 26 '15 at 08:43
2 Answers
13
This cannot be done from the controller , but rather from your razor View:
@Html.ActionLink("linkText", "Action", new {controller="ControllerName"}, new {target="_blank"})

Hussein Zawawi
- 2,907
- 2
- 26
- 44
-
This is what i am trying achieve - User clicks on button and it hits a controller where it checks for some filters and if it succeeds it should open a URL example "http://google.com in a new window If (user.selectedmethod == "Login") { return JavaScript(""); } – Yoda Mar 26 '15 at 17:27
-
Why you dont use JQuery or javascript then what is the pb with that? You can do an ajax call and validate the data then navigate a new window based on that ? – Hussein Zawawi Mar 26 '15 at 23:08
3
Calling URL from Controller:
return RedirectToAction("Edit", "Home");
**Calling Action Method from View using HTML Button or Image:**
When creating a link to a controller action in ASP.NET MVC, using the generic ActionLink method is preferable, because it allows for strongly typed links that are refactoring friendly.
@Html.ActionLink("Edit", "Home", new { id = item.ID })
However, what if we want to have an image that links to an action? You might think that you could combine the ActionLink and Image and Button helpers like this:
**Using Button:**
<button onclick="location.href='@Url.Action("Edit", "Home",new { Model.ID })';return false;">Detail</button>
<input type="button" title="Delete" value="D" onclick="location.href='@Url.Action("Edit", "Home", new { id = item.ID })'" />
**Using Image:**
<a href="@Url.Action("Edit", "Home", new { id = item.ID })" title="Edit">

Murat Yıldız
- 11,299
- 6
- 63
- 63