I am trying to create a menu that highlights the current page. I have found a few answers here, but the problem is I can't see that anyone handles submenus.
There is an answer here that looks enticingly simple: active menu item - asp.net mvc3 master page
But as far as I can tell, that one will highlight only the sub menu item if you click on a submenu. I want the submenu item to be highlighted, as well as its parent in the top menu.
E.g. if someone clicks Services, and then Consulting, I would want both of these to be highlighted - Services in the top menu, and Consulting in the submenu. How can I do this?
BTW, I would like to be able to render the submenu both as a dropdown using CSS, and also as a sidebar. How can I take the submenu ul and render it as a sidebar?
Here is a simple thing, you can modify it according to your needs but basics are here.
http://developerstyle.posterous.com/highlighting-current-page-in-mvc-3-slick-tric
This above link might be down soon as posterous is closing, here is an update link
http://bhavinsurela.com/highlighting-current-page-in-mvc-3-slick-tric/
I have a solution which I in part also found here in SO and modified, but still as to be improved in order to handle any number of submenus... right now it works for a submenu.
namespace PhotoBuss.Web.Back.Controllers
{
public class NavigationController : BaseAdministrationController
{
//
// GET: /Common/
[ChildActionOnly]
public ActionResult HeaderMenu()
{
// http://stackoverflow.com/questions/4653226/asp-net-mvc-menu-selected-item
var items = new List<MenuItemViewModel>
{
new MenuItemViewModel{ Text = "home", Action="Index", Controller="Administration", Selected=false},
new MenuItemViewModel{Text = "manage", Action="Index", Controller="Manage", Selected=false,
SubMenu =
new List<MenuItemViewModel>
{
new MenuItemViewModel{ Text= "photos", Action="Index", Controller="Photos", Selected = false },
new MenuItemViewModel { Text = "collections", Action="Index", Controller="Collections", Selected=false},
new MenuItemViewModel { Text = "keywords", Action="Index", Controller="Keywords", Selected=false},
new MenuItemViewModel { Text = "users", Action="Index", Controller="Users", Selected=false},
new MenuItemViewModel { Text = "user groups", Action="Index", Controller="Roles", Selected=false}
}
},
new MenuItemViewModel{Text="cms", Action="Index", Controller="CMS", Selected=false}
};
string action = ControllerContext.ParentActionViewContext.RouteData.Values["action"].ToString();
string controller = ControllerContext.ParentActionViewContext.RouteData.Values["controller"].ToString();
foreach (var item in items)
{
if (item.Controller == controller && item.Action == action)
{
item.Selected = true;
}
foreach(var subItem in item.SubMenu)
if (subItem.Controller == controller && subItem.Action == action)
{
item.Selected =
subItem.Selected = true;
}
}
return PartialView(items);
}
}
The ViewModel
public class MenuItemViewModel
{
public MenuItemViewModel()
{
SubMenu = new List<MenuItemViewModel>();
}
public string Text { get; set; }
public string Controller { get; set; }
public string Action { get; set; }
public bool Selected { get; set; }
public List<MenuItemViewModel> SubMenu { get; set; }
}
}
The View
#model List<PhotoBuss.Web.Back.Models.Navigation.MenuItemViewModel>
<link href="#Url.Content("~/Areas/Admin/Content/CSS/menu.css")" rel="stylesheet" type="text/css" />
<div class="headerMenu">
<ul>
#foreach (var menuItem in Model)
{
<li>#Html.ActionLink(menuItem.Text, menuItem.Action, menuItem.Controller, null,
new { #class = menuItem.Selected ? "selected" : "" })
#if (menuItem.SubMenu.Count >0)
{
<ul class="#(menuItem.Selected ? "selected" : "")">
#foreach (var subMenu in menuItem.SubMenu)
{
<li>#Html.ActionLink(subMenu.Text, subMenu.Action, subMenu.Controller, null,
new { #class = subMenu.Selected ? "selected" : "" })</li>
}
</ul>
}
</li>
}
</ul>
</div>
The CSS I'm using with this at the moment:
.headerMenu *
{
padding: 0;
margin: 0;
}
.headerMenu
{
position: relative;
background-color: #78C8FA;
width: 100%;
text-align: center;
color: #FFFFFF;
clear: both;
float: left;
margin-top: 10px;
}
.headerMenu ul
{
display: block;
list-style: none;
line-height: 3em;
height: 3em;
}
.headerMenu ul li
{
display: inline-block;
margin-left: 15px;
margin-right: 15px;
}
.headerMenu ul li a
{
display: block;
text-decoration: none;
color: white;
font-size: 1.5em;
padding-left:2em;
padding-right:2em;
}
.headerMenu ul li a:visited
{
color: white;
}
.headerMenu ul li a:hover, .menu ul li
{
color: #333333;
}
.selected
{
color: #333333 !important;
display:block !important;
background-color: #999999;
}
.headerMenu ul ul
{
display: none;
position: absolute;
width: 100%;
right: 50%;
left: 0;
background-color: #999999;
}
.headerMenu li:hover > ul, .selected
{
display: block;
}
It's fairly simple to determine which menu element to highlight by simply using the ViewContext.RouteData.Values dictionary, specifically the Action and Controller keys.
Here's a quick helper method :
public static string IsSelected(this RouteValueDictionary dictionary, string controller, string action)
{
string cssClass = "selected";
string routeValueController = dictionary["Controller"] as string;
string routeValueAction = dictionary["Action"] as string;
return string.IsNullOrEmpty(action) ?
routeValueController == controller ? cssClass : string.Empty :
routeValueController == controller && routeValueAction == action ? cssClass : string.Empty;
}
And can be used from the view as such :
<ul id="menu">
<li class="#this.ViewContext.RouteData.Values.IsSelected("Default", "Index")">
Accueil
</li>
</ul>
It's hard to get into a more specific solution as I'm not familiar with your application structure, but this should give you an idea to get started.
Here is a example where they handle the submenus and highlight it.
http://users.tpg.com.au/j_birch/plugins/superfish/#sample4
It uses superfish-navbar.css where you can see how it is done.
It is a very good plugin for menus.
Related
I am making a simple component to test newest Lit-element a checkbox.
Upon testing static get styles only the first element I style is shown, I have seen in the documentation what I am trying should be correct, may I have some help?.
this is my component:
import {LitElement, html, css} from 'lit-element';
class CheckboxMJ extends LitElement {
static get properties(){
return{
check:{type:Boolean},
name:{type:String},
}
}
static get styles() {
return css`
.checkWrapper{
font-family: Roboto;
background-color: red;
font-weight: 500;
font-size:14px;
color:#283D3B;
border:none;
outline:none;
height: 150px;
width: 300px;
border-radius: 3px;
overflow:hidden;
padding:3px;
}
input[checkbox i]{
background-color:red;
}
`;
}
constructor(){
super();
this.check=false;
this.name="";
}
render() {
return html`
<div class="checkWrapper">
<input class="checkbox-mj" type="checkbox" name="${this.name}" value="${this.check}"> ${this.name}
</div>
`
}
}
customElements.define('checkbox-mj', CheckboxMJ);
I have been encountering this issue several times with other components, kept changing order, and names of classes until it worked but I feel so lost about how this should be done right, please somebody enlighten me on how to use this feature correctly.
You have to keep in mind that checkboxes are very difficult to stylize. Many properties simply have no effect on this input. On the other hand you have to use a standard css selector to stylize the checkbox input[type="checkbox"].
If you want the check property to be reflected in your checkbox you must do it this way:
?checked="${this.check}"
Look at this guides for more information https://lit-element.polymer-project.org/guide/templates (Bind properties to templated elements).
import {
LitElement,
html,
css
} from 'lit-element';
class CheckboxMJ extends LitElement {
static get properties() {
return {
check: {
type: Boolean
},
name: {
type: String
},
}
}
static get styles() {
return css `
.checkWrapper{
font-family: Roboto;
background-color: red;
font-weight: 500;
font-size:14px;
color:#283D3B;
border:none;
outline:none;
height: 150px;
width: 300px;
border-radius: 3px;
overflow:hidden;
padding:3px;
}
input[type="checkbox"]{
margin:1rem
}
`;
}
constructor() {
super();
this.check = true;
this.name = "Check";
}
render() {
return html `
<div class="checkWrapper">
<input class="checkbox-mj" type="checkbox" name="${this.name}" ?checked="${this.check}"> ${this.name}
</div>
`
}
}
customElements.define('checkbox-mj', CheckboxMJ);
I am trying to set the current "active" link to white to show which page the user is on.
I see see on a previous post
"A link only takes up the a:active state when it is clicked, so you only see the change for a few seconds. You should look for a different way for getting it done, like adding a new css class for the selected menu item from your server side script. "
Q. How do I do this?
My CSS is:
ul.topnav {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: transparent;
}
ul.topnav li {
float: left;
}
ul.topnav li a:link {
color: black;
text-align: center;
padding: 10px 12px;
text-decoration: none;
font-size: larger;
font-family: Arial;
font-weight: bold;
}
.topnav ul li.current a {
color: Black;
}
/*
ul.topnav li a:visited {
color: black;
}
*/
ul.topnav li a:hover:not(.active) {
color: Aqua;
text-decoration: none;
transition: none;
}
l.topnav li a:hover:active {
color: white;
text-decoration: underline;
}
ul.topnav li a:active {
color: white;
transition: none;
text-decoration: underline;
}
<ul class="topnav">
<!--<li><a class="active" href="../index.html">HOME</a></li>-->
<li> HOME</li>
<li> ABOUT</li>
<li> NEW</li>
<li> PRODUCTS</li>
<li> CATALOG</li>
<li> SEARCH</li>
<li> DISTRIBUTORS</li>
<li> SERVICE</li>
<li> CONTACT</li>
</ul>
I hope this information helps. Been trying to fix this for awhile now.
Regards,
Larry
You will need to update the html of your navigation to include an identifier for the "current"/"active" page. This would mean updating your .html pages to add something like class="active" to the link that represents the current page. You would then update your css to something like:
ul.topnav li a:hover:not(.active) {
color: Aqua;
text-decoration: none;
transition: none;
}
ul.topnav li a.active:hover,
ul.topnav li a.active {
color: white;
transition: none;
text-decoration: underline;
}
I was able to fix the problem of setting active links "white" and inactive ones "black using javascript. You can see the result here: http://www.nav-aids.com/new-navaids/index.html
My menu is setup as:
<div class="col-7 col-m-7">
<ul class="topnav">
<li> <a id="index" href="../index.html">HOME</a></li>
<li> <a id="about" href="../Aboutus.html">ABOUT</a></li>
<li> <a id="new1" href="../new.html">NEW</a></li>
<li> <a id="products" href="../Samples.html">PRODUCTS</a></li>
<li> <a id="catalog" href="../catalog.html">CATALOG</a></li>
<li> <a id="search1" href="../search.html">SEARCH</a></li>
<li> <a id="distributors" href="../distributors.html">DISTRIBUTORS</a></li>
<li> <a id="service" href="../service.html">SERVICE</a></li>
<li> <a id="contact" href="../Mailto.html">CONTACT</a></li>
</ul>
</div>
<div class="col-2 col-m-2">
<div></div>
</div>
</td>
</tr>
</table>
</div>
<!-- set active menu item to white, inactive to black -->
<script language="JavaScript" type= "text/javascript">
menuactive()
</script>
My javascript is:
/* set menu button to white when selected and black if not selected */
function menuactive() {
/* The javascript below returns the menu page name in sPath */
var sPath = window.location.pathname;
//var sPage = sPath.substring(sPath.lastIndexOf('\\') + 1);
var sPage = sPath.substring(sPath.lastIndexOf('/') + 1);
/* alert below used to verify which menu page selected */
/*alert(sPage);*/
if (sPage == "index.html") {
var x = document.getElementById("index");
x.style.color = "white";
} else {
var x = document.getElementById("index");
x.style.color = "black";
}
if (sPage == "Aboutus.html") {
var x = document.getElementById("about");
x.style.color = "white";
} else {
var x = document.getElementById("about");
x.style.color = "black";
}
if (sPage == "new.html") {
var x = document.getElementById("new1");
x.style.color = "white";
} else {
var x = document.getElementById("new1");
x.style.color = "black";
}
if (sPage == "Samples.html") {
var x = document.getElementById("products");
x.style.color = "white";
} else {
var x = document.getElementById("products");
x.style.color = "black";
}
if (sPage == "catalog.html") {
var x = document.getElementById("catalog");
x.style.color = "white";
} else {
var x = document.getElementById("catalog");
x.style.color = "black";
}
if (sPage == "search.html") {
var x = document.getElementById("search1");
x.style.color = "white";
} else {
var x = document.getElementById("search1");
x.style.color = "black";
}
if (sPage == "distributors.html") {
var x = document.getElementById("distributors");
x.style.color = "white";
} else {
var x = document.getElementById("distributors");
x.style.color = "black";
}
if (sPage == "service.html") {
var x = document.getElementById("service");
x.style.color = "white";
} else {
var x = document.getElementById("service");
x.style.color = "black";
}
if (sPage == "Mailto.html") {
var x = document.getElementById("contact");
x.style.color = "white";
} else {
var x = document.getElementById("contact");
x.style.color = "black";
}
}
/* End menuactive */
BUT - I have a problem. My css hover no longer works. CSS I am using is:
ul.topnav li a:hover:not(.active) {
color: Aqua;
text-decoration: none;
transition: none;
}
ul.topnav li a.active:hover,
ul.topnav li a.active {
color: white;
transition: none;
}
Hovered links do not turn "Aqua". Any ideas?
Thanks.
Larry
Follwing is a image for the horizontal scroll bar menu , i am trying to achieve with angular js.
Using $swipe service of angular js to perform this action.
Able to achieve the function calls at directives ng-swipe-left and ng-swipe-right.
As i have set the overflow-x:hidden for the items in the starting , how do i change the css or make the menu scrollable at the ng-swipe-left or ng-swipe-right.
Any other better suggestion to perform this action is welcomed.
Trying to make this happen by this Example . on ng-swipe-left and ng-swipe-right , incereasing /decreasing the counter below , indeed have to make the menu bar scroll.
<div ng-swipe-left="prev($event)" ng-swipe-right="next($event)">
Thanks in advance.
You could use ng-class to add the scroll effect and to show the menu you could use the $scope.index too.
I've added a boolean to change how the menu opens because I'm not sure how you'd like to open the menu.
If var openDirRight is true then index of the menu selection goes from 0 to 3 (3 = length of menu array). If it's false it goes from 0 to -3.
Later you could add $state.go('page' + index_with_formatting) to transition to the menu item.
Please have a look at the demo below or in this fiddle.
(The buttons are just for debugging on desktop because I'm not sure how to trigger the swipe on desktop.)
var app = angular.module('myapp', ['ngTouch']);
app.controller('MyCtrl', function MyCtrl($scope) {
var openDirRight = true; // true = swipe left to right shows menu index > 0
// false = swipe right to left shows menu index < 0
var stopActions = function ($event) {
if ($event.stopPropagation) {
$event.stopPropagation();
}
if ($event.preventDefault) {
$event.preventDefault();
}
$event.cancelBubble = true;
$event.returnValue = false;
};
// Carousel thing
$scope.index = 0;
// Hide menu
$scope.showMenu = false;
// Links
$scope.navigation = [{
title: "Page A",
href: "#pageA"
}, {
title: "Page B",
href: "#pageB"
}, {
title: "Page C",
href: "#pageC"
}];
$scope.checkMenuVisibility = function() {
$scope.showMenu = openDirRight ? $scope.index > 0 : $scope.index < 0;
};
$scope.isActive = function(index) {
return ( (openDirRight ? 1: -1 ) * $scope.index - 1 ) === index;
};
// Increment carousel thing
$scope.next = function ($event) {
stopActions($event);
$scope.index++;
// limit index
if ( openDirRight ) {
if ( $scope.index > $scope.navigation.length)
$scope.index = $scope.navigation.length;
}
else {
if ( $scope.index > 0)
$scope.index = 0;
}
$scope.checkMenuVisibility();
};
// Decrement carousel thing
$scope.prev = function ($event) {
stopActions($event);
$scope.index--;
// limit index
console.log($scope.index);
if ( !openDirRight ) {
if ($scope.index < -$scope.navigation.length) {
console.log('limited to -3');
$scope.index = -$scope.navigation.length;
}
}
else if ( $scope.index < 0 ) {
$scope.index = 0;
}
$scope.checkMenuVisibility();
};
});
html, body, #page {
height: 100%;
min-height: 100%;
}
.box {
background-color: #EFEFEF;
box-shadow: 0 1px 2px #dedede;
border: 1px solid #ddd;
border-radius: 4px;
}
.menu {
/*float: left;*/
/*min-height:100%;*/
/*width: 98%;*/
}
.menu ul {
}
.menu_item {
display: inline-block;
line-height:2;
}
.menu_link {
display:block;
padding-left:1em;
}
.menu_link:hover {
background: #DEDEDE;
}
.menu-grip {
float:right;
height:5em;
line-height:.5;
padding-top:2em;
text-align:center;
width:1em;
}
h1 {
background:black;
color:white;
font-size:1.1em;
line-height:1.3;
}
.big-swiper {
font-size: 5em;
height:3em;
line-height:3;
margin:.5em auto;
text-align:center;
width:3em;
}
.big-swiper:before {
content:'<\a0';
color:#dedede;
font-weight:700;
}
.big-swiper:after {
content:'\a0>';
color:#dedede;
font-weight:700;
}
.active {
background-color: blue;
}
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet"/>
<script src="http://code.angularjs.org/1.2.14/angular.js"></script>
<script src="http://code.angularjs.org/1.2.14/angular-touch.js"></script>
<div id="page" ng-cloak ng-app='myapp' ng-controller="MyCtrl" ng-swipe-left="">
<h1>Angular Swipe Menu</h1>
<div class="menu-grip box" ng-show="!showMenu" ng-click="showMenu = true">.<br />.<br />.</div>
<nav class="menu box" ng-show="showMenu"> <!-- ng-swipe-right="showMenu = false">-->
<ul>
<li class="menu_item" ng-repeat='nav in navigation track by $index'><a class="menu_link" ng-href="{{nav.href}}" ng-class="{'active': isActive($index)}">{{nav.title}}{{$index}}</a>
</li>
</ul>
</nav>
<!-- buttons for testing on desktop -->
<button ng-click="next($event)">swipe right</button>
<button ng-click="prev($event)" class="pull-right">swipe left</button>
<div class="big-swiper box" ng-swipe-right="next($event)" ng-swipe-left="prev($event)">{{index}}</div>
</div>
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I have a site that's using a two layered navigation menu. The top navigation goes to physical pages, while the filter navigation reloads the content in a div (#main) on the current page. This is done using jquery, and the "current" class style for each link is set (and hence the corresponding data is called) even on the first visit. In other words, what loads and where the "current" class style is set to is all managed by javascript and php every time.
This works beautifully Here
My problem is that the client now wants the Top Navigation to have different background colors for hover and "current" for each link.
I know I can do this with ID's.. but I'd rather do it using classes.. Is this possible?
for reference:
The css for the top nav list:
#nav_container {
position: relative;
float: left;
width: 100%;
}
#top_nav {
display: table;
table-layout: fixed;
list-style: none;
margin: 0px;
padding: 0px;
width: 100%;
font-family: mbaskerville-semibold;
overflow: hidden;
-webkit-box-shadow: 0 8px 6px -7px #666;
-moz-box-shadow: 0 8px 6px -7px #666;
box-shadow: 0 8px 6px -7px #666;
border-top: 2px solid #CCC;
border-bottom: 0.5px solid #CCC;
}
#top_nav li {
display: table-cell;
*float: left; /* improve IE7 */
height: 25px;
text-align: center;
line-height: 25px;
font-weight: bold;
border-right: 0.5px solid #CCC;
}
#top_nav li a {
display: block;
text-decoration: none;
color: #021020;
background-color: #FFFFFF;
}
#top_nav li a.current {
color: #FFFFFF;
background-color: #766A5A;
}
#top_nav li a:hover {
color: #FFFFFF;
background-color: #766A5A;
}
#top_nav li:first-child {
padding-left: 0;
border-left: 0.5px solid #CCC;
}
The javascript:
$(document).ready(function(){
// current page
var $current_page = window.location.pathname;
// top navigation
$(function() {
// set link to current page
if ( $current_page ) {
$('#top_nav a[href$="' + $current_page + '"]').attr('class', 'current');
}
// if link is root, set first child (home)
if ( $current_page.length <= 1) {
$('#top_nav a:first').addClass('current');
}
// if no filter set, set all as filter
if ($('#filter_nav a').hasClass('current')) {
// do nothing
}
else {
$('#filter_nav a:first').addClass('current');
// load new data when filter is changed
$filter = "all";
$(".page_header").load("test.php?", {page: $current_page, filter: $filter}, function(response, status, xhr) {
if(status == 'success') {
$('.page_header').html(response);
}
else if(status == 'error') {
var emsg = '<i>There was an error: ' + xhr.status + ' ' + xhr.statusText + '</i>';
$('.page_header').html(emsg);
}
else { alert(status); }
});
return false
}
});
// filter navigation
var $filter;
$('#filter_nav li a').click(
function(e) {
// prevent the default action & bubbling
e.preventDefault();
e.stopPropagation();
// handle filter change styles
$(this).closest('ul').find('.current').removeClass('current');
$(this).addClass('current');
// load new data when filter is changed
$filter = $(this).attr('href');
$(".page_header").load("test.php?", {page: $current_page, filter: $filter}, function(response, status, xhr) {
if(status == 'success') {
$('.page_header').html(response);
}
else if(status == 'error') {
var emsg = '<i>There was an error: ' + xhr.status + ' ' + xhr.statusText + '</i>';
$('.page_header').html(emsg);
}
else { alert(status); }
});
return false
});
});
The php class:
<?php
/**
* _document: /lib/omc_frmwrk/present/NavMan.php
*
* = Navigation Management class
* Management of standard navigational elements
*
* ** TO DO:
*
*
*/
// class definition
class NavMan {
/*
* class parameters
*
*/
private static $links;
private static $nav_style;
/**
* Getters
*
*/
/**
* Setters
*
*/
public static function setLinks($x) { self::$links = $x; }
public static function setNavStyle($x) { self::$nav_style = $x; }
/*
* __construct()
* PUBLIC method
* = empty
*
*/
public function __construct() {}
/*
* Navigation Menu:
* PUBLIC method
* = unordered list, css styled, dop-down capable
*
*/
public function setNav() {
// open unordered list
echo '<ul id="' . self::$nav_style . '">';
// set layer
$layer = 0;
// place array content into variables
for ($i = 0; $i < count(self::$links); $i++) {
$this_layer = self::$links[$i][0];
$class = self::$links[$i][1];
$link = self::$links[$i][2];
$page = self::$links[$i][3];
// check if layer is current
if ($layer < $this_layer) {
// open sub list
echo '<ul><li>';
// increase variable
$layer++;
}
else if ($layer == $this_layer) {
// open sub-layer
echo '</li><li>';
}
else if ($layer > $this_layer) {
// open sub-layer
echo '</li></ul><div class="clear"></li><li>';
// decrease variable
$layer--;
}
// place link
echo '<a class="' . $class . '" href="' . $page . '">' . $link . '</a>';
}
// close unordered list
echo '</li></ul><div class="clear"></div>';
}
}
?>
With jQuery you can use the following functions:
hasClass to check which class an element has, use it here instead of
$('#top_nav a[href$="' + $current_page + '"]').attr('class', 'current');`
addClassto add a class to a dom element
removeClass to remove a class from an element. Before you add a class, you have to first of all remove the previous one always e.g. here you did not remove the unwanted class first before adding a new one.
if ( $current_page.length <= 1) {
$('#top_nav a:first').addClass('current');
}
toggleClass to alternate between two classes.
The styling can be different depending on which class is active.
For further reading:
http://jquerybyexample.blogspot.com/2011/08/addclass-removeclass-hasclass-and.html
http://api.jquery.com/
To reference the a:hover class:
jQuery to target CSS a:hover class
http://api.jquery.com/hover/
Is there a way in ASP.NET MVC3 to customize the result of the Html.ValidationMessageFor(...) method? For example add an image next to the error text or similar...
But I want to be able to change the image whether the validation is successful or not...
You can see that the error is wrapped with a Css Class, all you need to do is use that Css Class at your own will.
The default MVC3 contains in the styles.css
/* Styles for validation helpers
-----------------------------------------------------------*/
.field-validation-error
{
color: #ff0000;
}
.field-validation-valid
{
display: none;
}
.input-validation-error
{
border: 1px solid #ff0000;
background-color: #ffeeee;
}
.validation-summary-errors
{
font-weight: bold;
color: #ff0000;
}
.validation-summary-valid
{
display: none;
}
just add your images there.
Yup
U can write ur own extension to do so
public static MvcHtmlString ValidationMessageFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression)
{
TagBuilder div = new TagBuilder("div");
div.AddCssClass("aside");
string modelName = ExpressionHelper.GetExpressionText(expression);
ModelState state = htmlHelper.ViewData.ModelState[modelName];
if (state != null)
if ((state.Errors != null) && (state.Errors.Count > 0))
div.AddCssClass("invalid");
else
div.AddCssClass("valid");
div.InnerHtml = htmlHelper.ValidationMessageFor(expression).ToString();
return MvcHtmlString.Create(div.ToString());
}
Then use something like this
#Html.EditorFor(model => model.Name)
#Html.ValidationMessageFor(model => model.Name)
And lastly u need to define ur css for invalid and aside class to customize how the error looks.