RegularExpressionAttribute – How to make it not case sensitive for client side validation?

I created this attribute which allows you to specify RegexOptions. EDIT: It also integrates with unobtrusive validation. The client will only obey RegexOptions.Multiline and RegexOptions.IgnoreCase since that is what JavaScript supports. [RegularExpressionWithOptions(@”.+@example\.com”, RegexOptions = RegexOptions.IgnoreCase)] C# public class RegularExpressionWithOptionsAttribute : RegularExpressionAttribute, IClientValidatable { public RegularExpressionWithOptionsAttribute(string pattern) : base(pattern) { } public RegexOptions RegexOptions { get; … Read more

Best practice for ASP.NET MVC resource files

You should avoid App_GlobalResources and App_LocalResources. Like Craig mentioned, there are problems with App_GlobalResources/App_LocalResources because you can’t access them outside of the ASP.NET runtime. A good example of how this would be problematic is when you’re unit testing your app. K. Scott Allen blogged about this a while ago. He does a good job of … Read more

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

MVC MapPageRoute and ActionLink

My guess is that you need to add some parameter options to the MapPageRoute declaration. So if you have more than one webforms page in the WebForms directory this works well. routes.MapPageRoute (“ReportTest”, “reports/{pagename}”, “~/WebForms/{pagename}.aspx”); PS: You may also want to have a look at the RouteExistingFiles property of RouteCollection An alternative would be to … Read more

ASP.NET MVC – Alternative for [Bind(Exclude = “Id”)]

Yes there is: it’s called view models. View models are classes which are specifically tailored to the specific needs of a given view. So instead of: public ActionResult Index([Bind(Exclude = “Id”)] SomeDomainModel model) use: public ActionResult Index(SomeViewModel viewModel) where the view model contains only the properties which need to be bound. Then you could map … Read more

How to achieve a dynamic controller and action method in ASP.NET MVC?

Absolutely! You’ll need to override the DefaultControllerFactory to find a custom controller if one doesn’t exist. Then you’ll need to write an IActionInvoker to handle dynamic action names. Your controller factory will look something like: public class DynamicControllerFactory : DefaultControllerFactory { private readonly IServiceLocator _Locator; public DynamicControllerFactory(IServiceLocator locator) { _Locator = locator; } protected override … Read more

.NET MVC : Calling RedirectToAction passing a model?

I don’t believe ModelBinding exists when using RedirectToAction. Your best options, however, is to use the TempData collection to store the object, and retrieve it in the following action. [AcceptVerbs(HttpVerbs.Post)] public ActionResult Add(…) { //… Kindergarten k = … TempData[“KG”] = k; return RedirectToAction(“List”); } In your List Action public ActionResult List() { Kindergarten k … Read more

How can I use Html.DisplayFor inside of an iterator?

Actually, I figured it out. How stupid of me. This works: <@ Page Inherits=”ViewPage<IEnumerable<Foo>>”> <% foreach(var item in Model) { %> <%: Html.DisplayFor(m => item.BarBaz) %> <% } %> However, this will not work correctly for Html.HiddenFor and Html.ValueFor. In particular, Html.HiddenFor(m => item.NullableDecimal) will render as <input name=”NullableDecimal” value=”0″ /> and Html.ValueFor(m => item.NullableDecimal, … Read more

RedirectToAction not working after successful jquery ajax post? [duplicate]

You cannot redirect from an AJAX post. You could return the URL you want to redirect the browser to however and redirect from Javascript. Controller [HttpPost] public ActionResult GoHome() { return Json(Url.Action(“Index”, “Home”)); } Javascript $.ajax({ type: “POST”, url: “http://localhost/UserAccount/GoHome”, dataType: ‘json’, crossDomain: true, success: function (data) { window.location.href = data; } });