如何计算mvc中的日期差异并根据差异隐藏/显示Html.Actionlink
我的数据库中有日期字段,我想要做的是找到当前日期和创建产品的日期(在数据库中)之间的日期差异。例如如果产品日期是22/08/2012,当前日期是15/07/2011,那么差异是38天,计算差异一次后应该检查逻辑并根据它显示操作链接,
逻辑是固定的并且简单:
If(dateDifference > 5 )
{ show Actionfilter }
else
{
hide Actionfilter}
视图中的操作过滤器
<p> <%= Html.ActionLink("Pay by Cheque", "PayByChecque", "Booking", null, new { id = "paycheque", @class ="test" })%></p>
任何帮助或建议将不胜感激。
我正在考虑在控制器中进行计算并将其通过视图包传递给 jquery,例如:
$('#paycheque').hide();
$('Viewbag.difference').value > 5{
$("#showdiv").show();
else
$("#showdiv").hide();
});
但我正在努力计算差异
I have got the date field in my db , What I want to do is find the date difference betwwen current date and the date of the product created(which is in db) . For instance if the product date is 22/08/2012 and current date is 15/07/2011 therfore the difference is 38 days , After calculating the difference once should check the logic and display the action link according to it ,
The logic is fixed and simple:
If(dateDifference > 5 )
{ show Actionfilter }
else
{
hide Actionfilter}
Action filter in the view
<p> <%= Html.ActionLink("Pay by Cheque", "PayByChecque", "Booking", null, new { id = "paycheque", @class ="test" })%></p>
Any help or suggestion will be appreciated.
I was thinking of doing the calculation in the controller and pass it through view bag to the jquery like:
$('#paycheque').hide();
$('Viewbag.difference').value > 5{
$("#showdiv").show();
else
$("#showdiv").hide();
});
But I am struggling with calculating the difference
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的视图不应该负责进行计算。
通常,您的控制器会从数据库加载模型,并使用该模型填充适当的 ViewModel 对象(如上所述),该对象负责将数据“整形”为您的视图需要的格式。
尝试按照以下方式进行操作以下内容:-
MyViewModel.cs:-
在您的视图中(在 MyViewModel 上强类型化):-
Your view should not be responsible for doing the calculation.
Usually your controller would load a model from the database, and use that model to populate an appropriate ViewModel object (as above) which is responsible for "shaping" the data into the format that your view needs it in.
Try something along the lines of the following:-
MyViewModel.cs:-
And in your view (which is strongly typed on MyViewModel):-
使用 C# 计算天数差异
您可以使用的视图
模型在视图源的第一行中定义。在您的模型中,您定义一个字段来保存差异。
不要让视图计算它。
Calculating difference in days using C#
In the View you can use
Model is defined in the first line of the View-Source. In your Model you define a field, that holds the difference.
Don't let the View calculate it.