Newbie-ish questions ahead:
Where should I put my View Model declarations?
I created a file called “ViewModels.cs” in my Models folder.
I created a few View Model classes, including one called RatingViewModel:
using System;
using System.Collections.Generic;var
using System.Linq;
using System.Web;
namespace Web.Model
{
public class ViewModels
{
public class RatingViewModel
{
public User Rater { get; set; }
public Rating Rating { get; set; }
}
}
}V
Now I’m trying to create a helper/service class function that returns a new RatingViewModel object.
I created a “RatingsHelpers.cs” file.
But when I try to crate a new RatingViewModel object, it has no idea what I’m talking about.
I can’t figure it out. Am I missing some reference? How can I create/return a new View Model object from my helper/service class?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using Web.Model;
namespace System.Web.Mvc
{
public static class RatingsHelpers
{
public static RatingViewModel functionname()
{ ..... }
But it doesn’t know what “RatingViewModel” is…!?!?!?
I just know this should be obvious.
Pulling my hair out,
Johnny
To sum up:
ViewModelsclass inside theWeb.Modelnamespace. This class declares a nested class calledRatingViewModelRatingsHelperslocated in theSystem.Web.Mvcnamespace you are trying to access theRatingViewModelclass.You could achieve this by using
ViewModels.RatingViewModelbut I wouldn’t recommend you doing so. Nesting classes like this makes no sense.Instead you could place each view model class into a separate
.csfile and put it directly into theWeb.Modelnamespace:Then inside the helper class use could use directly the
RatingViewModelclass.