如何发送,查看之前修改控制器动作中提交的表单数据? [英] How to modify posted form data within controller action before sending to view?

查看:72
本文介绍了如何发送,查看之前修改控制器动作中提交的表单数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想呈现一个成功的行动(而不是使用RedirectToAction)后,同样的看法,但我需要修改呈现该视图模型数据。以下是证明两个方法不起作用一个人为的例子:

I want to render the same view after a successful action (rather than use RedirectToAction), but I need to modify the model data that is rendered to that view. The following is a contrived example that demonstrates two methods that that do not work:

    [AcceptVerbs("POST")]
    public ActionResult EditProduct(int id, [Bind(Include="UnitPrice, ProductName")]Product product) {
        NORTHWNDEntities entities = new NORTHWNDEntities();

        if (ModelState.IsValid) {
            var dbProduct = entities.ProductSet.First(p => p.ProductID == id);
            dbProduct.ProductName = product.ProductName;
            dbProduct.UnitPrice = product.UnitPrice;
            entities.SaveChanges();
        }

        /* Neither of these work */
        product.ProductName = "This has no effect";
        ViewData["ProductName"] = "This has no effect either";

        return View(product);
    }

有谁知道正确的方法是完成这个?

Does anyone know what the correct method is for accomplishing this?

推荐答案

进一步研究这个之后,我为什么以下code在操作没有任何影响的解释:

After researching this further, I have an explanation why the following code has no effect in the Action:

product.ProductName = "This has no effect";
ViewData["ProductName"] = "This has no effect either";

我的视图使用HTML助手:

My View uses HTML Helpers:

<% Html.EditorFor(x => x.ProductName);

HTML助手试图关键的查找时使用以下顺序precedence:

HTML Helpers uses the following order precedence when attempting lookup of the key:


  1. ViewData.ModelState字典项

  2. 模型属性(如果一个强类型的视图。这个属性是一个快捷方式到View.ViewData.Model)

  3. ViewData字典条目

有关HTTP POST操作,ModelState中总是被填充,因此修改模型(product.ProductName)或直接的ViewData(计算机[产品名称])没有效果。

For HTTP Post Actions, ModelState is always populated, so modifying the Model (product.ProductName) or ViewData directly (ViewData["ProductName"]) has no effect.

如果您确实需要直接修改的ModelState,语法这样做是:

If you do need to modify ModelState directly, the syntax to do so is:

ModelState.SetModelValue("ProductName", new ValueProviderResult("Your new value", "", CultureInfo.InvariantCulture));

或者,要清除的ModelState值:

Or, to clear the ModelState value:

ModelState.SetModelValue("ProductName", null);

您可以创建一个扩展方法来简化的语法:

You can create an extension method to simplify the syntax:

public static class ModelStateDictionaryExtensions {
    public static void SetModelValue(this ModelStateDictionary modelState, string key, object rawValue) {
        modelState.SetModelValue(key, new ValueProviderResult(rawValue, String.Empty, CultureInfo.InvariantCulture));
    }
}

然后,你可以简单地写:

Then you can simply write:

ModelState.SetModelValue("ProductName", "Your new value");

有关详细信息,请参阅MVC2数据视图功耗

For more details, see Consumption of Data in MVC2 Views.

这篇关于如何发送,查看之前修改控制器动作中提交的表单数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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