Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EditorTemplates/Object.cshtml using EditorFor() instead of Editor()?

I am trying to create a generic editor template that replicates Html.EditorForModel(), to later customize and build upon. Brad Wilson's template gets pretty close, but I found that it chokes when the same key exist in both ViewData (or ViewBag) and the model. For example ViewBag.Title causes problems if the view model also has a Title property.

I learned here that using strongly-type helpers (i.e. Html.EditorFor(x => x.Title) instead of Html.Editor("Title") seems to help. So I tried to modify Brad's template, but I ran into a brick wall, as nothing I tried so far has worked. I can't figure out how to use strongly-typed helpers in a context where I don't know the model type, like an editor template for example.

Is there any way to create an Object template like Brad's, but using strongly-typed helpers (i.e. LabelFor, EditorFor, ValidatorMessageFor) instead of weakly-typed ones (i.e. Label, Editor, ValidatorMessage)?

Thanks.

like image 823
Daniel Liuzzi Avatar asked Apr 14 '11 16:04

Daniel Liuzzi


People also ask

Why we use EditorFor in MVC?

The advantages of EditorFor is that your code is not tied to an <input type="text" . So if you decide to change something to the aspect of how your textboxes are rendered like wrapping them in a div you could simply write a custom editor template ( ~/Views/Shared/EditorTemplates/string.

How does HTML EditorFor work?

Simply put, the Html. EditorFor method allows the developer to retain control over the display of form elements by data type (ie. string, boolean, int…etc) or model attribute at a global level rather than at an individual view level. This allows for cleaner ASP markup and easily scalable form controls.

What is MVC editor template?

An EditorTemplate is a Razor file placed in the EditorTemplates folder: For Razor Pages apps, in the Pages/Shared/EditorTemplates folder. For MVC apps, in the Views/Shared/EditorTemplates folder or the Views/ControllerName/EditorTemplates folder.


2 Answers

I solved this problem in a slightly roundabout way, by removing the ViewData right before the call to @Html.Editor and then putting it back after.

Object.cshtml:

        object oldViewData = null;
        var hasConflictingViewData = ViewData.TryGetValue(prop.PropertyName, out oldViewData);

        if (hasConflictingViewData)
        {
            ViewData.Remove(prop.PropertyName);
        }

        @Html.Editor(prop.PropertyName)

        if (hasConflictingViewData)
        {
            ViewData.Add(prop.PropertyName, oldViewData);
        }

The only other option I could think of is using a ton of reflection to call EditorFor generically with a runtime type, and pass in an expression for the pertinent property.

like image 105
John Gibb Avatar answered Oct 23 '22 22:10

John Gibb


You can view all of the code for the new Object.shtml by going and downloading the MVC source code. I thought it was also in some common folder on your pc already but I can't remember where.

http://aspnet.codeplex.com/releases/view/58781

like image 28
Kyle Avatar answered Oct 23 '22 22:10

Kyle