Generate Url to Razor Page in Area from Razor View - c#

Within Razor View, I want to generate Url to specific Razor Page which is located in some Area.
I have already trying using this
#Url.Page("/Areas/Identity/Pages/Account/Manage/Orders", new { id = #Model.Id })
but what I get in return is
http://localhost:8888/Order/Submit/12345?page=%2FAreas%2FIdentity%2FPages%2FAccount%2FManage%2FOrders
while I would need this:
http://localhost:8888/Identity/Account/Manage/Orders?id=12345
Is it correct to assume that the only way to solve it is to apply customer routing? If yes, what would be the way? Thanks.

This should do the trick:
#Url.Page("/Account/Manage/Orders", new { id = Model.Id, area = "Identity"})

Related

.net mvc can I use razor within razor

Nutshell:
How can I put an #Html.Action within and #Html.ActionLink ?
E.g using a default Visual Studio 2017 MVC project, there's a line in the _Layout.cshtml:
#Html.ActionLink("Application Name" , "Index", "Home", new { area = "" }, new { #class = "navbar-brand" })
But I want it to be:
#Html.ActionLink(#Html.Action("SiteName") , "Index", "Home", new { area = "" }, new { #class = "navbar-brand" })
where Action("SiteName") pulls in the .cshtml which is literally just a string with my site name in?!? Is this possible? Or can I maybe define a string via the Action somewhere at the start of my _Layout and put that in the ActionLink ?
History:
So I spent a while finding out how to have a global variable that I can call anywhere in my MVC View (My old question here which lead to this answer here
With the idea from the first answer, info from this site and with the noble aim of trying to stick to "the pattern" I've ended up creating
a ViewModel
public class SSVs
{
public string SiteName { get; }
}
and a PartialView
SiteName.cshtml
-This literally contains a string saying "My Site Name"
Now I'm introducing this dotted around my _Layout.cshtml using #Html.Action("SiteName")
It works! Great, and all I have to do to update every reference to my site name is change the SiteName.cshtml
Now I'm thinking sticking to "The pattern" was completely over the top and I need a much simpler way to hold some global variables...

Call a controller action on click of hyperlink when javascript disabled

I have a cshtml with below code,I want to record the link which was clicked by the user,The link might be a external url.This should work even when javascript is disabled.
foreach (var featuredPod in Model.FeaturedPods)
{
Find out more
}
If you want to record what the user clicked , then you will need to process this on the server and redirect the user to the real link
So something like (assuming each featuredPod in the model has some form of Id)
#Html.ActionLink("Find out more", "RecordAction", "RedirectController", new {Id=featuredPod.Id}, new {#class="", target="_blank"})
Then in the RecordAction action in the RedirectController
// get your Model.FeaturedPods where Id = passed in id
// record what the url is somewhere
Return Redirect(featuredPod.LinkUrl)
Can't you just use #Html.ActionLink helper?
#Html.ActionLink("Find out more", "Action", "Controller", new {Id=Model.Id}, new {#class="(...)"}

ASP.net MVC 5 loading HTML.Partial from controller instead of loading on cshtml page

new to MVC so here goes.
I am currently loading up HTML.Partial on my Index.cshtml page like so:
#Html.Partial("~/Views/Switchb/editPerson.cshtml")
However, I am in need of customizing that in the controller depending on the current users category number.
So as an example, if the user has a category of 3 then I would need to do this:
#Html.Partial("~/Views/Switchb/3.cshtml")
Is there any type of call in the "code behind" in the controller that I can use in order to do that? Or would I just need to place the code within the cshtml page and pass its category number via the controller over to the cshtml page?
You can render a partial view from a controller action. You can pass the view name as string.
public ActionResult Switchb(string categoryNumber) {
var viewModel = new MyViewModel { CategoryNubmer = categoryNumber };
// additional processing, backend calls, formatting ....
return PartialView(categoryNumber, viewModel);
}
To call this action from View:
#{
var routeValues = new RouteValueDictionary(new {
categoryNumber= "3",
});
Html.RenderAction("Switchb", "MyController", routeValues);
}
Determine the category in the controller (via url parameter, from a database, or whatever) and then set that value as a property on your view's Model. Then in your line of code you can do this
#Html.Partial("~/Views/Switchb/" + Model.Category + ".cshtml");

How to pass parameter to sitecore controller rendering method

I have this on my cshtml page
<div class="liveMatch-timer">
#Html.Sitecore().Controller("Blog", "LiveBlogHeader")
</div>
And this is my controller
public PartialViewResult LiveBlogHeader()
{
var matchHeader = GetMatchDayHeader();
return PartialView(BlogConstant.LiveBlogHeaderPath, matchHeader);
}
I have one hidden field with the name "liveMatchItemId" on my cshtml page. I would like to pass its value to controller so that I can access it inside controller. I am expecting to change controller definition something like this
public PartialViewResult LiveBlogHeader(string liveMatchItemId)
Anyone can help me understand on how can I do this? I am new to sitecore and MVC.
EDIT: I am able to achieve this using below code
#Html.Action("LiveBlogHeader", "Blog", new { liveMatchItemId = "12" })
But how can I set hidden field value instead of static field "12"?
Probably you could use something like:
#Html.Action("LiveBlogHeader", "Blog", new { liveMatchItemId = Model.LiveMatchItemId })
Where Model.LiveMatchItemId is the property that you want to pass to the controller.

Telerik MVC Menu "MenuItem.Action()" renders wrong url

I'm using Telerik MVC Menu to render my mainmenu.
Following code is the line where a certain menu item gets built up:
item.Add().Text("Address").ImageUrl("~/Content/Images/Icons/house.png").Action("index", "basicdata", new {basicdatatype=BasicDataType.ADDRESS});
I expect the url to become: localhost/basicdata/address
But it actually renders: localhost/basicdata?basicdatatype=address
I'd like to get that enum in my Controller as so:
public ActionResult Index(BasicDataType basicDataType)
{
//Code here
}
But it doesn't work because the URL isn't in the right format. Can someone help out?
EDIT:
Even the following renders the wrong url:
item.Add().Text("Test").Action<BasicDataController>(o => o.Index(BasicDataType.PROJECT));
As you are using areas, you have to specify that area when linking to it from outside:
#{ Html.Telerik().Menu()
.Name("Menu")
.Items(menu =>
{
menu.Add()
.Text("Address")
.Action("index",
"basicdata",
new { basicdatatype = BasicDataType.ADDRESS,
area = "basicdata" });
}).Render();
}

Categories

Resources