在DateTime LastUpdated列上使用Html.hidden进行版本跟踪时,MVC.NET毫秒丢失 [英] MVC.NET milliseconds get lost when using Html.hidden on a DateTime LastUpdated column for version tracking

查看:67
本文介绍了在DateTime LastUpdated列上使用Html.hidden进行版本跟踪时,MVC.NET毫秒丢失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简短的问题:

如何操作在MVC.NET中?

How tot do this in MVC.NET?

长版问题:

我正在尝试使用SQL表中的DateTime列进行版本跟踪(其本身被建议作为在被表的editview传回后重新附加Linq2Sql数据类的解决方案)

Im trying to use a DateTime column in a SQL Table for version tracking (this in itself is proposed as a solution to re-attaching a Linq2Sql data class after being passedback by a editview for the table)

但是现在我得到找不到或更改行"的异常.在ctx.submit上:

But now i get 'row not found or changed'-exception. on the ctx.submit:

 ctx.appointments.Attach(appointment,true);
 ctx.SubmitChanges();

其中ctx是我的Linq数据上下文,约会我的表,约会我尝试附加的约会对象..Ive将约会表设置为已汇总

where ctx is my Linq datacontext, appointments my table, and appointment the appointment object i try to attach.. Ive setup the appointments table as sugested here

建议的解决方案的读者也注意到了这个问题.他建议作为解决方案.

This problem was also noted by a reader of the suggested solution. He suggested this as a solution.

这里的问题是我不使用Webforms,而是我使用MVC.NET.所以我猜用

Problem here is im not using webforms, im using MVC.NET. So i'm guessing using

            <%= Html.Hidden("LastUpdate", Model.LastUpdate) %>                

更改LastUpdate属性. 那么有没有办法确保当它传递回我的更新操作中时得到相同(或至少相等)的DateTime对象?

makes the LastUpdate property change. So is there a way to make sure i get the same (or at least equal) DateTime object when this is passed back into my update action?

我在更新约会表时使用建议的gettime()触发器.这显然存储了一个sql datetime对象. Linq2SQL可能无法对其进行精确解析.我想这也加剧了问题.

Im using the suggested gettime() trigger on update of my appointments table. this obviously stores an sql datetime object. which might not be precicely parsed by Linq2SQL. I guess this also adds into the problem.

我希望对此有解决方案.我知道仅使用时间戳格式也可以解决该问题.但这不在此问题的范围内

I'm hoping there's a solution for this. I know just using the timestamp format would solve it also.. but that's outside the scope of this question

推荐答案

我认为问题在于将上次更新时间写到这样的隐藏字段中只会在默认情况下在日期上执行ToString, t表示毫秒.您可以尝试将时间戳渲染为某些绝对值,例如刻度:

I think the problem is that writing out the last update time into a hidden field like that will just do a ToString on the date which by default won't render out the milliseconds. You could try rendering out the timestamp as some absolute value such as ticks:

<%= Html.Hidden("LastUpdate", Model.LastUpdate.Ticks) %>

然后,您可以通过将值转换回long&来重新构造日期时间.重建DateTime:

You can then reconstruct your datetime on the other side by converting the value back into a long & reconstructing the DateTime:

var dt = new DateTime(Int64.Parse(ticks));

这篇关于在DateTime LastUpdated列上使用Html.hidden进行版本跟踪时,MVC.NET毫秒丢失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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