Validation on ASP.net MVC3 Model with Dynamic Properties?

Up vote 2 down vote favorite share g+ share fb share tw.

I am working on an app where I have a requirement to be able to load object properties at runtime from a database. The customer wants to be able to add attributes to the database and have them show up in the app. I am accomplishing this by giving my model a list of Field objects that contain a name, a type, and a value.

This works well for displaying and editing project properties, but I'm having trouble with validation in the editor view. Thanks for your help. I want to be able to do this in my Edit action: HttpPost public ActionResult Edit(Movie movie) { if (ModelState.

IsValid) { db. Entry(movie). State = EntityState.

Modified; db.SaveChanges(); return RedirectToAction("Index"); } return View(movie); } Normal view: @using (Html.BeginForm()) { @Html. ValidationSummary(true) Movie @Html. HiddenFor(model => model.

ID) @Html. LabelFor(model => model. Title) @Html.

EditorFor(model => model. Title) @Html. ValidationMessageFor(model => model.

Title) } What I need to do: @using (Html.BeginForm()) { @Html. ValidationSummary(true) Movie @Html. HiddenFor(model => model.

ID) for(i = 1 to n) { @Html. LabelFor(model => model.Fieldsi. Name) @Html.

EditorFor(model => model.Fieldsi. Value) @Html. ValidationMessageFor(model => model.Fieldsi.

Value) } } Model: public class Movie { public Movie() { this. Fields = new List(); } public List Movies { get; set; } } Field Class: public class Field { public string Name { get; set; } public Type Type { get; set; } public object Value { get; set; } } c# asp.net .net asp.net-mvc-3 web-applications link|improve this question edited Feb 24 at 18:18 asked Feb 23 at 18:35user1229080112.

I believe something like this should work: @using (Html.BeginForm()) { @Html. ValidationSummary(true) Movie @Html. HiddenFor(model => model.

ID) @{int I = 0;} @foreach(var field in model. Fields) { var htmlFieldName = string. Format("Fields{0}", i); @field.

Title @Html. EditorFor(model => field, null, htmlFieldName) @Html. ValidationMessage(htmlFieldName) } } (Note that I made up how you're producing your label text, since using the actual value as the label didn't make sense to me).

The POST should end up with values like this: ID=123 Fields0=Jaws Fields1=VeggieTales ... ... and that should automatically bind to your Movie model, provided the model has, for example, a List named Fields. If your model doesn't look like that, this should at least get you on the right track. Update In your comment, you explain that you are trying to produce an editor for an object.

There are two major points of difficulty here: MVC relies on the static type returned in the lambda expression you give to EditorFor to determine which kind of editor it should produce. To override this, you will need to provide a specific template name where my original suggestion shows you providing null: @Html. EditorFor(model => field.

Value, field.Type. Name, htmlFieldName + ". Value") You'll probably need to tweak this to make it provide the right template name for types like Integer, but this should give you the general idea.

When posting back, there is no way for the server to know that Field0 is an int, etc. You can either: Provide hidden values to specify each type, and then use a custom model binder that can consume this information to build each Field based on the combined Type and Value. Recreate the structure of the Movie object on the server side based on the Movie's ID, and then walk through each of it's Fields calling: TryUpdateModel((dynamic)field, string. Format("Field{0}", i)); There are probably other options, but that's about all the time I'm willing to put into this today.

Html. EditorFor(model => field.... Is incorrect use of linq – Manas Feb 23 at 18:53 1 @Manas: EditorFor is not a LINQ method at all, and while I don't like using EditorFor to accomplish what I'm doing here, this syntax does the job. If you find a better way, I'd be happy to change my answer.

– StriplingWarrior Feb 23 at 18:56 That works if Fields is a List. The problem is that Fields is a List, which contains a Name (string), a Type (type), and a Value (object). When I @Html.

EditorFor(model => field. Value, null, htmlFieldName), it doesn't render anything. If I change .

Value to a string or other primitive type, it does render something. – user1229080 Feb 24 at 14:54 @user1229080: In order to ensure that people give you the best possible answers, please update your question with this information about your model, as Manas asked you to. – StriplingWarrior Feb 24 at 14:57 1 @StriplingWarrior: I actually didn't need a custom template.

The problem now is that validation won't work if I pass in @Html. EditorFor(model => field. Value, null, htmlFieldName), but it does work if I do int fieldValue = field.

Value then @Html. EditorFor(fieldValue, field.Type. Name, htmlFieldName.

Of course I can validate that, but it isn't bound to the model since it's just a copy of the value. I made a custom Editor Template to make sure both fieldValue and field. Value were both handled as ints, and they are.

I guess I should actually open a new question for this issue. – user1229080 Feb 24 at 20:25.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions