How to get ELMAH to work with ASP.NET MVC [HandleError] attribute?

You can subclass HandleErrorAttribute and override its OnException member (no need to copy) so that it logs the exception with ELMAH and only if the base implementation handles it. The minimal amount of code you need is as follows: using System.Web.Mvc; using Elmah; public class HandleErrorAttribute : System.Web.Mvc.HandleErrorAttribute { public override void OnException(ExceptionContext context) { … Read more

Using Ajax.BeginForm with ASP.NET MVC 3 Razor

Example: Model: public class MyViewModel { [Required] public string Foo { get; set; } } Controller: public class HomeController : Controller { public ActionResult Index() { return View(new MyViewModel()); } [HttpPost] public ActionResult Index(MyViewModel model) { return Content(“Thanks”, “text/html”); } } View: @model AppName.Models.MyViewModel <script src=”https://stackoverflow.com/questions/5410055/@Url.Content(“~/Scripts/jquery.unobtrusive-ajax.js”)” type=”text/javascript”></script> <script src=”https://stackoverflow.com/questions/5410055/@Url.Content(“~/Scripts/jquery.validate.js”)” type=”text/javascript”></script> <script src=”https://stackoverflow.com/questions/5410055/@Url.Content(“~/Scripts/jquery.validate.unobtrusive.js”)” type=”text/javascript”></script> <div id=”result”></div> … Read more

Multiple models in a view

There are lots of ways… with your BigViewModel you do: @model BigViewModel @using(Html.BeginForm()) { @Html.EditorFor(o => o.LoginViewModel.Email) … } you can create 2 additional views Login.cshtml @model ViewModel.LoginViewModel @using (Html.BeginForm(“Login”, “Auth”, FormMethod.Post)) { @Html.TextBoxFor(model => model.Email) @Html.PasswordFor(model => model.Password) } and register.cshtml same thing after creation you have to render them in the main view … Read more

How can I properly handle 404 in ASP.NET MVC?

The code is taken from http://blogs.microsoft.co.il/blogs/shay/archive/2009/03/06/real-world-error-hadnling-in-asp-net-mvc-rc2.aspx and works in ASP.net MVC 1.0 as well Here’s how I handle http exceptions: protected void Application_Error(object sender, EventArgs e) { Exception exception = Server.GetLastError(); // Log the exception. ILogger logger = Container.Resolve<ILogger>(); logger.Error(exception); Response.Clear(); HttpException httpException = exception as HttpException; RouteData routeData = new RouteData(); routeData.Values.Add(“controller”, “Error”); if … Read more

What is ViewModel in MVC?

A view model represents the data that you want to display on your view/page, whether it be used for static text or for input values (like textboxes and dropdown lists) that can be added to the database (or edited). It is something different than your domain model. It is a model for the view. Let … Read more