ASP.NET MVC 3 Custom HTML Helpers- Best Practices/Uses

I use HtmlHelpers all the time, most commonly to encapsulate the generation of boilerplate HTML, in case I change my mind. I’ve had such helpers as: Html.BodyId(): generates a conventional body id tag for referencing when adding custom css for a view. Html.SubmitButton(string): generates either an input[type=submit] or button[type=submit] element, depending on how I want … Read more

Asp.net MVC Authorize attribute, redirect to custom “no rights” page

You could write custom filter attribute like this: public class CustomAuthorizeAttribute : ActionFilterAttribute { public override void OnActionExecuting(ActionExecutingContext filterContext) { if (filterContext.HttpContext.User.Identity == null || !filterContext.HttpContext.User.Identity.IsAuthenticated) { filterContext.Result = new RedirectResult(System.Web.Security.FormsAuthentication.LoginUrl + “?returnUrl=” + filterContext.HttpContext.Server.UrlEncode(filterContext.HttpContext.Request.RawUrl)); } //Check user right here if (userNotRight) { filterContext.HttpContext.Response.StatusCode = 302; filterContext.Result = new HttpUnauthorizedResult(); } } } And use … Read more

ActionLink htmlAttributes with hyphens

The problem is that your anonymous object property data-icon has an invalid name. C# properties cannot have dashes in their names. There are two ways you can get around that: Use an underscore instead of dash (MVC will automatically replace the underscore with a dash in the emitted HTML): @Html.ActionLink(“Edit”, “edit”, “markets”, new { id … Read more

Are

the colon syntax means you’ll be html encoded automatically: http://haacked.com/archive/2009/09/25/html-encoding-code-nuggets.aspx They couldn’t just html encode all the existing <%= blocks, because things that are already properly encoded (which is hopefully most of the projects out there) would look strange.

Validating for large files upon Upload

One possibility is to write a custom validation attribute: public class MaxFileSizeAttribute : ValidationAttribute { private readonly int _maxFileSize; public MaxFileSizeAttribute(int maxFileSize) { _maxFileSize = maxFileSize; } public override bool IsValid(object value) { var file = value as HttpPostedFileBase; if (file == null) { return false; } return file.ContentLength <= _maxFileSize; } public override string … Read more

How do I clear MVC client side validation errors when a cancel button is clicked when a user has invalidated a form?

This answer is for MVC3. See comments below for help updating it to MVC 4 and 5 If you just want to clear the validation-messages so that they are not shown to the user you can do it with javascript like so: function resetValidation() { //Removes validation from input-fields $(‘.input-validation-error’).addClass(‘input-validation-valid’); $(‘.input-validation-error’).removeClass(‘input-validation-error’); //Removes validation message after … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)