1

I have an ASP.NET MVC 3 (Razor) application that references an assembly where the assembly is nothing more than a bunch of entities that I use in my business logic.

I'm creating strongly-typed views by referencing entities/models that exist within my reference BL assembly. Unfortunately, by referencing models in this way, the scaffolding feature doesn't work - Specifically, MVC will create the View successfully, but will not include any of the scaffolding of the exposed properties that live on the referenced model.

The only way I was able to get scaffolding to work was to explicitly create a class within the Models directory that inherits from the entity and explicitly define properties similar to this:

Model Example

namespace MyMvcApplication.Models
{
    public class MyMvcModel: MyReferencedEntity
    {
        new public string Name { get { return base.Name; } }
        new public string Password { get { return base.Password; } }
        new public string Foo { get { return base.Foo; } }
        new public string Bar { get { return base.Bar; } }
    }
}

Now if I reference the above model when I create a strongly-typed View, the scaffolding feature works great and all is well.

My question is; Is there a way I can get scaffolding to work on a View that references a class that doesn't live in the Models directory so that I don't have to recreate the class like I did in the above example?

Jed
  • 10,649
  • 19
  • 81
  • 125
  • 1
    Which aspect of scaffolding are you having trouble with. I tried the approach you describe using the Required attribute. And both scenarios worked as expected? – Stewart Ritchie Feb 06 '12 at 21:24
  • The aspect where all the public properties of the Model are added to the View (eg. @Html.LabelFor(model=>model.Name). Instead of creating the HTML controls for all my Model properties, the only thing that is generated is the @Html.BeginForm() along with some HTML controls that have nothing to do with my Model. I'm interested in the fact that it works for you - I have to ask; is your Model assembly added to the list of your application's *References* - or is your Model a class that lives within your application assembly? – Jed Feb 06 '12 at 22:05
  • My model assembly was added to the MVC application's references. – Stewart Ritchie Feb 06 '12 at 22:11
  • @StewartRitchie - I just created a new Solution that contains an ASP.NET MVC project and a ClassLibrary project. In the MVC proj, I added a reference to the ClassLibrary. In the MVC project, I created a Controller and a strongly-typed View that models a class from the ClassLibrary - The scaffolding worked! (just as you mentioned). There must be something about my entity that is causing problems that I will need to discover. Thanks for your input - simply letting me know it works for you helped me to approach solving the problem with a different perspective ;) – Jed Feb 06 '12 at 22:14
  • Can you post the code that is broken? As far as I know, there are no specific rules that say you must put your model classes in the 'Models' folder. – Stewart Ritchie Feb 06 '12 at 22:18
  • Ok, glad you're making progress! – Stewart Ritchie Feb 06 '12 at 22:19
  • Making progress, indeed. It looks as though the MVC Scaffolder ignores types that are generic lists, custom types, members, and probably more that I'm not aware of as of yet. In a nutshell, it looks as though it only scaffs Properties that are of a primitive value type. Still testing, though - I'm not 100% sure about what I'm seeing. – Jed Feb 06 '12 at 22:44
  • 1
    Yeah, you need to look for "Custom Binding" examples. – Stewart Ritchie Feb 07 '12 at 20:03
  • Double check if your classes are public. – Carlos Araujo Jul 28 '16 at 12:25

1 Answers1

0

Well I just have encountered exactly same scenario.I followed these steps .Much late but could be helpful for others :

  • Unload your model project
  • Reload it back
  • Build model project
  • Add project reference to model project from web app project
  • Try creating scaffolding controller.

Hope this helps!

Debashish Saha
  • 318
  • 1
  • 12