简体   繁体   English

如何从View和文件背后的关联代码访问ViewModel?

[英]How can I access my ViewModel from the View and associated code behind file?

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模式,并在ViewModel中强烈键入我的View。

<%@ 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. 然后,例如,我想根据DayPreference中的内容来选中/取消选中某些复选框。 I don't get how to access the model from my code behind file, which looks like this: 我不知道如何从文件后面的代码访问模型,如下所示:

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. 另外,如果我尝试做<%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? 我应该如何访问ViewModel及其属性?

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. 对于每个需要回发的视图,控制器的每个操作方法都有两个重载:一个带有HttpGet签名,另一个带有HttpPost签名。 The GET version will be called on the first load of the page and will set the initial page values. GET版本将在页面首次加载时调用,并将设置初始页面值。

The POST version will be called on form submit and accept your viewmodel as an arg. POST版本将在表单提交时调用,并将您的viewmodel作为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). 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. 我自己的约定是在ViewModel中有一个工作单元,负责持久化或处理提交的值。 Do NOT put this sort of thing in the controller. 不要将这种东西放在控制器中。

Your viewmodel will need a parameterless constructor, which is the version MVC will use when reconstituting it on page submit. 您的视图模型将需要一个无参数的构造函数,该模型是MVC在页面提交上重构它时将使用的版本。 In general I also have a second constructor I use on the GET version so that the VM can instantiate it's initial values. 通常,我还有第二个构造函数,我可以在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());
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从后面的代码访问我的 ViewModel - How can I access my ViewModel from code behind 如何在ViewModel中处理Validation.Error而不是我后面的View代码? - How can I handle a Validation.Error in my ViewModel instead of my View's code behind? 如何在后面的代码上将自定义属性从View绑定到ViewModel? - How can I bind custom properties from View to ViewModel on code behind? 如何使用从视图到视图模型的绑定重构代码 - how can I refactor code using Binding from view to viewmodel 如何在没有代码隐藏的情况下处理 ViewModel 中的 WPF 路由命令? - How can I handle WPF routed commands in my ViewModel without code-behind? 如何从aspx代码后面的公共类访问公共方法? - how can I access my public method from aspx code behind to a public class? 我如何才能将动态对象从后面的代码访问到.xaml文件? - How can i access Dynamic objects from code behind to .xaml file? 我如何使用反射从文件后面的asp.net代码访问服务器端控件? - How can i access a server side control from asp.net code behind file using reflection? 如何从堆栈面板中的控件后面的代码访问? - How can i access from code behind, controls that are in a stack panel? 如何从后面的代码访问aspx网格? - How can I access the aspx grid from code behind?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM