将datetime2数据类型转换为日期时间数据类型会导致超出范围的值?

时间:2014-02-28 09:35:41

标签: jquery asp.net-mvc-3 entity-framework jquery-ui razor

我有以下型号代码:

public class MyPartialViewModel1{
 public DateTime model1Date {get; set;};
     // other string properties
    }
 public class MyPartialViewModel2{
  public DateTime model2Date {get; set;}
 // other string properties
}

MyPartialViewModel1的部分视图是_MyPartialView1:

@model MyProjectNameSpace.MyPartialViewModel1
 <script type = "text/javascript">
  $('#model1Date').datepicker().bind('cut copy paste', function (e) {
            e.preventDefault(); 
        });
 </script>

@using (HTML.BeginForm("MyPartialView1PostToAction","MyController"))
{
   @HTML.TextBoxFor(model => model.model1Date)
//  other model properties razor code
 // also have a submit button for the form
}

MyPartialViewModel2的部分视图是_MyPartialView2:

 @model MyProjectNameSpace.MyPartialViewModel2
     <script type = "text/javascript">
      $('#model2Date').datepicker().bind('cut copy paste', function (e) {
                e.preventDefault(); 
            });
     </script>

    @using (HTML.BeginForm("MyPartialView2PostToAction","MyController"))
    {
       @HTML.TextBoxFor(model => model.model2Date)
    //  other model properties razor code
     // also have a submit button for the form
    }

使用两个局部视图的索引视图(注意:此视图不是针对任何模型强类型的,它只是呈现_MyPartialView1和_MyPartialView2以及其模板属性中没有任何日期字段的其他部分视图)

@using(HTML.BeginForm("IndexPostToAction","MyController")){
@HTML.RenderAction("MyPartialView1Action")
@HTML.RenderAction("MyPartialView2Action")
// other partial views along with submit button
}

我的控制器代码如下:

public class MyController : Controller
{
   public ActionResult Index()
   {
      return View();
   }
   [ChildActionOnly]
   public ActionResult MyPartialView1Action()
   {
  return PartialView("~/Views/Shared/MyCustomFolder/_MyPartialView1"MyPartialViewModel1)
   }
[ChildActionOnly]
   public ActionResult MyPartialView2Action()
   {
  return PartialView("~/Views/Shared/MyCustomFolder/_MyPartialView2"MyPartialViewModel2)
   }
[HttpPost]
public ActionResult MyPartialView1PostToAction(MyPartialViewModel1 model)
  {
    // doing something
  }
[HttpPost]
public ActionResult MyPartialView2PostToAction(MyPartialViewModel2 model)
  {
    // doing something
  }
[HttpPost]
public ActionResult IndexPostToAction(MyPartialViewModel1 model1,MyPartialViewModel2 model2)
  {
    // doing something
  }
}

好的现在这是我面临的问题, IndexView 回发给我带来了我使用datepicker在视图中输入的确切 model1Date 。但是对于 model2Date IndexPostToAction(),我得到了“01/01/0001 00:00:00”,虽然我使用datepicker输入了不同的日期有效日期并获得“将datetime2数据类型转换为日期时间数据类型会导致超出范围的值“

以下是您可能感兴趣的更多信息:

  • IndexView 页面中,如果单击* _MyPartialView2 *的提交按钮,我在 MyPartialView2PostToAction()上获得正确的 mode2Date

  • 在SSMS中,我验证了属性 model1Date model2Date 属于 datetime 不可为空 < / p>

  • 使用jQuery 2.0 +

  • 我能够从 IndexView 中的* _MyPartialView2 *中选择日期 IndexPostToAction()

你能帮我理解一下,为什么我没有从 IndexPostToAction() IndexView 获得准确的 model2date 值但是 MyPartialView2PostToAction()

2 个答案:

答案 0 :(得分:0)

datetime2可以处理比datetime更广泛的日期,因此转换为较小的类型将返回该错误。我建议在数据库中将列更改为datetime2。它包括所有可能的日期。结帐Link。它显示了两种类型之间的区别。

答案 1 :(得分:0)

我做了以下更改,以便在 IndexPostToAction()中获取 model2Date 的确切值:

将* _MyPartialView1 *代码更改为:

@model MyProjectNameSpace.MyPartialViewModel1

   @HTML.TextBoxFor(model => model.model1Date)
//  other model properties razor code
 // I REMOVED THE SUBMIT BUTTON,BEGINFORM AND SCRIPT CODE AND ADDED IT TO INDEX VIEW

将* _MyPartialView2 *代码更改为:

@model MyProjectNameSpace.MyPartialViewModel2

 @HTML.TextBoxFor(model => model.model2Date)
    //  other model properties razor code
     // I REMOVED THE SUBMIT BUTTON,BEGINFORM AND SCRIPT CODE AND ADDED IT TO INDEX VIEW

将我的 IndexView 代码更改为:

<script type = "text/javascript">
  $('#model1Date,#model2Date').datepicker().bind('cut copy paste', function (e) {
            e.preventDefault(); 
        });
 </script>

@using(HTML.BeginForm("IndexPostToAction","MyController")){
@using(HTML.BeginForm("MyPartialView1PostToAction","MyController"))
{
// added _MyPartialView1 submit button here
@HTML.RenderAction("MyPartialView1Action")
}
@using(HTML.BeginForm("MyPartialView2PostToAction","MyController"))
{
// added _MyPartialView2 submit button here
@HTML.RenderAction("MyPartialView2Action")
}
// other partial views along with Index page submit button
}

如果有上述更改,我可以从 IndexView 页面获取准确输入的datepicker值。我的猜测是因为使用begin表单,各个部分视图中的脚本代码导致了一些无法解释的冲突。