0

I have a page which consists of multiple partial views uploaded by a user. I have customized the Razor viewEngine to look at different locations for the .cshtml files. Now if i'm rendering a partial view _abc.cshtml(found on my external location) and this partial view requires a viewmodel AbcViewModel, whats the best way to deal with this?

  1. option 1: I have a DB entry that specifies _abc.cshtml uses AbcViewModel. So once I find the partial view, i'll know which viewmodel to use..In this case the ViewModel resides in the solution itself.

Using reflection I can get the class

  Assembly assembly = Assembly.GetExecutingAssembly();
  AbcViewModel viewModel= assembly.CreateInstance("AbcViewModel") as AbcViewModel;

I was wondering if there is a way that I could have the viewModel at an external location..this way I can extend my application..

Roar
  • 2,117
  • 4
  • 24
  • 39
Jovel JV
  • 11
  • 1
  • 4
  • How about simply parsing the partials' source code for `@model WhateverModel`? – andreister Jul 26 '13 at 06:40
  • hmm..sounds like a good idea..not sure how I would go about with that..could you help me out with a sample please...thanks – Jovel JV Jul 26 '13 at 08:15
  • If you are referring to `Razor.Parse(phsyicalfilepath,model)` I don't think i really will need that. – Jovel JV Jul 26 '13 at 08:26
  • No, I mean when you know which view to render, you find that `cshtml` file and actually parse it to find out which model type it needs. – andreister Jul 26 '13 at 08:37
  • oh ok ok...thats true!! I know the cshtml file and the model to be used.. my doubt was on how to find the model in the project(cause I just get the model name as a string), that can be done with reflection..so now it is whether it is possible to have the models located externally which I doubt is possible.. Also looking for other options to handle this scenario, thanks to you I have option 2 now! :) – Jovel JV Jul 26 '13 at 09:02
  • for creating types from an external assembly, check out http://stackoverflow.com/questions/465488/can-i-load-a-net-assembly-at-runtime-and-instantiate-a-type-knowing-only-the-na You'll probably want to require the models to implement a certain interface or something - or, as you said, use reflection for setting the model properties – andreister Jul 26 '13 at 10:54

0 Answers0