asp.net mvc hyperlink a page? - asp.net-mvc

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

Related

Open a link in new tab from controller in ASP.NET MVC

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/");
}

Get Item in Sitecore MVC by its path

I have created a Web rendering and try to get a specific Item by its path.
Something like this :
Item item=Sitecore.Context.Database.GetItem("/sitecore/content/home");
Is it possible to get item using #Model.Sitecore() ?
Thanks
I don't recommend it, but you can just get it in your view with #{ }
#{
var item = Sitecore.Context.Database.GetItem("/sitecore/content/home");
}
You should really move to a Sitecore controller rendering and do this work in the controller and return the Item as your model.
public class YourController : Controller
{
public ActionResult Stuff()
{
var item = Sitecore.Context.Database.GetItem("/sitecore/content/home");
return View(item);
}
}
Your view
#model Sitecore.Data.Items.Item
<div>
#Model.DisplayName
</div>

MVC 3 navigate to correct ActionResult

In my MVC3 project, I have one controller "MyDetailsController" in the
Areas -> Test -> Controller Folder
And in From the ActionResult "Create" in MyDetailsController, I want to call the ActionResult "Edit" of "DetailsController" Which is located in the Controller folder of my application
This is the code I tried
public ActionResult Create()
{
//Some Code
return RedirectToAction("Edit", "Details", new { id = Party.PartyID });
}
But its not loading the exact ActionResult I need.
The the URL am getting is http://localhost:53970/Test/Details/Edit/977612
The URL I needed is http://localhost:53970/Details/Edit/977612
Any help will be appreciated, Thank you.
This will work :
return RedirectToAction("Edit", "DetailsController", new { id = Party.PartyID ,area = ""});

Asp.net mvc checkbox checked from the action

Question: I have to check the checkbox(which is not part of model) from the action ?
If I select the checkbox and submit to post action, then call the view again the checkbox will be checked fine. But how to check it on first place from non-post action please ? Thanks!
public ActionResult Images(string params)
{
//some code
return View(cp);
}
[HttpPost]
public ActionResult Images(string chbx)
{
//some code
}
Images view:
#using (Html.BeginForm())
{
#Html.CheckBox("chbx")
}
I'm using MVC3 by the way.
do you mean #Html.CheckBox("chbx", true) ?

ASP.NET MVC: routing help

Consider two methods on the controller CustomerController.cs:
//URL to be http://mysite/Customer/
public ActionResult Index()
{
return View("ListCustomers");
}
//URL to be http://mysite/Customer/8
public ActionResult View(int id)
{
return View("ViewCustomer");
}
How would you setup your routes to accommodate this requirement?
How would you use Html.ActionLink when creating a link to the View page?
In global.asax.cs, add following (suppose you use the default mvc visual studio template)
Route.MapRoute("Customer",
"Customer/{id}",
new { Controller = "CustomerController", action="View", id="" });
Make sure you put this route before the default route in the template
You then need to modify your controller. For the view,
public ActionResult View(int? id)
{
if (id == null)
{
return RedirectToAction("Index"); // So that it will list all the customer
}
//...The rest follows
}
For your second question, ActionLink is simple.
Html.ActionLink("Link Text", "View", "Customer", new {id=1}, null);

Resources