0

I am just trying to learn expandoobjects in mvc4.

I have a model say

 pulic class A
 {
   public string FirstName{ get; set; }        
   public string LastName{ get; set; }
   public System.DateTimeOffset DOB {get; set; }
 }

say I have another model

 pulic class B
 {
   public string JobTitle{ get; set; }        
   public System.DateTimeOffset FromDate { get; set; }
   public System.DateTimeOffset ThruDate {get; set; }
 }

I have controllers for each model. Now I will have a single view to list all the items of either model 'A' or model 'B'.

I heard that we can achieve this using expandoobjects, I should be able to written list of items of model 'A' or model 'B' to that single view and that view should display its content. Im missing how to start with this using expandoobjects.

Can somebody give some start pls?

svick
  • 236,525
  • 50
  • 385
  • 514
mmssaann
  • 1,507
  • 6
  • 27
  • 55

1 Answers1

1

I wouldn't even begin to try and do this, the amount of reflection work needed would be horrendous, a real maintenance nightmare

Set up a folder structure like this

-Views
    -Shared
         -EditorTemplates
              -TextBox.cshtml
              -SelectList.cshtml
              -DateBox.cshtml

In these editortemplates, setup the template as you wish it to be displayed, so add all the relevant styling or controls that you have developed in house. These will be your control templates.

Next, on each of your pocos, leverage the UIHint attribute, which will help Razor decide what template to use for each field.

So

public class A
{
   [UIHint("TextBox")]
   public string FirstName{ get; set; }   
   [UIHint("TextBox")]     
   public string LastName{ get; set; }
   [UIHint("DateBox")]
   public System.DateTimeOffset DOB {get; set; }
}

Now when editorformodel starts to select out it's templates, it should pick up your editortemplates and use those instead.

I'm not a fan of cluttering a poco with ui concerns, I much prefer cleaner pocos and specific views for clarity, however, I think will save you trying to work out what you need to output and use more of the templating features in mvc

Slicksim
  • 7,054
  • 28
  • 32
  • @Slicksin, thanks for the advise. Though the answer is not for the expando objects, I still have some questions on ur answer. Do u mean I should create a separate view for each poco object in shared displaytemplates? what about the views in general view folder?will it have views again that refers to display templates views? what about editertemplates, I also need edit views on the poco objects? to reduce number of view we are thinking of going with expando objects. can u answer the questions i put in comments? – mmssaann Jul 11 '13 at 08:40
  • If you don't want to create views, then you don't have to. Razor will parse a complex object and select what it thinks is the best editor for your model. Using expandos will not gain you anything other than massive views which have to cope with looking through the an object graph, parsing the type, detecting nullables and then outputting the correct editortype. you will also need code on the controller to stuff these expandos with the different pocos. a few extra views will drastically cut your code and ease your unit test creation. and lets not get started on validation :) – Slicksim Jul 11 '13 at 08:55
  • but my problem is I have to apply certain styles to the editors, for text boxex, drop downs , date times I have to use the jquery controls that we have internally developed. will this be possible with EditorForModel()? – mmssaann Jul 11 '13 at 08:58
  • one last question, so the FirstName property will look for EditorTemplates/TextBox.cshtml?? – mmssaann Jul 11 '13 at 09:33
  • http://stackoverflow.com/a/8197182/1563373 shows an example of it, it will look based on the command. so editorfor will look in the controller's views first, if it doesn't find the template there, it will look in the shared views folder for it. – Slicksim Jul 11 '13 at 09:47
  • i added TextBox.cshtml in shared editortemplates folder that has just this line:. When I am calling it from my main view, @Html.EditorForModel(), this way textbox control is getting rendered fine, but i dont know how to bind the value to it, can u advise pls? – mmssaann Jul 11 '13 at 11:03
  • if you are just outputting that textbox, you can use value="@Model", beware of nullable fields and make sure that the value put in is sensible. you will also need to work out what name to give the field, the metadata in the viewcontext should help you there – Slicksim Jul 11 '13 at 11:50
  • thanks, that is my next problem.can u guide me how to use metadatacontext to give it a name or id? – mmssaann Jul 11 '13 at 11:54
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/33271/discussion-between-slicksim-and-mmssaann) – Slicksim Jul 11 '13 at 12:12