I want to display a hyperlink in browser and when I click on the hyperlink the request goes to my controller and open the URL in new tab of the browser.
Any idea how can I achieve this?
#*This is index.cshtml*#
#{
ViewBag.Title = "Index";
}
<h2>Click on the link below</h2>
#Html.ActionLink("GOOGLE", "NewWindow", "Home")
public class HomeController : Controller
{
// GET: Home
public ActionResult Index()
{
return View();
}
public ActionResult NewWindow()
{
return Content("<script>window.open('https://www.google.com/','_blank')</script>");
}
}
}
This code is showing error. Please help
Use target="_blank" to open a link in a new window.
#Html.ActionLink("GOOGLE", "RedirectToGoogle", "Home", null, new { target = "_blank" })
In the controller, return a redirection to the required URL:
public ActionResult RedirectToGoogle()
{
return Redirect("https://www.google.com/");
}
Related
I have a redirection problem in ASP.NET, I have in the folder view the _Layout.cshtml or there is the home page and I created a second page BookingChamb.cshtml in the same folder. I have created a link in the submit button on the home page (<a href="">) to page BookingChamb.cshtml, but the page is not showing and I received this message :
"Server Error in '/' Application. The resource can not be found.
Description: HTTP 404. The resource you are looking for (or one of its
dependencies) could have been removed, had its name changed, or is
temporarily unavailable. Please review the following URL and make sure
that it is spelled correctly.
Requested URL: /BookingChamb.cshtml "
Thank you in advance
In asp.net-mvc you cannot link directly to a view, you need a backing controller action:
public class HomeController : Controller
{
public ActionResult Index()
{
ViewBag.Title = "Home Page";
return View();
}
public ActionResult BookingChamb()
{
return View();
}
}
And then generate your link with:
#Html.ActionLink("Booking","Home","BookingChamb")
here is the link code in the _Layout.cshtml page :
<button class="btn-submit" type="submit"> chercher</button>
here is the link code in the HomeController page :
public class HomeController : Controller
{
public ActionResult Index()
{
ViewBag.Title = "Home Page";
return View();
}
}
here is the folder structure
enter image description here
I'm trying to render from controller either PartialView or View depending on condition coming from outside. Web-site fails on posting data with StackOverflowException.
Controller code:
public ActionResult Login(bool partial = false)
{
if (partial)
{
ViewBag.Partial = true;
return PartialView();
}
return View();
}
[HttpPost]
public ActionResult Login(UserViewModel userViewModel)
{
if (!ModelState.IsValid)
return View(userViewModel);
// some authrorization staff
}
Login.cshtml:
#using SK.DDP.ImageGallery.Helpers
#model SK.DDP.ViewModels.UserViewModel
#{
ViewBag.Title = "Login";
if (ViewBag.Partial != null)
{
Layout = string.Empty;
}
}
#*Some form staff*#
AuthorizationInfo.cshtml:
#{
Layout = string.Empty;
}
#{ Html.RenderAction("Login", "Authorization"); }
Template:
#*Some dif for form requested by menu*#
#{ Html.RenderAction("AuthorizationInfo", "Authorization"); }
I have a web-site with login page and login popup window appearing when user clicks on menu, so i wanted to reuse the same action of controller and code and app is continuing to fail with stackoverflow exception.
Thanks.
Seems its a bug in a Razor engine.
I workarounded it.
AuthorizationInfo.cshtml
#{ Html.RenderAction("LoginPartial"); }
AuthorizationController.cs
public ActionResult Login()
{
return View();
}
public ActionResult LoginPartial()
{
ViewBag.Partial = true;
return PartialView("Login");
}
Now Post of forms does not generate overflows with templates applied recursively.
I have a partial view which contains inputs for logging in. The partial view is present on every page of the website so that a user can login to the website from any page.
I have a base controller which is then inherited by all other controllers as below.
When the logon info is submitted, it goes to the logon action in the base controller.
How do I return the view that the logon info was submitted from?
public class BaseController : Controller
{
[HttpPost]
public ActionResult logon(string tx_username, string tx_password)
{
//Verify login details
}
}
public class HomeController : BaseController
{
public ActionResult Index()
{
return View();
}
public ActionResult About()
{
return View();
}
public ActionResult Contact()
{
return View();
}
}
Add another parameter ReturnUrl in the #Html.BeginForm which can be populated using the ViewConext.Controller.ValueProvider, and post the current action and controller name on which user is in the login post action like:
View:
First Way:
#using (Html.BeginForm("logon",
"Home",
FormMethod.Post,
new { ReturnUrl = Url.Action(#ViewContext.Controller.ValueProvider.GetValue("action").RawValue.ToString(),
#ViewContext.Controller.ValueProvider.GetValue("controller").RawValue.ToString()) }))
{
}
2nd Way:
#using (Html.BeginForm("logon",
"Home",
FormMethod.Post,
new { ReturnUrl = this.Request.RawUrl }))
{
}
Action:
[HttpPost]
public ActionResult logon(string tx_username, string tx_password,string ReturnUrl)
{
//If login successful
return Redirect(ReturnUrl);
}
I have a mvc project and I need to link a page using a hyperlink.I tried like this
<div class="someclass">1</div>
but its showing page cannot displayed error.What can be the reason?
structure is Views> Home > mylinkpage.cshtml
Thanks
Action is a method in a controller.
You should have.
public class HomeController : Controller
{
public ActionResult mylinkpage(string id)
{
return View();
}
}
Alternate you can try
#Html.ActionLink("1", "mylinkpage","Home", new { }, new { #class = "something" })
I have a controller
public class JobSearchResultController : Controller {
public ActionResult Index() {
return View();
}
}
the View ~/Views/JobSearchResult/Index
#Html.Raw("<b> TEST VIEW </b>")
And then I register the route
public IEnumerable<RouteDescriptor> GetRoutes()
{
return new[] {
new RouteDescriptor {
Priority = 5,
Route = new Route(
"JobSearchResult",
new RouteValueDictionary {
{"area", "Module.Test"},
{"controller", "JobSearchResult"},
{"action", "Index"}
},
new RouteValueDictionary(),
new RouteValueDictionary {
{"area", "Module.Test"}
},
new MvcRouteHandler())
}
};
}
And I'm calling it in one of the other views
#Html.ActionLink("Click Me",
"Index",
new { controller = "JobSearchResult", area = "Module.Test"" })
Works good, it takes me to the Actual View/Index displaying the plain page with
TEST VIEW
in localhost/Orchard.Web/JobSearchResult URL
Now, my problem is I'm expecting this page to use the current theme / current layout (masterpage) just like a normal content type
How can I do this?
I know theres an [Admin] attribute where you can be able to display in Admin view using the Admin theme but how about if I want to display it in the Client. Is there something like
[Client] //display in Client
public class JobSearchResultController : Controller {
public ActionResult Index() {
return View();
}
}
Thanks in advance!
Yes. You can apply the Orchard.Themes.ThemedAttribute:
[Themed] //display in Client
public class JobSearchResultController : Controller {
public ActionResult Index() {
return View();
}
}