ASP MVC 3:发送查看Modifiy值 [英] Asp MVC 3: Modifiy Values Sent to View

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

问题描述

据我了解 ModelBinder的可以生成类的实例出来的RouteData / FORMDATA

as far as I understand a ModelBinder can generate class instances out of routedata/formdata.

我正在寻找一种方法为操纵交给视图的数据之前,它被认为食用。

What I'm looking for is a way to manipulate the data handed over to the view before it is consumed by the view.

什么是possiblities?难道我错过了一些东西明显?

What are the possiblities? Do I miss something obvious?

在此先感谢!

修改结果
我不想送清除标识的客户端,但加密他们(至少在修改例)。碰巧的是很多时候我想这个步骤尽可能地自动化。结果
我期待像一个模型绑定器或属性附加到方法/视图模型/...

EDIT
I don't want to send clear IDs to the client but encrypt them (at least in edit cases). As it happens very often I want this step as much as possible automated.
I look for something like a ModelBinder or a Attribute to attach to a method/viewmodel/...

例如:结果
GET

Example:
GET

public ActionResult Edit(int id)
{
    var vm = new EditArticleViewModel();

    ToViewModel(repository.Get<Article>(id), vm);

    return View(vm); // id is something like 5 and should be encryped before being used by the view
}

查看

@model EditArticleViewModel

<div>
    @Html.HiddenFor(x => x.Id) <!-- x.Id should be encrypted, not just "5" -->
    ...
</div>

LG
warappa

Lg warappa

推荐答案

您可以做一个动作过滤器的内容:

You could do something with an action filter:

public class EncryptIDAttribute : ActionFilterAttribute
{
    public override void OnActionExecuted(ActionExecutedContext filterContext)
    {
        var vm = filterContext.Controller.ViewData.Model as EditArticleViewModel;

        if(vm != null)
        {
            vm.ID = SomeMethodToEncrypt(vm.ID);
        }
    }
}

和它适用于任何培训相关行动:

and apply it to any relevent actions:

[EncryptID]
public ActionResult Edit(int id)
{
    var vm = new EditArticleViewModel();

    ToViewModel(repository.Get<Article>(id), vm);

    return View(vm);
}

在该页面,然后贴你可以使用一个模型绑定解密的ID。

When the page is then posted you can use a model binder to decrypt the id.

如果你当时就想在多个视图模型,你可以看看它创建一个标志财产被加密的自定义数据批注应用此。在您的行为过滤器则可以寻找与该数据注解的属性和它们相应进行加密。

If you then wanted to apply this across multiple view models you could look at creating a custom data annotation which flags a property to be encrypted. In your action filter you can then look for any properties with this data annotation and encrypt them accordingly.

这篇关于ASP MVC 3:发送查看Modifiy值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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