Asp MVC 3:修改发送到视图的值

发布于 2024-11-05 18:16:15 字数 858 浏览 4 评论 0原文

据我了解,ModelBinder可以从routedata/formdata生成类实例。

我正在寻找的是一种在视图使用数据之前操作传递给视图的数据的方法。

有哪些可能性?我错过了一些明显的事情吗?

提前致谢!

编辑
我不想将清晰的 ID 发送给客户端,而是加密它们(至少在编辑情况下)。由于这种情况经常发生,我希望这一步尽可能自动化。
我寻找诸如 ModelBinder 或属性之类的东西来附加到方法/视图模型/...

示例:
获取

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 瓦拉帕

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.

What are the possiblities? Do I miss something obvious?

Thanks in advance!

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/...

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
}

View

@model EditArticleViewModel

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

Lg
warappa

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

请帮我爱他 2024-11-12 18:16:15

您可以使用操作过滤器执行某些操作:

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);
        }
    }
}

并将其应用于任何相关操作:

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

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

    return View(vm);
}

然后发布页面时,您可以使用模型绑定器来解密 id。

如果您想在多个视图模型中应用此功能,您可以考虑创建一个自定义数据注释来标记要加密的属性。然后,您可以在操作过滤器中查找具有此数据注释的任何属性并相应地对其进行加密。

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);
}

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.

嘴硬脾气大 2024-11-12 18:16:15

您可以编写一个自定义 HiddenFor 辅助方法来自动加密该值:

public static class HiddenExtensions
{
    public static MvcHtmlString HiddenForEncrypted<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> ex)
    {
        var metadata = ModelMetadata.FromLambdaExpression(ex, htmlHelper.ViewData);
        var name = ExpressionHelper.GetExpressionText(ex);
        var value = metadata.Model;
        var encryptedValue = SomeFunctionToEncrypt(value);
        return htmlHelper.Hidden(name, encryptedValue);
    }
}

作为替代方案,您可以使用 Html.Serialize 帮助程序在 MVCFutures 程序集中,它在幕后执行此操作。

所以基本上你会在你的视图中写入:

@Html.Serialize("id", Model.Id, SerializationMode.Encrypted)

和在你的控制器中:

public ActionResult Edit([Deserialize(SerializationMode.Encrypted)]int id)
{
    ...
}

You could write a custom HiddenFor helper method that will automatically encrypt the value:

public static class HiddenExtensions
{
    public static MvcHtmlString HiddenForEncrypted<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> ex)
    {
        var metadata = ModelMetadata.FromLambdaExpression(ex, htmlHelper.ViewData);
        var name = ExpressionHelper.GetExpressionText(ex);
        var value = metadata.Model;
        var encryptedValue = SomeFunctionToEncrypt(value);
        return htmlHelper.Hidden(name, encryptedValue);
    }
}

As an alternative you could use the Html.Serialize helper in the MVCFutures assembly that does this under the covers.

So basically you will write in your view:

@Html.Serialize("id", Model.Id, SerializationMode.Encrypted)

and in your controller:

public ActionResult Edit([Deserialize(SerializationMode.Encrypted)]int id)
{
    ...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文