Getting 404.0 error for ASP.NET MVC 3 app on IIS 7.0 / Windows Server 2008

You actually just reminded me that I needed to fix this issue in an enviroment here. If your situation is the same as mine then it’s a simple fix. Just add the following to your web config: <system.webServer> <modules runAllManagedModulesForAllRequests=”true” /> Edit: To provide further explanation on the issue at hand. In my case what … Read more

The model item is of type CookMeIndexViewModel, but requires a model item of type IEnumerable

In your view you are using @model IEnumerable<CookMe_MVC.ViewModels.CookMeIndexViewModel> which indicates that the model expected by the View is of type IEnumerable of CookMeIndexViewModel. However in the controller you are passing an object of type CookMeIndexViewModel as a model return View(viewModel); hence the error. Either change the view to have @model CookMe_MVC.ViewModels.CookMeIndexViewModel or pass a IEnumerable … Read more

Does a child action share the same ViewBag with its “parents” action?

Child actions follow a different controller/model/view lifecycle than parent actions. As a result they do not share ViewData/ViewBag. If you want to pass parameters to a child action from the parent you could do this: @Html.Action(“Child”, new { message = ViewBag.Message }) and in the child action: public ActionResult Child(string message) { … }

Can I return custom error from JsonResult to jQuery ajax error method?

You could write a custom error filter: public class JsonExceptionFilterAttribute : FilterAttribute, IExceptionFilter { public void OnException(ExceptionContext filterContext) { if (filterContext.RequestContext.HttpContext.Request.IsAjaxRequest()) { filterContext.HttpContext.Response.StatusCode = 500; filterContext.ExceptionHandled = true; filterContext.Result = new JsonResult { Data = new { // obviously here you could include whatever information you want about the exception // for example if you … Read more

How do I get the full name of a user in .net MVC 3 intranet app?

You can do something like this: using (var context = new PrincipalContext(ContextType.Domain)) { var principal = UserPrincipal.FindByIdentity(context, User.Identity.Name); var firstName = principal.GivenName; var lastName = principal.Surname; } You’ll need to add a reference to the System.DirectoryServices.AccountManagement assembly. You can add a Razor helper like so: @helper AccountName() { using (var context = new PrincipalContext(ContextType.Domain)) { … Read more

mvc [DataType(DataType.EmailAddress) no validation

You could use the usual DataAnnotations library by just using [EmailAddress] using System.ComponentModel.DataAnnotations; [Required] [EmailAddress] public String Email { get; set; } Also just for reference, here’s the regular expression version of this validation: [RegularExpression(@”^[A-Za-z0-9](([_\.\-]?[a-zA-Z0-9]+)*)@([A-Za-z0-9]+)(([\.\-‌​]?[a-zA-Z0-9]+)*)\.([A-Za-z]{2,})$”, ErrorMessage = “Email is not valid”)] public String Email {get; set;} Best of luck!

mvc upload file with model – second parameter posted file is null

Why not add the uploaded files to your model like this: public class UploadFileModel { public UploadFileModel() { Files = new List<HttpPostedFileBase>(); } public List<HttpPostedFileBase> Files { get; set; } public string FirstName { get; set; } // Rest of model details } Then change your view to this: @using (Html.BeginForm(“UploadFile”, “Home”, FormMethod.Post, new { … Read more