ASP.net MVC 4 and BootStrap _NavBar Style issue - css

After changing my theme rrom default ASP.net MVC theme to BootStrap I got some styling issue in NavBar, Log off should be in same line with Hello, admin:
This's my _NavBar.cshtml :
#helper ActiveItem(string actionName, string controllerName, string areaName)
{
if (ViewContext.RouteData.Values["action"].ToString() == actionName &&
ViewContext.RouteData.Values["controller"].ToString() == controllerName &&
(ViewContext.RouteData.DataTokens["area"] == null || ViewContext.RouteData.DataTokens["area"].ToString() == areaName))
{
#:active
}
}
<div class="navbar navbar-default navbar-fixed-top">
<div class="container">
<!-- Navbar header and form mobile version -->
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#">PFE</a>
</div>
<!-- Menu Link list and Deconnexion button -->
<div class="navbar-collapse collapse">
<p class="navbar-text navbar-right">
#Html.Partial("_LoginPartial")
</p>
<ul class="nav navbar-nav">
<li class="#ActiveItem("Index", "Home", null)">#Html.ActionLink("Home", "Index", "Home")</li>
<li class="#ActiveItem("About", "Home", null)">#Html.ActionLink("About", "About", "Home")</li>
<li class="#ActiveItem("Contact", "Home", null)">#Html.ActionLink("Contact", "Contact", "Home")</li>
</ul>
</div>
</div>
</div>
And this _LoginPartial.cshtml :
#if (Request.IsAuthenticated) {
<text>
Hello, #Html.ActionLink(User.Identity.Name, "Manage", "Account", routeValues: null, htmlAttributes: new { #class = "username", title = "Manage" })!
#using (Html.BeginForm("LogOff", "Account", FormMethod.Post, new { id = "logoutForm" })) {
#Html.AntiForgeryToken()
Log off
}
</text>
}
else {
<text>
#Html.ActionLink("Register", "Register", "Account", routeValues: null, htmlAttributes: new { id = "registerLink" })
| #Html.ActionLink("Log in", "Login", "Account", routeValues: null, htmlAttributes: new { id = "loginLink" })
</text>
}
Html code After interpted by Chrome:
Hello, <a class="username" href="/Account/Manage" title="Manage">admin</a>!
<form action="/Account/LogOff" id="logoutForm" method="post"><input name="__RequestVerificationToken" type="hidden" value="FouTIIlrUNMPSCuPnaUtLA-zm5Q5ya2-24OFPZF5EWKECyyN7l4F1SOIClnfUU_2CzEbq6tQSAJ_EDKbLJLA6Hzc4Gc_OSTWbHfXmSdwV8E1" />
Log off
</form>
How to fix it please?

Your form is probably being treated as a block. Just use display: inline-block property on it and it should be fixed.
#logoutForm {
display: inline-block;
}
http://jsfiddle.net/XdQA8/

To solve I added the _LoginPartial.cshtml inside a DIV tag like this :
<div class="navbar-collapse collapse">
<div class="navbar-text navbar-right">
#Html.Partial("_LoginPartial")
</div>

Related

How do I fix homepage issue in MVC 5 when i add bootsrap lumen theme?

Please help with this as I want to customize the navbar and replace the names from default such as application and index etc to my application, home page and etc.
Model Page Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace VidlyMovies.Models
{
public class Customers
{
public int Id { get; set; }
public string Name { get; set; }
}
}
LayoutpageCode
#{
ViewBag.Title = "Random";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2> #Model.Movies.Name</h2>
Controller Page Code
namespace VidlyMovies.Controllers
{
public class MoviesController : Controller
{
// GET: Movies/Random
public ActionResult Random()
{
var movie = new Movies() {Name = "Avengers"};
return View(movie);
}
}
}
OK, assuming this is Bootstrap 3 and your "css" bundle is loading it, their example here could be used. Taking a shot at integrating your code I would try:
<body>
<nav class="navbar navbar-inverse navbar-fixed-top">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<!-- Wrap your banner, app name etc. in a link back to home page -->
<a href="#Url.Action("Vidly", "Home", new { area = "" })" class="navbar-brand" title="Return to home page.">
<!-- I am using a font awesome icon plus text. You can use a bootstrap glyph or a banner, image, etc. -->
<span><i class="fa fa-home"></i> My App Name</span>
</a>
</div>
<!-- This is where you put your menu items. It will collapse if screen is small. The id is important to link above. -->
<div id="navbar" class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li class="active">#Html.ActionLink("Vidly", "Vidly", "Home")</li>
<li>#Html.ActionLink("About", "About", "Home")</li>
<li>#Html.ActionLink("Contact", "Contact", "Home")</li>
</ul>
</div>
</div>
</nav>
<div class="container body-content">
#RenderBody()
<hr />
<footer>
<p>© #DateTime.Now.Year - My ASP.NET Application</p>
</footer>
</div>
#Scripts.Render("~/bundles/jquery")
#Scripts.Render("~/bundles/bootstrap")
#RenderSection("scripts", required: false)
</body>

How can I center the nav menu items using bootstrap 3?

My Navbar:
Problem
How can I get the admin menu item to be in the middle rather than dropping to the left? I'm using a template I purchased from wrapbootstrap and cannot figure out why this is happening. I've tweaked a lot of CSS using the dev tools in firefox and no matter what I do, ADMIN hangs to the left.
CSS:
<nav class="navbar navbar-default" role="navigation">
<div class="container-fluid">
<!-- Toggle get grouped for better mobile display -->
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#navbar-collapse-1">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
</div>
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse" id="navbar-collapse-1">
<ul class="nav navbar-nav navbar-right">
<li class="active">#Html.ActionLink("Home", "Index", "Home", new { area = "" }, new { })</li>
<li class="dropdown">
<a data-toggle="dropdown" class="dropdown-toggle" href="#">Games</a>
<ul role="menu" class="dropdown-menu">
<li>#Html.ActionLink("Results", "Index", "Results", new { area = "" }, new { })</li>
<li>#Html.ActionLink("League Table", "Index", "LeagueTable", new { area = "" }, new { })</li>
</ul>
</li>
<li>#Html.ActionLink("Schedule", "Index", "Schedule", new { area = "" }, new { })</li>
<li>#Html.ActionLink("Players", "Index", "Players", new { area = "" }, new { })</li>
<li>#Html.ActionLink("News", "Index", "News", new { area = "" }, new { })</li>
<li>#Html.ActionLink("Gallery", "Index", "Gallery", new { area = "" }, new { })</li>
<li>#Html.ActionLink("Contact", "Contact", "Home", new { area = "" }, new { })</li>
#if (Request.IsAuthenticated)
{
<li class="dropdown">
<a data-toggle="dropdown" class="dropdown-toggle" href="#">Admin</a>
<ul role="menu" class="dropdown-menu">
<li>#Html.ActionLink("Admin Home", "Index", "Home", new { area = "Admin" }, new { })</li>
<li>#Html.ActionLink("Games", "Index", "Games", new { area = "Admin" }, new { })</li>
<li>#Html.ActionLink("Game Types", "Index", "GameTypes", new { area = "Admin" }, new { })</li>
<li>#Html.ActionLink("News", "Index", "News", new { area = "Admin" }, new { })</li>
<li>#Html.ActionLink("Players", "Index", "Players", new { area = "Admin" }, new { })</li>
<li>#Html.ActionLink("Seasons", "Index", "Seasons", new { area = "Admin" }, new { })</li>
</ul>
</li>
}
</ul>
</div>
</div>
</nav>
I had a similar problem before. I reduced the font-size.
.nav.navbar-nav.navbar-right li a {
/*reduce font-size here*/
}
I reduced the padding between the navbar elements.
.navbar-nav > li{
padding-left:30px;/*reduce*/
padding-right:30px;
}
You can also change the margins. It works too:
.navbar-nav > li{
margin-left:30px;
margin-right:30px;
}
Another way to go about it is using the media queries: check which screen-size has the admin dropping down and maybe turn one or two elements into a dropdown at that screen-size: for example have gallery and contacts into a dropdown called More. So when the user clicks it, he will have the option to choose between the two.

How can i get data on change event of dropdownlist in MVC4?

Hi i am using partial view in mvc. i put dropdown in partial view and render that partial view in index page.
I have to get data on change event of dropdown list.I prefer angularjs instead of java script.but i am not able to call controller Action from view using angularjs.
In Controller:
public class HomeController : AthController
{
public ActionResult Index()
{
var dashboardList = GetAllDashboards() as List<DashboardDefinition>;
return View(dashboardList);
}
public ActionResult GetDashboard()
{
return View();
}
...
...
}
In View:
#model IEnumerable<Portal.Domain.ReportingModule.ReportDefinition.DashboardDefinition>
#{
ViewBag.Title = "Reporting";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Reporting View</h2>
#Html.ActionLink("Add new Chart", "Create", "Chart", new { area = "Reporting" }, null)
#section SubMenu{
#Html.Partial("_ReportingMenu")
}
In PartialView:
#model IEnumerable<Portal.Domain.ReportingModule.ReportDefinition.DashboardDefinition>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js"></script>
<div data-ng-app="">
<div class="nav navbar navbar-default navbar-no-margin submenu" data-ng-controller="dashboardController">
<select class="btn btn-default" id="drpDashboard" data-ng-model="dashboard" data-ng-change="dashboardChange(dashboard)">
#foreach (var dashboard in #Model)
{
<option value="#dashboard.Id">#dashboard.DashboardName</option>
}
</select>
<a id="SubMenuIntegratorNew" class="btn btn-default submenu-item" href="#">
<i class="fa fa-file-o fa-lg"></i>
<span class="submenu-item-text">New</span>
</a>
<a id="SubMenuIntegratorSave" class="btn btn-default submenu-item" href="#">
<i class="fa fa-floppy-o fa-lg"></i>
<span class="submenu-item-text">Save</span>
</a>
<a id="SubMenuIntegratorSaveAs" class="btn btn-default submenu-item" href="#">
<i class="fa fa-files-o fa-lg"></i>
<span class="submenu-item-text">Save As</span>
</a>
<a id="SubMenuIntegratorAddChart" class="btn btn-default submenu-item" href="#">
<i class="fa fa-picture-o fa-lg"></i>
<span class="submenu-item-text">Add Chart</span>
</a>
</div>
</div>
<script type="text/javascript">
function dashboardController($scope) {
$scope.dashboardChange = function (item) {
$http({
url: "/Home/GetDashboard",
method: "GET"
});
}
}
</script>
In this code i checked using debauging that dashboardChage metho is calling but GetDashboard action can't be call.
You can put the Drop down inside a form and include the change attribute to this dropdown like this
#{ Html.BeginForm("Action", "Controller", FormMethod.Post, new { enctype = "multipart/form-data" }); }
#Html.DropDownList("Id", ViewData["Id"] as List<SelectListItem>, new { onchange = "this.form.submit();" })
#{ Html.EndForm(); }

ASP.NET MVC refresh table in tab-pill

I have a problem with my solution.
I have 2 related classes and display them like the example on the ASP.net -Webseite, but only with one related class.
This class is displayed in a tab-pill.
Now I added Modals and a Javascript to edit, delete and create new Entitys, like This., but i don't know how to implement the refresh function, that only my table from the related class refresh and not the page. I will not lose the tab-pill, with is activated.
Thanks for help
Here my View-Page:
#model Armageddon.Models.itUser
#{
ViewBag.Title = "Details ";
}
#using Armageddon.Helpers;
<h2>Details:</h2>
<div>
<hr />
<div class="col-md-5" style="text-align:right">#Html.LabelFor(model => model.displayName)</div>
<div class="col-md-5">#Html.DisplayFor(model => model.displayName)</div>
</div>
<!-- modal placeholder-->
<div id='myModal' class='modal fade in'>
<div class="modal-dialog">
<div class="modal-content">
<div id='myModalContent'></div>
</div>
</div>
</div>
<ul class="nav nav-tabs" role="tablist" id="myTab">
<li role="presentation" class="active">Logins</li>
</ul>
<div class="tab-content">
<div role="tabpanel" class="tab-pane" id="UIT2SysGroup">
#if (Model.UIT2SystemGroup != null)
{
<table class="table">
<tr>
<th>Gruppen</th>
<th>#Html.NoEncodeActionLink("<span class='glyphicon glyphicon-plus'></span>", "Add", "Create", "UIT2SystemGroup", routeValues: new { PersonId = Model.itUserID }, htmlAttributes: new { data_modal = "", #class = "btn btn-primary pull-right" })</th>
</tr>
#if (Model.UIT2SystemGroup.Count() != 0)
{
foreach (var item in Model.UIT2SystemGroup)
{
<tr>
<td>
#item.SystemGroup.Name
</td>
<td>
<div class="pull-right">
#Html.NoEncodeActionLink("<span class='glyphicon glyphicon-trash'></span>", "Delete", "Delete", "UIT2SystemGroup", routeValues: new { id = item.UIT2SystemGroupID}, htmlAttributes: new { data_modal = "", #class = "btn btn-danger" })
</div>
</td>
</tr>
}
}
else
{
<tr><td colspan="3"><div class="alert alert-info" role="alert"><div class="glyphicon glyphicon-info-sign"></div> Zurzeit sind keine administrativen Aufgaben vergeben.</div></td></tr>
}
</table>
}
</div>
</div>
You need to look into Ajax to call the controller that is returning the view.
There is a good sample here
http://www.c-sharpcorner.com/UploadFile/d551d3/how-to-load-partial-views-in-Asp-Net-mvc-using-jquery-ajax/
You could put your table inside a partial view and return this as part of your page.

ASP.Net Razor MVC Bootstrap Login Modal Validation

Using Bootstrap in my Razor page, I am opening a modal window:
<div class="container-fluid">
<div class="navbar navbar-fixed-top navbar-default">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="offcanvas" data-target=".navbar-offcanvas" data-canvas="body">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand pull-right" href="#">
<img src="assets/img/logo-header.png" alt="Alternate Text for Image">
</a>
</div>
<div class="navbar-offcanvas offcanvas navmenu-fixed-left">
<a class="navmenu-brand" href="#">eServices</a>
<ul class="nav nav-justified">
<li>New Here?</li>
<li>Services</li>
<li>Sign In</li>
</ul>
</div>
</div>
</div>
<div class="modal fade" id="modalLogin" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<p>
<h3 class="modal-title" id="myModalLabel">Login to MyApplication</h3>
</p>
</div>
#using (Html.BeginForm("index", "home", new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Post, new { role = "form", #class = "form-horizontal" }))
{
#Html.AntiForgeryToken()
<div class="form-group #Html.ValidationErrorFor(m => m.Username, "has-error has-feedback")">
<div class="col-sm-12">
#Html.FormTextBoxFor(p => p.Username, new { #class = "form-control" })
#if (!Html.IsValid(m => m.Username))
{
<span class="glyphicon glyphicon-remove form-control-feedback"></span>
}
#Html.ValidationMessageFor(m => m.Username, null, new { #class = "help-block" })
</div>
</div>
<div class="form-group #Html.ValidationErrorFor(m => m.Password, "has-error has-feedback")">
<div class="col-sm-12">
#Html.FormPasswordFor(p => p.Password, new { #class = "form-control" })
#if (!Html.IsValid(m => m.Password))
{
<span class="glyphicon glyphicon-remove form-control-feedback"></span>
}
#Html.ValidationMessageFor(m => m.Password, null, new { #class = "help-block" })
</div>
</div>
#Html.ValidationSummary(true, string.Empty, new { #class = "test" }, "span")
<div class=" pull-right">
<p>
<button type="submit" class="btn btn-default">#Forms.ButtonSignin</button>
</p>
<br />
<p>
#Html.ActionLink("Forgot your username?", "ForgotUsername")
</p>
<p>
#Html.ActionLink("Forgot your password?", "ForgotPassword")
</p>
</div>
}
</div>
</div>
</div>
The issue I have is that, for example, I entered an incorrect username/password combination, the form validates, but the modal window closes. Is there a way to re-open the modal window after the form has posted if the validation triggered an error?
You could add a property named IsModalShown i.e.
public class AModel
{
public bool IsModalShown { get; set; }
}
Render this as a hidden for in your view i.e.
#Html.HiddenFor(m => m.IsModalShown)
When the modal is opened set the hidden value to true, this will enable the modal state to be posted back to the controller action i.e.
$('#modalLogin').on('show.bs.modal', function (e) {
$('#IsModalShown').val(true);
})
Please note the above will depend on the version of bootstrap you are using but there are other docs on the official site
Then add the following to your view that automatically pops it up
$(function(){
#if(Model.IsModalShown)
{
$('#signin_modal').modal('show');
}
});
I had a similar problem, but didn't have a model in view. Posting my solution, in case it would help someone. I wanted to show an error message stored in TempData (in case of wrong login/password combination, on which Batuta was asking).
Controller:
public ActionResult SignIn(string login, string password)
{
if (Membership.ValidateUser(login, password))
{
// something
}
else
{
TempData["message-error"] = "Wrong combination";
}
return RedirectToAction("Index", "Home");
}
Showing the message in modal window:
#if (TempData["message-error"] != null)
{
#TempData["message-error"]
}
Opening modal in index:
#if (TempData["message-error"] != null)
{
<script type="text/javascript">
$('#myModal').modal('show');
</script>
}
Maybe not that clean, but works for me.

Resources