adding css to actionlink with area defined - asp.net-mvc

I have an #Html.ActionLink() as such
#Html.ActionLink("Ändra lösenord!!", "ChangePassword", "Manage", new { area = "Account" }, new { #class = "settingsMenu" })
But it wont take my class, how can I add a css-class to my ActionLink that has an area defined already?
On another link (without area it works fine)
#Html.ActionLink("Skapa konto", "Register", "", new { #class = "loginfront" })

You must change your code as follow:
Edited:
#Html.ActionLink("Ändra lösenord!!", "ChangePassword", new { area = "Account", controller = "Manage" }, new { #class = "settingsMenu" })

It was a conflict of css, added !important to my "settingsMenu" class and now it works.

Related

Html.BeginForm to call an action not in an Area?

How can I use Html.BeginForm to link from within an area to an action and controller not in an area? I am using:
using (Html.BeginForm("LogOff", "Account", FormMethod.Post, new { id = "logoutForm", #class = "navbar-right" }))
When I click the button it is looking for the controller in the area but it is not there.
Appreciate any suggestion.
Thanks on helpful comments, The solution is:
using (Html.BeginForm("LogOff", "Account", new { area = "" }, FormMethod.Post, new { id = "logoutForm", #class = "navbar-right" }))
Try explicitly providing null or empty string "" as the area:
using (Html.BeginForm("LogOff", "Account", FormMethod.Post, new { area = null, id = "logoutForm", #class = "navbar-right" }))

How to pass Area in Url.Action?

The problem in Html.ActionLink() is that you can't add additional html content inside the tag that it generates.
For example, if you want to add an icon besides the text like:
<i class="fa fa-users"></i> Go to Users
Using Html.ActionLink(), you can only generate:
Go to Users
So, to resolve this, you can use Url.Action() to generate only the URL inside the tag like:
// Here, Url.Action could not generate the URL "/admin/users". So this doesn't work.
<i class="fa fa-usesr"></i> Go to Users
// This works, as we know it but won't pass the Area needed.
<i class="fa fa-users"></i> Go to Users
So, how do you pass the Area using Url.Action()?
You can use this Url.Action("actionName", "controllerName", new { Area = "areaName" });
Also don't forget to add the namespace of the controller to avoid a conflict between the admin area controller names and the site controller names.
Something like this
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Admin_default",
"Admin/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional },
new[] { "Site.Mvc.Areas.Admin.Controllers" }
);
}
#Url.Action("{action}", "{controller}", new { Area = "areaname" });
#Html.ActionLink("LinkName", "{action}", "{controller}", new { area = "{areaname}" }, new { #class = "btn btn-cool" })
write area name as html attribute with anonymus object. you can use actionlink html helper extension method to achieve same thing.
If you want to create link for root controllers, it's enough to use this code:
Url.Action("ShowImage", "Page", new { Area = "" })
#Url.Action("{action}", "{controller}", new { Area = "areaname" });
#Html.ActionLink("LinkName", "{action}", "{controller}", new { area = "{areaname}"}, new { #class = "btn btn-cool" })
You can use above this

how to Hide dropdownfor in razor view in mvc 3?

I want to hide drop down according to class property value in asp.net mvc 3 razar view
You could apply a CSS class to the dropdown:
#Html.DropDownListFor(x => x.SelectedItemId, Model.Items, new { #class = "hidden" })
and then define the hidden CSS class:
.hidden {
display: none;
}
This hidden class could of course be applied dynamically based on some property value of the view model. For example you could write a custom HTML helper which could be used like so:
#Html.DropDownListFor(
x => x.SelectedItemId,
Model.Items,
Html.GetHtmlAttributes(Model.IsHidden)
)
where the GetHtmlAttributes is a custom extension method that you could write:
public static class HtmlExtensions
{
public static RouteValueDictionary GetHtmlAttributes(bool isHidden)
{
if (!isHidden)
{
return null;
}
return new RouteValueDictionary(new { #class = "hidden" });
}
}
Do you want to simply exclude the dropdown completely?
#if (!Model.DontShowDropdown)
{
#Html.DropDownListFor(....)
}
or, do you want to hide it?
Html.DropDownListFor(..., new { "style" = Model.DontShowDropDown ? "display:none" : "" };
You can use this ex:
#Html.DropDownList("mydropdown", new SelectList(new[] {
new SelectListItem{ Value = "0",Text="Item1"},
new SelectListItem{ Value = "1",Text="Item2"},
new SelectListItem{ Value = "2",Text="Item3"},
new SelectListItem{ Value = "3",Text="Item4"}
}, "Value", "Text", "2"), new
{
id = "mydropdown",
#class = "myddClass",
style = ""
})
in css add #mydropdown or .myddClass, then you can add display:none; in external css or add add it to inline css like style = "display:none;"

Can I add a class to an HTML.ActionLink in MVC3

I have this code and would like to add a class to the link. Is it possible to do this in MVC3?
Html.ActionLink("Create New", "Create")
Yes, you can just add another parameter with object representing css class:
Html.ActionLink("Create New", "Create", CONTROLLERNAME, null, new { #class= "yourCSSclass"} )
It can be translated to:
Html.ActionLink(link text, action name, controller name, route values object, html attributes object)
Edit:
To add custom styles, use this:
Html.ActionLink(
"Create New",
"Create",
CONTROLLERNAME,
null,
new { #class= "yourCSSclass", #style= "width:100px; color: red;" }
)
#Html.ActionLink("ClickMe", // link text
"Index", // action name
"Home", // controller
new { id = 2131 }, // (optional) route values
new { #class = "someClass" }) // html attributes
Html.ActionLink("Create New", "Create", null, htmlAttributes: new { #class = "className" })
You can use the ActionLink overload which takes an htmlAttributes parameter to add a class to the generated element:
Html.ActionLink("Create New", "Create", new {}, new { #class = cssClass });
According to the documentation, this should do the trick:
Html.ActionLink("LinkText", "Action", "Controller", new { }, new {#class="css class"})
Edit: Thanks for noticing Dampe, I updated the code sample.

MVC Html.BeginForm using Areas

I'm using MVC areas and on a view that's in an area called "Test" I would like to have a form that posts to the following method:
area: Security
controller: AccountController
method: logon
How can I make this happen with Html.BeginForm? Can it be done?
For those of you that want to know how to get it to work with the default mvc4 template
#using (Html.BeginForm("LogOff", "Account", new { area = ""}, FormMethod.Post, new { id = "logoutForm" }))
Try this:
Html.BeginForm("logon", "Account", new {area="Security"})
Try specifying the area, controller, action as RouteValues
#using (Html.BeginForm( new { area = "security", controller = "account", action = "logon" } ))
{
...
}
Use this for area with HTML Attributes
#using (Html.BeginForm(
"Course",
"Assign",
new { area = "School" },
FormMethod.Get,
new { #class = "form_section", id = "form_course" }))
{
...
}
#using (Html.BeginForm("", "", FormMethod.Post, new { id = "logoutForm", action = "/Account/LogOff" }))
{#Html.AntiForgeryToken()
<a class="signout" href="javascript:document.getElementById('logoutForm').submit()">logout</a>
}
For Ajax BeginForm we can use this
Ajax.BeginForm("IndexSearch", "Upload", new { area = "CapacityPlan" }, new AjaxOptions { HttpMethod = "POST", InsertionMode = InsertionMode.Replace, UpdateTargetId = updateTarget }, new { id = "search-form", role = "search" })

Resources