pass enum to html.radiobuttonfor MVC3

using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; using System.Text; using System.Web; using System.Web.Mvc; using System.Web.Mvc.Html; namespace YourNamespace { public static class HtmlExtensions { public static MvcHtmlString RadioButtonForEnum<TModel, TProperty>( this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression ) { var metaData = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData); var names = Enum.GetNames(metaData.ModelType); var sb = new StringBuilder(); foreach (var name in … Read more

Unobtrusive validation not working on dynamically-added partial view

Ok, I am going to start over with a new answer here. Before you call $.validator.unobtrusive.parse, remove the original validator and unobtrusive validation from the form like so: var form = $(“#main_div”).closest(“form”); form.removeData(‘validator’); form.removeData(‘unobtrusiveValidation’); $.validator.unobtrusive.parse(form); This same answer is documented here.

asp.net mvc decorate [Authorize()] with multiple enums

Here is a simple and elegant solution which allows you to simply use the following syntax: [AuthorizeRoles(MyEnum.Admin, MyEnum.Moderator)] When creating your own attribute, use the params keyword in your constructor: public class AuthorizeRoles : AuthorizeAttribute { public AuthorizeRoles(params MyEnum[] roles) { … } protected override bool AuthorizeCore(HttpContextBase httpContext) { … } } This will allow … Read more

Defining multiple Foreign Key for the Same table in Entity Framework Code First

To achieve what you want you need to provide some aditional configuration.Code First convention can identify bidirectional relationships, but not when there are multiple bidirectional relationships between two entities.You can add configuration (using Data Annotations or the Fluent API) to present this information to the model builder. With Data Annotations, you’ll use an annotation called … Read more

The ViewData item that has the key ‘MY KEY’ is of type ‘System.String’ but must be of type ‘IEnumerable’

I had same problem, and finally I got the answer… The problem is that in the POST action, after submitting the form, the ModelState is not valid, or it’s catching an error in try/catch, so the View is returned. But this time the View has not the ViewData[“basetype”] correctly set. You need to populate it … Read more