building custom navigation in razor view - asp.net-mvc

I hope to get some useful input on how to implement my desired functionality:
I've got a razor partial view without a model that contains the navigation for my webapp. As some functions are only ment to be available for specific users, I want to customize the view.
In particular I want to show/not show specific li elements in the ul.
I already have a query which determines whether the element needs to be shown or not.
The element itself has to be like this:
<li>#Resources.Label1</li>
How would I achieve this?

You should be able to use the ViewBag.
Main view or controller:
ViewBag.LoggedIn = true;
Partial view:
<ul>
<li>Visible to everyone</li>
#if (ViewBag.LoggedIn == true) {
<li>Visibility based on some data</li>
}
</ul>

Related

Given razor file, how do I differentiate whether it is a view or a partial view?

When I add a new ActionResult in controller like this
public ActionResult Step8(AddPropertyStep7ViewModel model)
{
return View();
}
I need to specify what kind of view I want to return.{View with layout, View , Partial View} I know that I want to return a file like this one below
This is the Step1.cshtml. How do I determine whether it is a view or a partial view of sth?
#using W.Resources
#model W.Models.ViewModels.Agent.AddPropertyStep1ViewModel
#{
Layout = "~/Areas/Agent/Views/Shared/_Layout.cshtml";
}
<div class="side site--dashboard"> ... <div>
#section scripts
{
<script> ... </script>
}
The basic way to define partial view is that it has Layout = null.
BUT
even if you have value in your Layout property, and you return the view like following from controller.
return PartialView("Your_view_name");
it will still get rendered as a partial view (it will not get wrapped in master page content).
As MVC works by Convention over configuration, the convention to define partial view is that the view name should start with "_" (_MyPartialView.cshtml)
As you mentioned in your edited question you need to specify which kind of view you want to return. i don't see need where you just need to return layout and nothing else. the solution i proposed above either to use return View() or return PartialView() from your action method will work perfectly and you can decide if you want to render page with master page or as partial view.
Views are views are views. The terms "layout" and "partial" just make it easier to talk about how a view is being used, but there's no functional difference between them. In other words, a layout is a view used as a layout. A partial is merely a view used as a partial. There's no way to just look at a view and know what type of view it is because it's entirely contextual.
That said, layouts are kind of an exception, since in order to truly function as a layout, they need #RenderBody() to be called somewhere. However, you could still use a view that didn't call this as a "layout" for another view. It just wouldn't actually render the view's HTML: all that would be returned is the layout's HTML.
There is a convention of prefixing layouts/partials with an underscore. However, that is merely a way to quickly see within the project tree that there's something special about a view file. It's not required and doesn't really give you any information about what exactly is special about the view.
That said, your question here isn't entirely clear. You say you want to specify what type of view is returned, presumably from your action. Well, you're already doing that, in effect. When you return View(), you're saying you want the view to be treated like a standard view. If you wanted to return a partial, you would have to return PartialView() instead. There's no way to specify that the returned view must utilize a layout, though.

Can I conditionally render partial views in _Layout.cshtml?

Suppose I have a _Layout.cshtml where I render a left sidebar, which is common to every page of my website.
Something along these lines - a menu, for example
<div id="left-sidebar">
#Html.Action("_MenuView", "LeftSideMenu")
</div>
A feature I would like to have would be to add another partial view, but only display it in certain sections of the website.
For example, in the blog section I may want to display a list of post categories or a treeview of the posts.
<div id="left-sidebar">
#Html.Action("_MenuView", "LeftSideMenu")
#if ("???")
{
#Html.Action("_BlogTreeView", "BlogEntries")
}
</div>
How could I do that? I know that I want to display "_BlogTreeView" if the view I'm rendering is returned by BlogController ... where do I go from there?
In your layout, add this section
#RenderSection("blogEntries", false)
Then in every view where you want to show the partial view add this:
#section blogEntries {
#Html.Action("_BlogTreeView", "BlogEntries")
}

ASP.NET MVC loading multiple partial views into a single div using JQuery

I am working on a help page which is composed of a navigation tree, content box, and search box. The navigation tree has links to Frequently Asked Questions and Glossary, each of which are linked to an Action which return partial views.
To the right of the navigation tree I have a single content div that I would like to contain whichever partial view is selected in the navigation tree.
Success case (what I want to accomplish): clicking on one of the FAQ links calls the FAQ() method in my controller, then returns a partial view which is then displayed in my content div to the right of the navigation tree. Clicking on another item would cause another partial view to be loaded.
How do I go about this? I've read a ton of ASP.NET MVC JQuery loading blog posts and tutorials but can't find anyone who's done exactly this.
Many thanks!
You should be able to use the jQuery method .load() to load HTML into your div.
http://api.jquery.com/load/
You can create an action that returns the partial view as HTML.
ASP.NET MVC Returning Partial View as a full View Page
jQuery:
one easy way you can do is load all partial views in "Container Div" at page load in one go (if performance is not a issue)
then assign each partial div with different div id inside "container", than use jquery to control show(); hide(); for each div.
MVC:
however if i were you, "Glossary" and "FAQ" looks same model to me it shouldn't be put in different partial view in first place.
if they did designed in separate model, in this scenario, i would recommend you to create a proxy class as a ViewModel above models you want to display, and then load it with one partial view only

ViewData not inheriting in partials

I was trying to use a shared partial view to render when a particular listing page has no data. I wanted to use ViewData to pass information from the page into my listing control, which would then conditionally render the NoData partial view using the ViewData values.
I would like to be able to specify them in the view markup, not in the controller action, but when I add them in the view the don't seem to inherit down into child partial views (like the Nodata partial view). However, specifying them in the ViewData values in the controller actions works fine, the data is available all the way down...
Does anyone know why it behaves this way?
When rendering a partial you can also pass the ViewData.
<% Html.RenderPartial("NoData", ViewData); %>
<%Html.RenderPartial("partialViewName", "viewData", "model"); %>
it is a best practice to do the decision inside the controller, if you have a scenario to make a decision inside the view, separate them and call them inside the controller conditionally

Conditionally displaying parts of my view

Say I have an asp.net mvc website with a page that lists products. ON that page I have a "delete" button that should only show up for the user that inserted the product. What's the best way to do this?
One way I thought of doing it was setting a boolean in the controller to let the view know if the button should be displayed. Something like:
if(IsProductOwner(UserId))
ViewData["CanDelete"] = true;
Then in the view I can just do
<% if((boolean)ViewData["CanDelete"] == true) { %>
// show delete button
<% } %>
But is there a better way to do this?
My initial thought is that you should at least make that a function of the Product class so you can go:
<% if (product.IsOwnedBy(UserId)) { %>
// show delete button
<% } %>
This removes some of the floaty ViewData and builds the business logic into your classes rather than floating out on the edges.
However, I haven't found a decent way to do this sort of conditional display in views unless the view is significantly different then I get the action to display a different view depending on the context.

Resources