MVC 5 #Html.ActionLink - asp.net-mvc

I'm having some issues with some data filtering ; I want, from the following dropdown menu, to be able to display a list of projects by approval or not.
When the user creates a new project one of the fields is "approved", which is a boolean. That checkbox is left not checked and when the project has a Go the user picks that checkbox as an approved project.
Basically, I want, when the user picks the option "Approved", to be redirected to the list of already approved projects.
How can I do that?
<li class="dropdown">
Project Execution <span class="caret"></span>
<ul class="dropdown-menu">
<li> #Html.ActionLink("Approved", "Index", "NEWPROJECTs")</li>
<li role="separator" class="divider"></li>
<li>#Html.ActionLink("On Going", "Index", "NEWPROJECTs")</li>
<li role="separator" class="divider"></li>
<li>#Html.ActionLink("List", "Index", "PROJECTEXECUTIONs")</li>
</ul>

Using Route Parameters
If you have a single controller action that handles one or more of these operations, then you'll likely need to supply a route value to determine what data you should be filtering by:
public ActionResult NewProjects(string filter)
{
// Check the filter that was used and filter the content that you
// will pass to the view accordingly
// Get your projects prior to filtering
var projects = _context.Projects;
switch (filter)
{
case "ONGOING":
projects = projects.Where(p => p.Status == "ONGOING");
break;
default:
projects = projects.Where(p => p.Status == "APPROVED");
break;
}
return View(projects);
}
Then when you build your Action Links, simply specify the filter as a route value so that your controller action can consume it and properly filter your data:
<li>
#Html.ActionLink("Approved", "Index", "NEWPROJECTs", new { filter = "APPROVED"})
</li>
<li role="separator" class="divider"></li>
<li>
#Html.ActionLink("On Going", "Index", "NEWPROJECTs", new { filter = "ONGOING"})
</li>

Related

ASP.Net MVc Adds an "nav-item selected" class to a the same route

<li class="#(ViewContext.RouteData.Values["Action"].ToString() == "Brands" ? "nav-item selected" : "")" id="main-nav-item-128" data-id="128">
Pwani Life Care </li>
<li class="#(ViewContext.RouteData.Values["Action"].ToString() == "Brands" ? "nav-item selected" : "")" id="main-nav-item-128" data-id="128">
Pwani Life Care </li>
I have those links under lists in my asp.net mvc application redirection to lets say /brands?id=12 and another one to /brands?id=17. However, i want to add the class nav-item selected to the links when they are active. any help?
since I understand that each link directs to the same action but different id, so you shouldn't be looking for action, instead you should compare to the id,
in your controller you can pass the id as ViewBag.TheID,
then in your html you can do this
<li class="#(ViewBag.TheID == 15 ? "nav-item selected" : "")" id="main-nav-item-128" data-id="128">
Pwani Life Care </li>
and so one for each link.

Passing variables as parameters to #Url.Action

How to pass variables as parameters to #Url.Action ?
I populate a Menu control using #Url.Action.
List:
- childitem (it's populated from stored procedure)
Fields:
- TX_MENU: "My menu item"
- TX_ACTION: "Index"
- TX_CONTROLLER: "Buzon"
I'd like to populate my Menu control assigning
href=#Url.Action("SomeAction", "SomeController")
How could I pass variables as parameters with #.
I tried this code, but failed:
<ul>
#foreach (var itemchild in childitem)
{
<li class='last'><a href='#Url.Action(#itemchild.TX_ACTION, #itemchild.TX_CONTROLLER)'>#itemchild.TX_MENU</a>
</li>
}
</ul>
#itemchild.TX_ACTION and #itemchild.TX_CONTROLLER are not recognized as variables inside #Url.Action.
You should white it like this:
<ul>
#foreach (var itemchild in childitem)
{
<li class='last'><a href='#Url.Action(itemchild.TX_ACTION, itemchild.TX_CONTROLLER)'>#itemchild.TX_MENU</a>
</li>
}
</ul>
Don't escape your variables with # symbol when you in Url.Action() helper

mvc bootstrap sidebar add active class to the whole parents path

I am building a sidebar navigation dynamically and I have the following issue:
Option 1 // add class active
Option 1.1 // add class active
Option 1.1.1 // add class active
Option 1.1.1.1 // add class active
option 1.1.1.1.1 // selected link id = 7 (this is guid in real app)
Option 1.1.2
Option 1.2
Option 2
Option 3
Basically the navigation is in the following format:
<ul>
<li>
link
</li>
<li>
link
<ul>
<li>
link
</li>
</ul>
</li>
</ul>
And here is what I try to do recursively.
#BuildNavigation(Model)
#helper BuildNavigation(IEnumerable<Menu> menu)
{
foreach (Menu item in menu)
{
<li>
#item.Title
#if (item.Items.Any())
{
<ul class="nav sub-menu">
#BuildNavigation(item.Items)
</ul>
}
</li>
}
}
This works fine, the sidebar is built. But how can I add the active classes to the <li> based on the current selection?
I can add a property to the Menu class bool IsActive and then within the view I can check if that menu is active but how can I add the active class to all parents <li> of that selected link?

What is the recommended way to show db query results on every page (badge notification)

I want to show the number of items linked to my user as a badge next to the menu on the top of my asp.net MVC page. (I am using bootstrap 3)
Because I don't want this to be static i.e. it must dynamically update I put it in a partial view page _Menu.cshtml that is referenced in my Layout page which is called with every page load
<ul class="nav navbar-nav">
<li>#*Html.ActionLink("Proposals", "Index", "MyController")*#
#{
var db = new MyDb();
var count = db.Proposals.Count(p => p.UserName== User.Identity.Name );
db.Dispose();
}
Home <span class="badge pull-right">#count</span>
</li>
</ul>
So it can appear on every page
I don't think this is very good way because
1) Separation of concerns. I have db logic directly in my cshtml
2) Calling it on every page is not so good idea but I am not sure of the best way to still refresh it but somehow cache it
What is the best practice for menu badge notification in asp.net MVC for the above scenario?
There are a lot of ways you may get it work. Your way is not recommended in MVC.
You may use ViewData, PartialView or jQuery.
Add action method to your controller:
public ActionResult GetCount()
{
var db = new MyDb();
var count = db.Proposals.Count(p => p.UserName== User.Identity.Name );
db.Dispose();
return PartialView(count )
}
In your GetCount() PartialView:
Home <span class="badge pull-right">#Model</span>
In your _Menu.cshtm if using PartialView:
<ul class="nav navbar-nav">
<li>#Html.ActionLink("Proposals", "Index", "MyController")
#{Html.RenderAction("GetCount", "Home")}
</li>
</ul>
In your _Menu.cshtm if using jQuery:
<ul class="nav navbar-nav">
<li>#Html.ActionLink("Proposals", "Index", "MyController")
<div class="Count"></div>
#{Html.RenderAction("GetCount", "Home")}
</li>
</ul>
<script>
$(document).ready(function(){
$(".Count").load('#Url.Action("GetCount", "Home")')
});
</script>

Active tag on MVC 4 layout page

I am using MVC 4 (Razor).
My HTML design contains a menu that (using some sort of javascript component),
I am looking for a smart way to mark the menu with what page am I currently on.
<li class="glyphicons home currentScroll active"><i></i><span>Dashboard</span></li>
<li class="glyphicons charts"> <i></i><span>Campaigns</span> </li>
<li class="glyphicons sort"> #Html.ActionLink("Home", "Index", "Home")</li>
<li class="glyphicons picture"> <li>#Html.ActionLink("Contact", "Contact", "Home")</li>
As you see the active li is marked, if I want to use this menu in the _layout.cshtml page, how can I change the Active mark according to the page I am currently loading?
Check this:
var controller = (string)ViewContext.RouteData.Values["controller"];
var action = (string)ViewContext.RouteData.Values["action"];
For each menu item check for equals menu controller and current controller (and action, may be)

Resources