Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open URL in a new tab from MVC controller [closed]

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 ?

like image 559
Yoda Avatar asked Dec 05 '22 04:12

Yoda


2 Answers

This cannot be done from the controller , but rather from your razor View:

@Html.ActionLink("linkText", "Action", new {controller="ControllerName"}, new {target="_blank"})
like image 147
Hussein Zawawi Avatar answered Dec 25 '22 20:12

Hussein Zawawi


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">


Hope this helps...

like image 30
Murat Yıldız Avatar answered Dec 25 '22 21:12

Murat Yıldız