Asp MVC 3:修改发送到视图的值
据我了解,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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用操作过滤器执行某些操作:
并将其应用于任何相关操作:
然后发布页面时,您可以使用模型绑定器来解密 id。
如果您想在多个视图模型中应用此功能,您可以考虑创建一个自定义数据注释来标记要加密的属性。然后,您可以在操作过滤器中查找具有此数据注释的任何属性并相应地对其进行加密。
You could do something with an action filter:
and apply it to any relevent actions:
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.
您可以编写一个自定义
HiddenFor
辅助方法来自动加密该值:作为替代方案,您可以使用 Html.Serialize 帮助程序在 MVCFutures 程序集中,它在幕后执行此操作。
所以基本上你会在你的视图中写入:
和在你的控制器中:
You could write a custom
HiddenFor
helper method that will automatically encrypt the value: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:
and in your controller: