How to get the current view name in asp.net MVC 3? - asp.net

How can I get the current view name regarding to current URL, in asp.net MVC 3 using Razor engine?

No idea why you would need to get the current view name but you could use the VirtualPath property inside a view. Normally it's more useful to know the current action or controller. But anyway, here's how to get the current view name:
#VirtualPath
and if you wanted to get only the filename:
#Path.GetFileName(Server.MapPath(VirtualPath))
and without the extension:
#Path.GetFileNameWithoutExtension(Server.MapPath(VirtualPath))

I've also tested this code, and I could do something with it.
But, I'm not sure if is this a good solution or not.
For example, I need to detect the Contacts view located in Home directory. So I wrote:
if (#Request.RawUrl == "/Home/Contacts")
{
// do something
}

You can get it from RequestContext.RouteData
specifically, its Values collection contains "controller" and "action" keys
i.e.
RequestContext.RouteData.Values["controller"]
RequestContext.RouteData.Values["action"]

ASP.NET Core's equivalent:
#ViewContext.ExecutingFilePath
Output is like this:
/Views/Shared/String.cshtml
The rendering of a view may involve one or more files (e.g. _ViewStart, Layouts etc).
This property contains the path of the file currently being rendered.
ViewContext.ExecutingFilePath Property

Related

ZF3 - adding a view fragment to ViewModel

I am curious to know if it is possible to add a fragment to a view model:
return new ViewModel([]);
Returns my view to https://example.com/view
However is it possible to set this dynamically to add a fragment?
return new ViewModel("#fragment")
To return to https://example.com/view#fragment
The use case, have a js login/register view that is either https://example.com/view#login or https://example.com/view#register and want to return to the correct view...
EDIT
Obviously, this can be done using a re-direct, however, in the case of returning form errors, ViewModel needs to be used...
Not sure if this is possible to apply param to router, but you can alwas return redirectToRegister = true, and then write and execute JavaScript in view which will change view if True.
A fragment is a same document reference i.e. if the URL specification is used correctly #register and #login will refer to different elements in the same document.
The view model therefore should be returning the same html in both cases, and the fragment will be handled by the browser. To distinguish register and login pages instead use different paths in the URL.

Equivalent to Razor Section Helper In Sitecore

I am trying to include different scripts on different pages in Sitecore and can't seem to find a very good way of doing this. In a normal mvc project I could add the #Section{} helper and use that on different partial views to specify where I want those scripts in the layout view, but I haven't been able to find an equivalent for the way that Razor helper is implemented with Sitecore. I'd like to do this without using a place holder, I don't want to add a view in Sitecore every time I need to add a script file.
Thanks in advance.
I'm afraid you're out of luck here.
#Section is not supported because Sitecore doesn't render the Razor views in the same way as MVC does.
A Sitecore MVC layout is basically just a regular view that is rendering several other partial views or controller actions.
So when the placeholders in the <body> of your layout view are being rendered, the <head> section of that layout has already been rendered.
There is no such thing as deferred rendering in Sitecore MVC like you can do with #Section.
Everything in the view is executed from top to bottom, so if you can put your scripts at the end of your layout (like before the </body>), you can still manipulate data in the views or actions that are executed earlier.
The way I have it setup in my current Sitecore MVC solution is my layout has an extension method call to RenderScripts() at the bottom before the closing body tag.
#Html.RenderScripts()
That extension method looks like this:
public static IHtmlString RenderScripts(this HtmlHelper htmlHelper)
{
var templates = (from object key in htmlHelper.ViewContext.HttpContext.Items.Keys
where key.ToString().StartsWith("_script_")
select htmlHelper.ViewContext.HttpContext.Items[key]).OfType<Func<object, HelperResult>>()
.Select(template => template(null)).ToList();
foreach (var template in templates)
{
htmlHelper.ViewContext.Writer.Write(template);
}
return MvcHtmlString.Empty;
}
Then on each MVC Razor View when I want to include a .js file that is specific to that rendering I call something like below at the bottom of the file:
#Html.Script(
#<script src="#Url.Content("~/js/custom/orderdetail.js?t=11172015")" type="text/javascript"></script>
)
Below is the Script extension method:
public static MvcHtmlString Script(this HtmlHelper htmlHelper, Func<object, HelperResult> template)
{
htmlHelper.ViewContext.HttpContext.Items["_script_" + Guid.NewGuid()] = template;
return MvcHtmlString.Empty;
}
This has worked out well for us and I think it is what you are trying to do.
Indeed, the Section-helper isn't supported in Sitecore. If you're using MVC4 you can maybe use Bundles to solve your problem. For more information see: http://www.asp.net/mvc/tutorials/mvc-4/bundling-and-minification
You can also create multiple bundles for specific views. In a single Bundle you can add multiple script and output it in your view by adding #Scripts.Render()

URL for new views in MVC4

I've seen several MVC4 tutorials that show how to access the index URL for a view, but I can't seem to reach a new view that I add.
I can access my home index at:
http://localhost:3214/
But if I create a new view (let's call it "NewView.cshtml") I can't access it from
http://localhost:3214/NewView.cshtml
Where would it be?
The page inspector expects the page to be at:
http://localhost:4244/Home/NewView
But it isn't there.
UPDATE:
In the solution explorer the file is located at:
MyProject/Views/Home/NewView.cshtml
OK - So think of your Views as merely html files (although they are not) - i.e. they are purely for display purposes. But they are not static like normal HTMLs - they have code.
Hence its the controller and the route that you need to understand.
The route is what you type in the browser. For instance /Home/NewView will be translated to HomeController, NewView action if thats how you have configured it. The default view is {controller}/{action}/{id} so try http://localhost:4244/Home/NewView/1
Now to properly display and code NewView you need to go to your HomeController and add a NewView action. Like:
public ActionResult NewView()
{
return View(); // This will automatically display the NewView.chtml view from the Home (or Shared) folder in your Views folder
}
Then go to your Routes (typically in your global.asax file and add it like:
routes.MapRoute(
"SomeUniqueRouteName",
"Home/NewView",
new { controller = "Home" action = "NewView" }
);
Then you can call it like http://localhost:4244/Home/NewView without the id cause you haev specified a route for it.
Let me know if you need any more help.

call mxml from actionscript

I am very new to Flex & Action script and here is my requirement:
There is one boolean flag in action script and if flag value is true then redirect to success.mxml page and if flag value false then redirect to error.mxml page.
Can anyone suggest me, how to redirect to mxml page/componenet from action script based on some condition.
MXML files are Actionscript class files. They are not pages as in JSP.
Although you can add both success.mxml and error.mxml as a child in Application tag and depending upon your flag value, you can set visibility of either to true or false.
Please Learn some Flex tutorial or book first because this question is too basic to answer.
You have to create the two variable with data type of both the mxml.
so as you your flag is true, just create a new instance of that variable and you can also call its all the public variable/functions for you as file. You also call their layout objects if you given them id's.
It is not bound in the Flex. You can call anything form anywhere if not then try Events.
Try to learn the flex tutorial for that. You can also try the Tour de flex.
Regards,
Manish

Flex Application for PlayBook Text Input Variable

I know this is probably a very simple question, but can someone please walk me through how to take what a user inputs into a s:TextInput and use that as a variable in a JSON data request?
Basically, I want to have a user enter a search term, like "math" and then have that placed into a variable so I can use it in a JSON request.
Something like public var q:String, except that my search box (and hence user input) is on another "view" of the application.
I've just started with Flex Mobile applications and I might be way out of my league. Does anyone know how to do this?
The containing view should still be able to access teh child view's properties (and therefore controls); you just need the control(s) to have a unique ID, which then becomes a property name. That said, I would consider the whole search feature to be self-contained, so it'd have its own controller (action script class for handling actions) wired up to the search button, adn have a reference to the view that it can extract the info from, etc.

Resources