validation for at least one checkbox

Working demo http://jsfiddle.net/RGUTv/ Updated – 12 Jan 2015 => http://jsfiddle.net/r24kcvz6/ CDN was cahnged http://ajax.aspnetcdn.com/ajax/jquery.validate/1.13.1/jquery.validate.js – you can replicate the behaviour by deselcting all the checkboxes and clicking on the submit button. http://jqueryvalidation.org/#latest-files-on-jsdelivr-cdn-%28hotlinking-welcome%29: code $.validator.addMethod(‘require-one’, function(value) { return $(‘.require-one:checked’).size() > 0; }, ‘Please check at least one box.’); var checkboxes = $(‘.require-one’); var checkbox_names = $.map(checkboxes, … Read more

Output cache per User

In your Web.config: <caching> <outputCacheSettings> <outputCacheProfiles> <add name=”Dashboard” duration=”86400″ varyByParam=”*” varyByCustom=”User” location=”Server” /> </outputCacheProfiles> </outputCacheSettings> </caching> In your Controller/Action: [OutputCache(CacheProfile=”Dashboard”)] public class DashboardController : Controller { …} Then in your Global.asax: //string arg filled with the value of “varyByCustom” in your web.config public override string GetVaryByCustomString(HttpContext context, string arg) { if (arg == “User”) { … Read more

Html inside label using Html helper

Looks like a good scenario for a custom helper: public static class LabelExtensions { public static MvcHtmlString LabelFor<TModel, TProperty>( this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> ex, Func<object, HelperResult> template ) { var htmlFieldName = ExpressionHelper.GetExpressionText(ex); var for = htmlHelper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(htmlFieldName); var label = new TagBuilder(“label”); label.Attributes[“for”] = TagBuilder.CreateSanitizedId(for); label.InnerHtml = template(null).ToHtmlString(); return MvcHtmlString.Create(label.ToString()); } } and then: … Read more

Razor reseverd words

Here’s a list of Razor reserved keywords (Note: This applies to cshtml, vbhtml follows VB’s rules): Razor-specific keywords inherits functions section helper model (only in MVC projects) You can escape these using @(inherits) Language-specific Razor keywords These are C# keywords that are understood by Razor if do try for foreach while switch lock using case … Read more

ASP.NET MVC Partial view ajax post?

Don’t redirect from controller actions that are invoked with AJAX. It’s useless. You could return the url you want to redirect to as a JsonResult: [HttpPost] public ActionResult _AddCategory(CategoriesViewModel viewModel) { if(//success) { // DbOperations… return Json(new { redirectTo = Url.Action(“Categories”) }); } else { // model state is not valid… return PartialView(viewModel); } } … Read more