基本视图模型类参考问题。 (ASP.net MVC)
新手可能会遇到的问题:
我应该将视图模型声明放在哪里?
我在 Models 文件夹中创建了一个名为“ViewModels.cs”的文件。 我创建了一些视图模型类,其中包括一个名为 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
现在我正在尝试创建一个返回新的 RatingViewModel 对象的帮助程序/服务类函数。
我创建了一个“RatingsHelpers.cs”文件。 但是当我尝试创建一个新的 RatingViewModel 对象时,它不知道我在说什么。 我想不通。我缺少一些参考吗?如何从我的帮助器/服务类创建/返回新的视图模型对象?
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()
{ ..... }
但它不知道“RatingViewModel”是什么......!?!?!?
我只知道这应该是显而易见的。
,把我的头发拔掉
约翰尼
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
总结一下:
Web.Model
命名空间内创建了一个ViewModels
类。该类System.Web.Mvc
命名空间中的帮助器RatingsHelpers
中声明了一个名为RatingViewModel
的嵌套类,您尝试访问RatingViewModel
类。您可以通过使用 ViewModels.RatingViewModel 来实现此目的,但我不建议您这样做。像这样的嵌套类是没有意义的。
相反,您可以将每个视图模型类放入单独的
.cs
文件中,并将其直接放入Web.Model
命名空间中:然后在辅助类中使用可以直接使用 <代码>RatingViewModel 类。
To sum up:
ViewModels
class inside theWeb.Model
namespace. This class declares a nested class calledRatingViewModel
RatingsHelpers
located in theSystem.Web.Mvc
namespace you are trying to access theRatingViewModel
class.You could achieve this by using
ViewModels.RatingViewModel
but 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
.cs
file and put it directly into theWeb.Model
namespace:Then inside the helper class use could use directly the
RatingViewModel
class.问题是您将
RatingViewModel
类放入 ViewModels 类中,这意味着它是ViewModels
的子类。我不确定这是否是您想要的行为。如果您想调用子类,则必须编写 ViewModels.RatingViewModel 来引用该类。
The problem is that you put the
RatingViewModel
class inside the ViewModels class, which means that it's a child class ofViewModels
. I'm not sure that's the behaviour you want.If you want to call a child class you have to write
ViewModels.RatingViewModel
to reference the class.扩展斯克里普尼所说的内容。将 RatingViewModel 类嵌套在 Web.Models 内的 ViewModels 类中似乎是多余的。您可能想摆脱外部 ViewModels 类。
Expanding on what scripni said. Having a RatingViewModel class nested inside a ViewModels class inside the Web.Models seems superfluous. You probably want to get rid of the outer ViewModels class.