如何计算mvc中的日期差异并根据差异隐藏/显示Html.Actionlink

发布于 2024-11-24 08:16:44 字数 670 浏览 3 评论 0原文

我的数据库中有日期字段,我想要做的是找到当前日期和创建产品的日期(在数据库中)之间的日期差异。例如如果产品日期是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 技术交流群。

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

发布评论

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

评论(2

慈悲佛祖 2024-12-01 08:16:44

您的视图不应该负责进行计算。

通常,您的控制器会从数据库加载模型,并使用该模型填充适当的 ViewModel 对象(如上所述),该对象负责将数据“整形”为您的视图需要的格式。

尝试按照以下方式进行操作以下内容:-

MyViewModel.cs:-

public class MyViewModel
{
  public DateTime FirstDate { get; set; }
  public DateTime SecondDate { get; set; }

  public bool SheIsIntoYou
  {
    return SecondDate.Subtract(FirstDate).Days < 5
  }
}

在您的视图中(在 MyViewModel 上强类型化):-

<% if (Model.SheIsIntoYou) { %>
  <%: Html.ActionLink("Ask out again", "MyController", "MyAction") %>
<% } %>

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:-

public class MyViewModel
{
  public DateTime FirstDate { get; set; }
  public DateTime SecondDate { get; set; }

  public bool SheIsIntoYou
  {
    return SecondDate.Subtract(FirstDate).Days < 5
  }
}

And in your view (which is strongly typed on MyViewModel):-

<% if (Model.SheIsIntoYou) { %>
  <%: Html.ActionLink("Ask out again", "MyController", "MyAction") %>
<% } %>
不…忘初心 2024-12-01 08:16:44

使用 C# 计算天数差异

您可以使用的视图

<% if(Model.XXX > 5) { %>

   <!-- display --> 
   <p>
     <%= Html.ActionLink("Pay by Cheque", "PayByChecque", "Booking", null, new { id = "paycheque", @class ="test"  })%>
   </p>

<% } %>

模型在视图源的第一行中定义。在您的模型中,您定义一个字段来保存差异。

不要让视图计算它。

Calculating difference in days using C#

In the View you can use

<% if(Model.XXX > 5) { %>

   <!-- display --> 
   <p>
     <%= Html.ActionLink("Pay by Cheque", "PayByChecque", "Booking", null, new { id = "paycheque", @class ="test"  })%>
   </p>

<% } %>

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.

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