MVC Displaying name of logged in user in nav bar - asp.net-mvc

I'm using twitter bootstrap package in my application for my app layout.
I mostly use _BootstrapLayout.basic.cshtml as my default layout.
#using System.Web.Optimization
#using BootstrapSupport
#using NavigationRoutes
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>#ViewBag.Title</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="#Styles.Url("~/content/css")" rel="stylesheet"/>
#RenderSection("head", required: false)
#RenderSection("jtable", required:false)
#Html.Partial("_html5shiv")
#* favicons and touch icons go here *#
</head>
<body>
<div class="navbar navbar-inverse navbar-fixed-top">
<div class="navbar-inner">
<div class="container">
<a class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</a>
<a class="brand" href="#" title="change in _bootstrapLayout.basic.cshtml">Sorama</a>
<div class="nav-collapse collapse">
<ul class="nav">
#Html.Navigation()
</ul>
</div>
</div>
</div>
</div>
<div class="container">
#Html.Partial("_alerts")
#Html.Partial("_validationSummary")
#RenderBody()
<hr>
<footer>
<p>© Sorama #System.DateTime.Now.ToString("yyyy")</p>
</footer>
</div>
#Scripts.Render("~/js")
#RenderSection("Scripts", required: false)
</body>
</html>
this gives me a page with nav bar
How can i show the Welcome message to the logged in User on the right corner of the nav bar? Something like Welcome ABC! and some log off option on side?
The only thing I know is i can get name of current user from User.Identity.Name but I don't know how to make it appear on menu bar.
I couldn't find something that could help me so I thought may be I could get it here.
Edit: After #User.Identity.Name being added in view
I added above mentioned code after <ul> tag with #html.navigation
and this is what I get
I get the Welcome Demo on menu bar(next to profile, difficult to see) but it is nothing like I expected.
Could something be done in DefaultRouteConfig provided by Bootstrap?
public class LayoutsRouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.MapNavigationRoute<ModuleController>("Module", c => c.Index());
routes.MapNavigationRoute<AccountController>("Hardware", c => c.Login());
routes.MapNavigationRoute<LayoutsController>("Profile", c => c.Starter())
.AddChildRoute<LayoutsController>("Change Password", c => c.Marketing())
.AddChildRoute<AccountController>("Add User", c => c.Register())
.AddChildRoute<LayoutsController>("Logout", c => c.SignIn())
;
}
}

After the div element that contains the nav, just add another div element and put the username in there.
Something like:
<div>#User.Identity.Name</div>
ul tag is styled for Bootstrap navigation so creating a
<li> #User.Identity.Name</li>
should look ok and you can use action link to user management instead of #

Related

Drop down menu does not work if I enable bootstrap in my mvc application

Very simply, I put a drop down menu on my navigation bar that is in my _layout.cshtml page. It has a few items and drops down when I click on it as expected. So far so good! But when I reference Bootstrap at the start of the page the menu will no longer drop down. Bootstrap is stepping on something that is breaking my drop down menu. It appears that Bootstrap is not competely compatable with MVC. Does anyone have any idea of what the fix should be? I have tried this in both Chrome and Firefox. I am using Visual Studio 2017 with Bootstrap 4. Here is a sample of my _layout.cshtml page.
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>#ViewBag.Title - Bootstrap Reference</title>
#Styles.Render("~/Content/css")
#Scripts.Render("~/bundles/modernizr")
#Scripts.Render("~/bundles/jquery")
#Scripts.Render("~/bundles/jqueryui")
#Scripts.Render("~/bundles/bootstrap")
</head>
<body>
<div class="navbar navbar-default">
<div class="container">
<div class="navbar-header">
#Html.ActionLink("BootStrap Practice", "Index", "Home", new { area = "" }, new { #class = "navbar-brand" })
</div>
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li>
Messages <b class="caret"></b>
<ul class="dropdown-menu">
<li>Inbox</li>
<li>Drafts</li>
<li>Sent Items</li>
</ul>
</li>
</ul>
#Html.Partial("_LoginPartial")
</div>
</div>
</div>
Solved the problem by referencing Bootstrap near the end of the _Layout.cshtml page instead of the beginning of it. Not sure why that mattered, but I am new to Bootstrap and MVC, so in time I may have a better explanation.
You can try this:
Add class dropdown in the li element.
<li class="dropdown">
<a class="dropdown-toggle" data-toggle="dropdown" href="#">Messages <span class="caret"></span></a>
<ul class="dropdown-menu">
<li>Inbox</li>
<li>Drafts</li>
<li>Sent Items</li>
</ul>
</li>
I had a similar issue wherin a bootstrap menu would not appear within a partially rendered view and adding the render tag at the end of the view fixed it.
In case I had a page that was partially rendering a view. The partial view was working perfectly on its own but once it was injected into another page the dropdown menu stopped working and there was zero errors or warnings in the console. Adding the Scripts.Render at the end of the partial view fixed it for some reason.

Why is my bootstrap header covering other content? [duplicate]

This question already has answers here:
top nav bar blocking top content of the page
(20 answers)
Closed 6 years ago.
I have a problem using Bootstrap's grid system, what's happening is that my nav bar is seemingly covering up the other content, which is intended to be underneath it!
I'm using ASP.NET MVC in this Project, so all I've done is set up a Layout page like this;
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>You Hire it Ltd</title>
<script src="~/scripts/jquery-2.2.4.js"></script>
<script src="~/scripts/bootstrap.js"></script>
<link href="~/Content/bootstrap.min.css" rel="stylesheet" />
<link href="~/Content/style.css" rel="stylesheet" />
</head>
<body>
<div class="container-fluid">
<div class="row">
<header>
<nav class="navbar navbar-default navbar-fixed-top">
<div class="col-md-8">
<div class="navbar-header">
<a class="navbar-brand" href="#">
You Hire It
</a>
</div>
<ul class="nav nav-pills">
<li class="active">#Html.ActionLink("Home", "Index")</li>
<li>#Html.ActionLink("About", "About")</li>
<li>#Html.ActionLink("T&Cs", "Legal")</li>
<li>#Html.ActionLink("Contact us", "Contact")</li>
</ul>
</div>
<div class="col-md-offset-2"></div>
<div class="col-md-2 pull-right">
<div class="container-fluid">
<div class="ButtonWrapper">
<button class="btn btn-success">Sign up/Login</button>
</div>
</div>
</div>
</nav>
</header>
</div>
</div>
#RenderBody()
</body>
</html>
and the page in question is like this;
<div class="container-fluid">
<div class="row">
<div class="col-md-6">
<h1>Hello</h1>
</div>
</div>
</div>
I'm not entirely sure what's causing this behavior, any pointers would be greatly appreciated.
Thanks guys :)
You need to add padding to the top. From the Bootstrap docs:
The fixed navbar will overlay your other content, unless you add padding to the top of the body.
So in your style.css you need..
body {
padding-top:70px;
}

MVC 5 bootstrap - Compatible with web browsers

More recently, I started using the MVC pattern 5 I like it but unfortunately I do not have a good experience with Bootstrap.
I want my application to look neat. (without super-additive, just clean and tidy). At the same time, I would like to work the same way (if possible) on all devices such as the iPhone, tablets, desktop PC and Mac, and Android.
I found the answer in your forum (here is the article), but I'm not sure if it will work properly.
Question:
Could someone tell me with which web browsers these solutions will work properly and what will not work properly, please.
Simply. What is the compatibility of the solution.
Environment:
MVC 5 with jQuery 2.1.1 , modernizr 2.8.3, Bootstrap 3.2.0, Razor 3.2.2
There is my _Layout page (almost default layout, I changed only sections fooder).
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>#ViewBag.Title - My ASP.NET Application</title>
#Styles.Render("~/Content/css")
#Scripts.Render("~/bundles/modernizr")
</head>
<body>
<div class="navbar navbar-inverse navbar-fixed-top">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
#Html.ActionLink("Application name", "Index", "Home", new { area = "" }, new { #class = "navbar-brand" })
</div>
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li>#Html.ActionLink("Home", "Index", "Home")</li>
<li>#Html.ActionLink("About", "About", "Home")</li>
<li>#Html.ActionLink("Contact", "Contact", "Home")</li>
</ul>
#Html.Partial("_LoginPartial")
</div>
</div>
</div>
<div class="container body-content">
#RenderBody()
<hr />
<footer>
<div class="navbar navbar-default navbar-fixed-bottom">
<div class="container" style="font-size: .8em">
<p class="navbar-text">
© #DateTime.Now.Year - My ASP.NET Application
</p>
</div>
</div>
</footer>
</div>
#Scripts.Render("~/bundles/jquery")
#Scripts.Render("~/bundles/bootstrap")
#RenderSection("scripts", required: false)
</body>
</html>
There is my css file Site.css in Content folder. Is default as well, I changed only size padding-bootom and padding-top (according to the recommendations bootstrap Link to documentation).
body {
padding-bottom: 70px;
padding-top: 70px;
}
/* Set padding to keep content from hitting the edges */
.body-content {
padding-left: 15px;
padding-right: 15px;
}
/* Override the default bootstrap behavior where horizontal description lists
will truncate terms that are too long to fit in the left column
*/
.dl-horizontal dt {
white-space: normal;
}
/* Set width on the form input elements since they're 100% wide by default */
input,
select,
textarea {
max-width: 280px;
}

navbar in ruby on rails with yield

So after a couple of days of trawling various forums, tutorials etc - I'm still no closer to doing what is seemingly something straightforward - namely, have links on a Navbar populate a named yield with the contents of a partial. I'm probably missing something fundamental, but hey! I've asked this before, but I don't think I worded the question too well, so hopefully this time I'll do better!
My view:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="author" content="">
<link rel="shortcut icon" href="../../assets/ico/favicon.ico">
<title>Software Dev Tools Web UI</title>
</head>
<div class="container">
<!-- Static navbar -->
<div class="navbar navbar-default" role="navigation">
<div class="container-fluid">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
</button>
<a class="navbar-brand" href="#">MARS Web UI</a>
</div>
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav">
XXXXXXXXX
</ul>
<ul class="nav navbar-nav navbar-right">
<li class="active">Logout</li>
</ul>
</div><!--/.nav-collapse -->
</div><!--/.container-fluid -->
</div>
</div>
<body>
<div class="container">
<%= yield :mainbody %>
</div>
</body>
<script>
$(".nav li").on("click", function() {
$(".nav li").removeClass("active");
$(this).addClass("active");
});
</script>
</html>
Where I have inserted XXXXXXX above, I have tried:
<li>P4</li>
<li>P4 Output</li>
This simply rendered both "perforce" and "p4_output" partials one below the other, and clicking the navbar options had no effect.
So then I figured maybe something more like this was required:
<li class="active"><%= link_to "Perforce", {:action => 'renderp4'}, :remote => true %></li>
<li class="active"><%= link_to "Perforce", {:action => 'renderp4_output'}, :remote => true %></li>
But at this point I came to a grinding halt, not knowing how what would be needed in my "renderp4" and "renderp4_output" actions so as to return HTML to my mainbody yield - if it is even possible?!?
Hopefully what I am trying to achieve is obvious - while I appreciate and acknowledge I am probably rather wide of the mark with my solution currently, any guidance would be appreciated. As I say, I've hunted around, but just hit a brick wall!
Thanks :)
Create a renderp4.js.erb file
$('.container').replaceWith('<%= escape_javascript render "authentication/perforce" %>');

Links work in Chrome but not IE9 or Firefox

In Chrome everything works perfectly on a website page, the relevant part of whose source code I've copied below. However, in IE9 or Firefox (I have not checked Opera or Safari) the top and left-side navigation links do not work. The nonworking links, two of which have drop-down menus, rapidly flicker between two link texts when the mouse hovers over them. The links also toggle between link texts when you click on them in IE9 and Firefox. They do not link to other pages in those browsers. Oddly, the similarly formatted footer navigation links on the same page (also below) DO work correctly in all three browsers. After researching possible fixes I don't think this is a CSS, style tag, DOCTYPE or javascript issue, but I'm not positive. Can anyone suggest a fix?
<!DOCTYPE html PUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>The Animal Union - Home</title>
<link href="css/mainstyle.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="container">
<div id="header">
<div id="navi">
<strong>
<ul >
<li>Home</li>
<li>About
<ul></strong>
<li>History</li>
<li>Activities</li>
<li>Board</li>
</ul>
<li> <strong>
Donate </strong>
<ul>
<li>Noncash Gifts</li>
<li>Matching Gifts</li>
<li>Estate Planning</li>
</ul>
<li><strong>Volunteer</li>
<li>Shop</li>
<li>Contact </li>
<li>Links</strong></li>
</ul>
</div>
</div>
</div>
<div id="wrapper">
<div class="wrapper_leftpanel">
<div id="navi_drop">
<ul >
<li>HOME</li>
<li>ABOUT</li>
<li>DONATE</li>
<li>VOLUNTEER</li>
<li>SHOP</li>
<li>CONTACT</li>
<li>LINKS</li>
</ul>
</div>
</div>
</div>
<div id="footer">
<div class="footer">
<ul style="text-align:right;">
<li>Home</li>
<li>About</li>
<li>Donate</li>
<li>Volunteer</li>
<li>Shop</li>
<li>Contact</li>
<li>Links</li>
</ul>
</div>
</body>
</html>

Resources