MVC 3值丢失后,发送到客户端 [英] MVC 3 Value Lost After Sent to Client

查看:139
本文介绍了MVC 3值丢失后,发送到客户端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须失去了一些关于如何工作的,因为我不知道这一点。也许有人在这里可以提供帮助。

我的地址对象有一个名为ValidationStatus属性。这不是在屏幕上可见,但有一个隐藏字段:

  @ Html.HiddenFor(型号=> model.ValidationStatus)

所以,我运行程序,打开一个现有的地址,其中有OK一ValidationStatus,并更改地址,以便它是无效的。然后,我张贴的形式向控制器。对象的验证方法调用第三方服务,它返回一个错误。在code设置ValidationStatus为无效,并返回一个验证消息视图。

在查看负载,ValidationStatus已正确设置为无效,因为我可以通过查看调试下面的语句看到:

  @if(Model.ValidationStatus ==无效)//显示一个附加字段。

所以,我在新的字段中输入数据,并再次发布的形式向控制器。在控制器中的第一行,我把一个断点,支票托收[ValidationStatus]在即时窗口。这是OK,而不是无效。

我缺少的是在这里吗?为什么没有价值坚守?没有什么在客户端可以改变该值。

下面是控制器code(pretty基本的,真的):

  [HttpPost]
公众的ActionResult指数(的FormCollection收集,串destinationControllerName)
{
    prepareSecondaryData(); //要返回负载下拉的情况下,列出了需要查看    尝试
    {
        如果(!TryUpdateModel(_policy))
            返回查看(_policy);        如果(!_services.PolicyEditor.SavePolicy(_policy))
            返回查看(_policy);
    }
    赶上(例外EXP)
    {
        UIHelper.Log(UIHelper.LogLevel.Error,对此,保存过程中的错误,EXP);
        ViewBag.Error = UIHelper.GenericErrorMessage();
        返回查看(_policy);
    }    返回RedirectToAction(指数,destinationControllerName);
}


解决方案

在渲染视图客户端的的ModelState 在提供模型的数值的最高优先级。这是您的情况的情况。当视图第一次发送到客户端, ValidationStatus 相应的的ModelState [ValidationStatus] 还没有得到价值,所以它需要模特的价值 - OK。当它被发送到服务器,的ModelState [ValidationStatus] 填充了OK - 从客户的隐藏字段发送。当通过第三方验证,并再次回去,即使 model.ValidationStatus ==无效的的ModelState [ValidationStatus] == OK,所以根据后者更高的优先级,ModelState中为模型设定值OK。和客户端的隐藏字段值获取OK。要修复它,做这样的事情。

 的ModelState [ValidationStatus]值=新ValueProviderResult(无效的,无效,CultureInfo.CurrentCulture)。

总的想法是,在ModelState中数组对应的记录应该有一个模型正确的值。

更新:

或者,从ModelState中明确的价值,使从模型MVC使用价值。 ModelState.Remove(ValidationStatus)

I must be missing something about how this works, because I can't figure this out. Maybe someone here can help.

My Address object has a property called ValidationStatus. It is not visible on the screen, but has a hidden field:

@Html.HiddenFor(model => model.ValidationStatus)

So, I run the program, open an existing address, which has a ValidationStatus of "OK", and change the address so that it is invalid. I then posts the form to the Controller. The object's Validate method calls the 3rd-party service, which returns an error. The code sets ValidationStatus to "Invalid" and return the View with a validation message.

When the View loads, ValidationStatus is properly set to "Invalid" as I can see by debugging the following statement in the View:

@if (Model.ValidationStatus == "Invalid") //show an additional field.

So I enters data in the new field and again post the form to the Controller. In the first line in the controller, I put a breakpoint and check collection["ValidationStatus"] in the immediate window. It is "OK" instead of "Invalid".

What am I missing here? Why didn't the value stick? There is nothing on the client side that can change that value.

Here's the controller code (pretty basic, really):

[HttpPost]
public ActionResult Index(FormCollection collection, string destinationControllerName)
{
    PrepareSecondaryData(); // loads drop-down lists in case the View needs to be returned

    try
    {
        if (!TryUpdateModel(_policy))
            return View(_policy);

        if (!_services.PolicyEditor.SavePolicy(_policy))
            return View(_policy);
    }
    catch (Exception exp)
    {
        UIHelper.Log(UIHelper.LogLevel.Error, this, "Error during Save", exp);
        ViewBag.Error = UIHelper.GenericErrorMessage();
        return View(_policy);
    }

    return RedirectToAction("Index", destinationControllerName);
}

解决方案

When rendering view to client, ModelState has the highest priority in providing values of model. That is the case in your situation. When the view is first time sent to client, ValidationStatus corresponding ModelState["ValidationStatus"] has not got value, so it takes model's value - "OK". When it is posted to server, ModelState["ValidationStatus"] is populated with "OK" - sent from hidden field from client. And when validated by 3rd party and returned back again, even though model.ValidationStatus == "Invalid", the ModelState["ValidationStatus"] == "OK", so according to higher priority of latter, ModelState sets value "OK" for the model. And the client gets value "OK" in the hidden field. To fix it, do something like

  ModelState["ValidationStatus"].Value = new ValueProviderResult("Invalid", "Invalid", CultureInfo.CurrentCulture);

General idea is that corresponding record in ModelState array should have correct value for the model.

UPDATE:

Or alternatively, clear value from modelstate, to make MVC use value from model. ModelState.Remove("ValidationStatus")

这篇关于MVC 3值丢失后,发送到客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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