Error: Parser Error Message: Only one 'model' statement is allowed in a file.

Topics: Troubleshooting, Writing modules
Dec 16, 2011 at 3:30 PM

I have Data Models

And Models in my WebinarPart

I need to use both of these references in my Razor file in order to write HTML for the models.

But I get this error: Parser Error Message: Only one 'model' statement is allowed in a file.

@model Pryor.Data.Models.IWebinars  <-- Can only use this one OR..
@model Pryor.Webinars.Models.WebinarPart <--- Can only use this one?? Cannot use them both??
@using Orchard.Mvc.Html
@using Pryor.Data.Models

Is there a way around this and why is this rule in place??

Dec 16, 2011 at 3:56 PM

Try using a ViewModel that has all the necessary objects as properties.  Then you can access them in the HTML.

 

Dec 16, 2011 at 4:31 PM

Thanks I applied you solution. But received the same error:

@model Pryor.Webinars.ViewModels.WebinarPartViewModel

tried

@using Pryor.Webinars.ViewModels.WebinarPartViewModel

it just gets ignored completely.


Dec 16, 2011 at 5:16 PM

You need to create something like this:

using Pryor.Data.Models
using Pryor.Webinars.Models

namespace Pryor.Webinars {
  public class WebinarViewModel() {
    public WebinarPart object1{ get; set;}
    public IWebinar object2 {get; set;}
  }
}

Then in your controller intantiate the new viewmodel. Then set the object1, and object2 with the objects you need. I would actually just put ALL the objects I need in the new ViewModel.

 

This works, this is the reason for ViewModels to have more than a single model at your disposal.

Coordinator
Dec 16, 2011 at 6:49 PM

Or use shapes, which are a lot easier.

Dec 16, 2011 at 10:02 PM

If WebinarPart implements IWebinars then you don't need both models, only your WebinarPart ... (How would you pass in two different models anyway?)