使用自定义MVC扩展修改后的数据? [英] Modify post data with a custom MVC extension?

查看:113
本文介绍了使用自定义MVC扩展修改后的数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我在寻找到编写一些自定义的MVC扩展和第一个我试图解决是处理事情,如货币,日期和时间一FormattedTextBox。我有它的渲染可以正常使用,格式化,具有很强的类型和所有的一切工作金黄。不过,我现在遇到了问题清理格式化的东西时,页面职位回数据。

So I'm looking into writing some custom MVC extensions and the first one I'm attempting to tackle is a FormattedTextBox to handle things such as currency, dates, and times. I have the rendering of it working perfectly, formatting it, working with strong types and everything all golden. However, the problem I'm now running into is cleaning up the formatted stuff when the page posts the data back.

举个例子,一个货币格式。让我们用美元为这些例子。当一个对象有一个属性为小数,值将是 79.95 。你的编辑视图会是这样的:

Take for example, a currency format. Let's use USD for these examples. When an object has a property as a decimal, the value would be 79.95. Your edit view would be something like:

<%= Html.FormattedTextBox(model => Model.Person.HourlyWage, "{0:C}") %>

这是一切都很好的GET请求,但在POST,该值将是 $ 79.95 ,这时候你分配给该小数,得到不高兴非常迅速结束了推搡一个 0 在那里。

This is all well and good for the GET request, but upon POST, the value is going to be $79.95, which when you assign to that decimal, gets unhappy very quickly and ends up shoving a 0 in there.

所以我的问题是,如何才能获得code地方工作的MVC框架去之前与价值的工作,并开始推搡它放回我的ViewModel?我宁愿这样做服务器端比客户端。

So my question is, how do I get code working somewhere to work with that value before the MVC Framework goes and starts shoving it back into my ViewModel? I'd much rather this be done server-side than client-side.

谢谢!

推荐答案

一个解决办法是写一个的自定义模型粘合剂这将能够绑定 $ 79.95 转换为十进制场模型。另一种解决方案是做在客户端。在一个jQuery form.submit 处理程序,你可以修改被张贴在输入字段的值。

One solution would be to write a custom model binder which will be able to convert bind $79.95 to a decimal field in your model. Another solution is to do it on the client side. In a jQuery form.submit handler you could modify the values of the input fields to be posted.

这篇关于使用自定义MVC扩展修改后的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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