简体   繁体   中英

ASP MVC2 - Dynamic Field Layout

I'm new to MVC and ADO.net Entity Framework. Instead of having to create an edit/display for each entity, I'd like to have the controller base class generate the view and validation code based off metadata stored in a table - something along those lines.

I would imagine something like this has already been done, or there are good reasons for not doing it. Any insight or suggestions are appreciated.

The display can be done using Html.DisplayForModel() or Html.EditorForModel(). This generates a view for the entity to render, but it probably won't look the way you like. I think it might inject validators, but that is based of data annotations, not the LINQ to Entity DB metadata.

As far as I know, I don't know of anything that automatically sucks in L2E model metadata... I was thinking of writing something myself too, as it would be very convenient to have.

HTH.

I don't like to answer my own question, but I found a library - [ASP.Net Dynamic Forms][1] - which was pretty much exactly what I needed.

It's pretty well written, isn't too complicated and easily extensible. The source author leaves the implementation very open ended, so far, I've been able to easily create a sql layer where the form settings reside, code looks something like this -

` string controllerName = controllerType.ToString();

        Form form = new Form();
        List<FormSetting> settings = new DataEntities().FormSettings.Where((c => c.ControllerName == controllerName)).ToList();

        foreach (FormSetting setting in settings)
        {
            Field fieldToAdd = CreateField(setting);
            form.Fields.Add(fieldToAdd);
        }`

[1]: http://mvcdynamicforms.codeplex.com/ .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM