使用视图模型模式与MVC 2强类型HTML辅助 [英] Using ViewModel Pattern with MVC 2 Strongly Typed HTML Helpers

查看:134
本文介绍了使用视图模型模式与MVC 2强类型HTML辅助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我与ASP.NET MVC2 RC工作,无法弄清楚如何获得HTML帮手, TextBoxfor 视图模型模式即可工作。当编辑页面上使用时的UpdateModel()被调用控制器中的数据不会被保存。我采取了以下code例子从NerdDinner范例程序。

I am working with ASP.NET MVC2 RC and can't figure out how to get the HTML helper, TextBoxfor to work with a ViewModel pattern. When used on an edit page the data is not saved when UpdateModel() is called in the controller. I have taken the following code examples from the NerdDinner application.

Edit.aspx

Edit.aspx

<%@ Language="C#" Inherits="System.Web.Mvc.ViewUserControl<NerdDinner.Models.DinnerFormViewModel>" %>
...
<p>
    // This works when saving in controller (MVC 1)
    <label for="Title">Dinner Title:</label>
    <%= Html.TextBox("Title", Model.Dinner.Title) %>
    <%= Html.ValidationMessage("Title", "*") %>
</p>
<p>
    // This does not work when saving in the controller (MVC 2)
    <label for="Title">Dinner Title:</label>
    <%= Html.TextBoxFor(model => model.Dinner.Title) %>
    <%= Html.ValidationMessageFor(model=> model.Dinner.Title) %>
</p>

DinnerController

DinnerController

// POST: /Dinners/Edit/5

[HttpPost, Authorize]
public ActionResult Edit(int id, FormCollection collection) {

    Dinner dinner = dinnerRepository.GetDinner(id);

    if (!dinner.IsHostedBy(User.Identity.Name))
        return View("InvalidOwner");

    try {
        UpdateModel(dinner);

        dinnerRepository.Save();

        return RedirectToAction("Details", new { id=dinner.DinnerID });
    }
    catch {
        ModelState.AddModelErrors(dinner.GetRuleViolations());

        return View(new DinnerFormViewModel(dinner));
    }
}

在原来的助手风格时(Http.TextBox)中的UpdateModel(晚餐)调用按预期工作和新的值保存。

When the original helper style is used (Http.TextBox) the UpdateModel(dinner) call works as expected and the new values are saved.

在新的(MVC2)辅助风格时(Http.TextBoxFor)中的UpdateModel(晚餐)调用不会更新值。是的,目前价值被加载到负载的编辑页面。

When the new (MVC2) helper style is used (Http.TextBoxFor) the UpdateModel(dinner) call does not update the values. Yes, the current values are loaded into the edit page on load.

有没有别的东西,我需要添加到控制器code为它工作?如果我只是使用模型,而不是一个视图模型模式的新的辅助工作正常。

Is there something else which I need to add to the controller code for it to work? The new helper works fine if I am just using a model and not a ViewModel pattern.

感谢您。

推荐答案

这里的问题是你的编辑形式是使用强类型的辅助反对DinnerFormViewModel类型,但你在一个晚宴类型调用的UpdateModel。

The issue here is your Edit form is using strongly typed helpers against a DinnerFormViewModel type, but you're calling UpdateModel on a Dinner type.

当您使用强类型的辅助抗类型,助手创建表单域假设这就是你发布到类型。当类型不匹配,有一个问题。

When you use the strongly typed helpers against the type, the helpers create form fields assuming that's the type you're posting to. When the types don't match up, there's a problem.

不过,这是很容易解决。您可以提供preFIX到的UpdateModel这表明你没有尝试编辑整个模型,您试图编辑模型的属性,在这种情况下,晚餐。

However, this is very easy to fix. You can provide a prefix to UpdateModel which indicates that you weren't trying to edit the whole model, you were trying to edit a property of the model, in this case a Dinner.

UpdateModel(dinner, "Dinner");

另一种方法是调用的UpdateModel实际视图模型。

The other approach is to call UpdateModel on the actual ViewModel.

var viewModel = new DinnerFormViewModel();
viewModel.Dinner = repository.GetDinner(id);
UpdateModel(viewModel);

我认为第一种方式要好得多。

I think the first approach is much better.

这篇关于使用视图模型模式与MVC 2强类型HTML辅助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆