【问题标题】:convert int to datetime in mvc3在 mvc3 中将 int 转换为 datetime
【发布时间】:2012-07-25 16:22:04
【问题描述】:

在我的模型课中,我有 public DateTime GoalDate { get; set; }

在我的视图类中我有

 @Html.TextBoxFor(model => model.weightGoalDate

在我的控制器中我有

 var newGoal = new Goal();

            newGoal.bpGoalDate = DateTime.Now ;

我想要的是将目标日期设置为当前日期后 1 周,并在页面加载时显示在文本框中。

例如,当我加载页面时,日期将自动设置为 2012 年 8 月 1 日,因为今天是 2012 年 7 月 25 日

【问题讨论】:

    标签: asp.net-mvc-3 datetime datepicker


    【解决方案1】:

    使用AddDays方法

    public ActionResult SomeAction()
    {
      var newGoal=new Goal();
      newGload.GoalDate =DateTime.Now.AddDays(7);
      return View(newGoal);  
    }
    

    来自msdn,

    返回一个新的 DateTime,它将指定的天数添加到 此实例的值。

    【讨论】:

    • 我如何在文本框中显示?
    • 使用@Html.TextBoxFor 辅助方法
    • 喜欢这个@Html.TextBoxFor(model =>model.GoalDate)?
    • 文本框不显示任何内容
    • 您的视图是强类型到Goal 类吗? .那么它应该。
    猜你喜欢
    • 1970-01-01
    • 2011-01-22
    • 1970-01-01
    • 1970-01-01
    • 2021-07-22
    • 2019-02-11
    • 2011-09-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多