如何从View和相关代码隐藏文件中访问我的ViewModel?

时间:2021-11-14 07:12:02

I am a real beginner at ASP.NET and working with MVC2 + EF4 in Visual Studio 2010.

我是ASP.NET的初学者,在Visual Studio 2010中使用MVC2 + EF4。

I am trying to use the MVVM pattern and strongly typing my View to a ViewModel.

我正在尝试使用MVVM模式并将我的View强烈键入ViewModel。

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master"        AutoEventWireup="True" CodeBehind="~/Views/Options/Index.aspx.cs" Inherits="System.Web.Mvc.ViewPage<OptionsViewModel>" %>

My OptionsViewModel looks like this:

我的OptionsViewModel看起来像这样:

 public class OptionsViewModel
{
    public List<DeskPreference> DeskPreferences { get; set; }
    public List<DayPreference> DayPreferences { get; set; }
}

In the controller I create a new OptionsViewModel and do return View(myOptionsViewModel);

在控制器中我创建一个新的OptionsViewModel并返回View(myOptionsViewModel);

Then, for example, I want to check/uncheck some boxes based on what is in DayPreference. I don't get how to access the model from my code behind file, which looks like this:

然后,例如,我想根据DayPreference中的内容检查/取消选中一些框。我不知道如何从我的代码隐藏文件访问模型,如下所示:

using System.Web.Mvc;
using DeskRota_v1.ViewModels;

public class OptionsPage : System.Web.Mvc.ViewPage<OptionsViewModel>
{
    protected void Page_Load(object sender, EventArgs e)
    {   
        setCheckBoxes();
    }

    private void setCheckBoxes()
    {           
        foreach (DayPreference dayPreference in Model.DayPreferences)
        {
\\ check boxes here
}
}

It comes up with "The name 'Model' does not exist in the current context". Also if I try to do <% Model. %> in the view there is no intellisense, which I thought there should be. Could somebody please explain what I am doing wrong? How am I supposed to access the ViewModel and its properties?

它提出了“名称'模型'在当前上下文中不存在”。如果我尝试做<%Model。 %>在视图中没有intellisense,我认为应该有。有人可以解释一下我做错了什么吗?我该如何访问ViewModel及其属性?

1 个解决方案

#1


0  

Your controller will have two overloads of each action method for each view that you need to post back: one with an HttpGet signature and one with an HttpPost signature. The GET version will be called on the first load of the page and will set the initial page values.

对于需要回发的每个视图,您的控制器将对每个操作方法都有两个重载:一个具有HttpGet签名,另一个具有HttpPost签名。将在第一次加载页面时调用GET版本,并将设置初始页面值。

The POST version will be called on form submit and accept your viewmodel as an arg. MVC will automagically reconstruct it with the values that were posted in your form (assuming you're using relatively simple types. More complex types is doable but more complicated).

POST版本将在表单提交时调用,并接受您的viewmodel作为arg。 MVC将使用您在表单中发布的值自动重建它(假设您使用的是相对简单的类型。更复杂的类型是可行但更复杂的)。

My own convention is to have a work unit in the ViewModel that is responsible for persisting or otherwise processing the values that were submitted. Do NOT put this sort of thing in the controller.

我自己的约定是在ViewModel中有一个工作单元,负责持久化或以其他方式处理提交的值。不要把这种东西放在控制器中。

Your viewmodel will need a parameterless constructor, which is the version MVC will use when reconstituting it on page submit. In general I also have a second constructor I use on the GET version so that the VM can instantiate it's initial values.

您的viewmodel将需要一个无参数构造函数,这是MVC在页面提交时重新构建它时将使用的版本。一般来说,我还有一个我在GET版本上使用的第二个构造函数,以便VM可以实例化它的初始值。

[HttpGet]
public ActionResult Index(int somethingICareAbout)
{
  return View(new IndexViewModel(somethingICareAbout));
}

[HttpPost]
public ActionResult Index(IndexViewModel viewModel)
{
  viewModel.SaveChanges()/DoWork()/Whatever();
  return View(new viewModel());
}

#1


0  

Your controller will have two overloads of each action method for each view that you need to post back: one with an HttpGet signature and one with an HttpPost signature. The GET version will be called on the first load of the page and will set the initial page values.

对于需要回发的每个视图,您的控制器将对每个操作方法都有两个重载:一个具有HttpGet签名,另一个具有HttpPost签名。将在第一次加载页面时调用GET版本,并将设置初始页面值。

The POST version will be called on form submit and accept your viewmodel as an arg. MVC will automagically reconstruct it with the values that were posted in your form (assuming you're using relatively simple types. More complex types is doable but more complicated).

POST版本将在表单提交时调用,并接受您的viewmodel作为arg。 MVC将使用您在表单中发布的值自动重建它(假设您使用的是相对简单的类型。更复杂的类型是可行但更复杂的)。

My own convention is to have a work unit in the ViewModel that is responsible for persisting or otherwise processing the values that were submitted. Do NOT put this sort of thing in the controller.

我自己的约定是在ViewModel中有一个工作单元,负责持久化或以其他方式处理提交的值。不要把这种东西放在控制器中。

Your viewmodel will need a parameterless constructor, which is the version MVC will use when reconstituting it on page submit. In general I also have a second constructor I use on the GET version so that the VM can instantiate it's initial values.

您的viewmodel将需要一个无参数构造函数,这是MVC在页面提交时重新构建它时将使用的版本。一般来说,我还有一个我在GET版本上使用的第二个构造函数,以便VM可以实例化它的初始值。

[HttpGet]
public ActionResult Index(int somethingICareAbout)
{
  return View(new IndexViewModel(somethingICareAbout));
}

[HttpPost]
public ActionResult Index(IndexViewModel viewModel)
{
  viewModel.SaveChanges()/DoWork()/Whatever();
  return View(new viewModel());
}