How do I maintain ModelState errors when using RedirectToAction?

The PRG pattern is ok, but I did this: Base controller: protected override void OnActionExecuted(ActionExecutedContext filterContext) { if (TempData[“ModelState”] != null && !ModelState.Equals(TempData[“ModelState”])) ModelState.Merge((ModelStateDictionary)TempData[“ModelState”]); base.OnActionExecuted(filterContext); } Action (I’m using xVal): try { user.Login(); AuthenticationManager.SignIn(user); } catch (RulesException rex) { // on bad login rex.AddModelStateErrors(ModelState, “user”); TempData[“ModelState”] = ModelState; return Redirect(Request.UrlReferrer.ToString()); } The action throws an … Read more

LINQ to Entities does not recognize the method ‘System.String ToString()’ method in MVC 4

You got this error because Entity Framework does not know how to execute .ToString() method in sql. So you should load the data by using ToList and then translate into SelectListItem as: var query = dba.blob.ToList().Select(c => new SelectListItem { Value = c.id.ToString(), Text = c.name_company, Selected = c.id.Equals(3) }); Edit: To make it more … Read more

How can I create a route constraint of type System.Guid?

Create a RouteConstraint like the following: public class GuidConstraint : IRouteConstraint { public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection) { if (values.ContainsKey(parameterName)) { string stringValue = values[parameterName] as string; if (!string.IsNullOrEmpty(stringValue)) { Guid guidValue; return Guid.TryParse(stringValue, out guidValue) && (guidValue != Guid.Empty); } } return false; }} Next when adding … Read more

ASP.NET MVC – Pass array object as a route value within Html.ActionLink(…)

Try creating a RouteValueDictionary holding your values. You’ll have to give each entry a different key. <% var rv = new RouteValueDictionary(); var strings = GetStringArray(); for (int i = 0; i < strings.Length; ++i) { rv[“str[” + i + “]”] = strings[i]; } %> <%= Html.ActionLink( “Link”, “Action”, “Controller”, rv, null ) %> will … Read more

What is the difference between DependencyResolver.SetResolver and HttpConfiguration.DependencyResolver in WebAPI

Prevent mixing MVC and Web API in the same project. Microsoft seems to suggest this, because the Visual Studio template for Web API mixes the project automatically with MVC, but this is a bad idea. From an architectural point of view, MVC and Web API are completely different. MVC is a UI technology aimed to … Read more