Html.EditorFor如何更改模型 [英] How does Html.EditorFor process changes to the model

查看:88
本文介绍了Html.EditorFor如何更改模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图弄清EditorFor为什么为什么以及如何缓存信息.

I'm trying to figure out why why and how EditorFor caches information.

这是一个简单的程序,具有一个两个文本区域.一种用于输入,另一种用于输出.输出文本区域的原因是它使全选副本更加容易.

This is a simple program that has one two textareas. One for input and one for output. The reason for a text area for output is that it makes a select all copy easier.

我的代码非常简单.首先,我的控制器输入一些默认值

My code is pretty simple. First my controller puts some default value

    public ActionResult MoveCss()
    {
        MoveCssIO model = new MoveCssIO(); ;
        model.InputCss = "Fish";
        return View(model);
    }

然后我的模型开始运行,并使输出成为输入的大写字母

Then my model kicks in and makes output the uppercase of input

    [Required(ErrorMessage="Please provide some text to transform")]
    [DataType(DataType.MultilineText)]
    public string InputCss { get; set; }


    private string _OutputCss;

    [DataType(DataType.MultilineText)]
    public string OutputCss { 
        get
        {
            Transform();
            return _OutputCss;
        }
        set
        {
            _OutputCss = value;
        }
    }

    private void Transform()
    {
        if (!string.IsNullOrWhiteSpace(InputCss))
        {
            _OutputCss = InputCss.ToUpper();
        }
    }

这是我的看法

##
@Html.Encode(Model.OutputCss)
$$
@Html.DisplayFor(model => model.OutputCss)
&&
@Html.EditorFor(model => model.OutputCss)
%%

所以在初始加载时我得到了

So at initial load I get

##
FISH
$$
FISH
&&
<textarea class="text-box multi-line" id="OutputCss" name="OutputCss">
FISH</textarea>
%%

然后我使用InputCss的编辑器(未显示)将InputCss上的值更改为ham

I then use an editor for InputCss (not shown) to change the value on InputCss to ham

    [HttpPost]
    public ActionResult MoveCss(MoveCssIO model)
    {
        return View(model);
    }

这将我的OutputCss设置为HAM,并且页面进行了重新加载(没有ajax),但是当我获取输出时是

This set my OutputCss to HAM and the page does a reload (no ajax) but when I get for output is

##
HAM
$$
HAM
&&
<textarea class="text-box multi-line" id="OutputCss" name="OutputCss">
FISH</textarea>
%%

我的问题是,为什么Html.EditorFor中的值与Html.DisplayFor中的值不同,我该如何解决?

My question is why is the value in Html.EditorFor different than Html.DisplayFor and how do i fix this?

已更新:更加清晰

推荐答案

您的问题非常不清楚,但我怀疑您正在尝试在POST操作中修改某些值,并且在渲染视图时仍看到旧值.如果是这种情况,则需要将其从modelstate中删除,否则html helpers将始终绑定旧值:

Your question is extremely unclear but I suspect that you are trying to modify some value in a POST action and you are still seeing the old value when the view is rendered. If this is the case you need to remove it from the modelstate or html helpers will always bind the old value:

[HttpPost]
public ActionResult MoveCss(MoveCssIO model)
{
    // you are changing a POSTed value here so make sure
    // you remove it from the model state or HTML helpers 
    // will use the original value
    model.InputCss = "ham"; 
    ModelState.Remove("InputCss");
    return View(model);
}

还请注意,编辑器/显示模板绝对没有缓存.

Also note that there is absolutely no caching involved with editor/display templates.

这篇关于Html.EditorFor如何更改模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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