ASP.NET MVC 4 Custom Authorize Attribute with Permission Codes (without roles)

I could do this with a custom attribute as follows. [AuthorizeUser(AccessLevel = “Create”)] public ActionResult CreateNewInvoice() { //… return View(); } Custom Attribute class as follows. public class AuthorizeUserAttribute : AuthorizeAttribute { // Custom property public string AccessLevel { get; set; } protected override bool AuthorizeCore(HttpContextBase httpContext) { var isAuthorized = base.AuthorizeCore(httpContext); if (!isAuthorized) { … Read more

Bundler not including .min files

The solution I originally posted is questionable (is a dirty hack). The tweaked behaviour has changed in Microsoft.AspNet.Web.Optimization package and the tweak does not work anymore, as pointed out by many commenters. Right now I cannot reproduce the issue at all with the version 1.1.3 of the package. Please see sources of System.Web.Optimization.BundleCollection (you can … Read more

Execute Insert command and return inserted Id in Sql

The following solution will work with sql server 2005 and above. You can use output to get the required field. inplace of id you can write your key that you want to return. do it like this FOR SQL SERVER 2005 and above using(SqlCommand cmd=new SqlCommand(“INSERT INTO Mem_Basic(Mem_Na,Mem_Occ) output INSERTED.ID VALUES(@na,@occ)”,con)) { cmd.Parameters.AddWithValue(“@na”, Mem_NA); cmd.Parameters.AddWithValue(“@occ”, … Read more

MVC Force jQuery validation on group of elements

You can validate individual controls using Validator.element(element) – see documentation here, so you could use the following approach (you haven’t posted the views html so can’t write the actual code for you) In the Next button click event Select all the the controls within the associated div – e.g. var controls = $(this).closest(‘div’).find(‘input, textarea, select’); … Read more

How to make custom error pages work in ASP.NET MVC 4

My current setup (on MVC3, but I think it still applies) relies on having an ErrorController, so I use: <system.web> <customErrors mode=”On” defaultRedirect=”~/Error”> <error redirect=”~/Error/NotFound” statusCode=”404″ /> </customErrors> </system.web> And the controller contains the following: public class ErrorController : Controller { public ViewResult Index() { return View(“Error”); } public ViewResult NotFound() { Response.StatusCode = 404; … Read more