ASP.NET MVC,可在控制器发生变异提交的值? [英] ASP.NET MVC, can the controller mutate the submitted values?

查看:143
本文介绍了ASP.NET MVC,可在控制器发生变异提交的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是在ASP.NET MVC允许改变提交的值?

  [HttpPost]
公众的ActionResult创建(人toCreate)
{
    toCreate.Lastname = toCreate.Lastname +-A-    返回查看(toCreate);
}

我试过code,但ASP.NET MVC继续显示用户提交的值

[更新]

这样的:

  [HttpPost]
公众的ActionResult创建(人toCreate)
{
    返回查看(新的Person {姓=侬});
}

或本

  [HttpPost]
公众的ActionResult创建(人toCreate)
{
    返回查看();
}

仍显示用户输入的值,这使我思考,为什么产生code需要发出:在HttpPost返回查看(toCreate)? 为什么不直接返回查看()?至少它不违反预期该值可以从控制器覆盖

[更新:2010-06-29]

在这里找到了答案:<一href=\"http://stackoverflow.com/questions/707569/asp-net-mvc-changing-models-properties-on-postback\">http://stackoverflow.com/questions/707569/asp-net-mvc-changing-models-properties-on-postback这里:<一href=\"http://stackoverflow.com/questions/2588588/setting-modelstate-values-in-custom-model-binder\">http://stackoverflow.com/questions/2588588/setting-modelstate-values-in-custom-model-binder

工作code:

  [HttpPost]
公众的ActionResult创建(人toCreate)
{
    ModelState.Remove(姓氏);
    toCreate.Lastname = toCreate.Lastname +-A-
    返回查看(toCreate);
}


解决方案

显然是没有办法重新验证的ModelState一旦你改变一些关键的值。和isValid仍然false,因为一个新的值设置为一些关键不触发重新验证。

解决方案是先删除触发IsValid的是假的,并重新创建它和值分配给它的关键。当你做到这一点的ModelState自动重新验证,如果一切都很好,的IsValid返回true。

这样的:

  bindingContext.ModelState.Remove(弹头);
    bindingContext.ModelState.Add(弹头,新的ModelState());
    bindingContext.ModelState.SetModelValue(弹头,新ValueProviderResult(obj.Slug,obj.Slug,NULL));

is it allowed in ASP.NET MVC to alter the submitted values?

[HttpPost]
public ActionResult Create(Person toCreate)
{
    toCreate.Lastname = toCreate.Lastname + "-A-";

    return View(toCreate);
}

i tried that code, but ASP.NET MVC keep showing the values submitted by the user

[UPDATE]

this:

[HttpPost]
public ActionResult Create(Person toCreate)
{
    return View(new Person { Lastname = "Lennon" });
}

or this:

[HttpPost]
public ActionResult Create(Person toCreate)
{
    return View();
}

still shows the values inputted by the user, which led me to thinking, why the generated code need to emit: return View(toCreate) in HttpPost? why not just return View()? at least it doesn't violate the expectations that the values can be overridden from controller

[UPDATE: 2010-06-29]

Found the answer here: http://stackoverflow.com/questions/707569/asp-net-mvc-changing-models-properties-on-postback and here: http://stackoverflow.com/questions/2588588/setting-modelstate-values-in-custom-model-binder

Working Code:

[HttpPost]
public ActionResult Create(Person toCreate)
{
    ModelState.Remove("Lastname");
    toCreate.Lastname = toCreate.Lastname + "-A-";
    return View(toCreate);
}

解决方案

Apparently there is no way to revalidate the ModelState once you change a value of some key. The IsValid remains false because setting a new value to some key does not trigger revalidation.

The solution is to first remove the key that triggered IsValid to be false and recreate it and assign the value to it. When you do that the ModelState automatically revalidates and if everything is fine, IsValid returns true.

Like this:

bindingContext.ModelState.Remove("Slug");
    bindingContext.ModelState.Add("Slug", new ModelState());
    bindingContext.ModelState.SetModelValue("Slug", new ValueProviderResult(obj.Slug, obj.Slug, null));

这篇关于ASP.NET MVC,可在控制器发生变异提交的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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