ASP MVC中的日期时间问题 [英] Problem with datetime in ASP MVC
问题描述
您好,
我想在我的数据库中保存类型为DateTime的属性Date,但我总是遇到这个例外:
ex.Message =程序或功能'AddNewBestellung'需要参数'@Date',这是未提供的。
主持人:
Hello,
I want to save an attribute Date with type DateTime in my database but I got always this exception :
ex.Message = "Procedure or function 'AddNewBestellung' expects parameter '@Date', which was not supplied."
The Controller:
[HttpPost]
public ActionResult Save_Bestellung(Bestellung bs)
{
var errors = ModelState.Values.SelectMany(v => v.Errors);
try
{
if (ModelState.IsValid)
{
BestellungManagement bdb = new BestellungManagement();
if (bdb.AddBestellung(bs))
{
return View("~/Views/Bestellung/Index.cshtml");
ViewBag.Message = "Bestellung saved Successfully";
ModelState.Clear();
}
}
return RedirectToAction("Index");
}
catch
{
return JavaScript("alert('Order is Wrong ! Please verify your data !!')");
}
}
Class BestellungManagement:
Class BestellungManagement:
public bool AddBestellung(Bestellung bs)
{
try
{
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["DeliveryCon"].ToString()))
{
using (SqlCommand cmd = new SqlCommand("AddNewBestellung", con))
{
cmd.Parameters.AddWithValue("@Date", SqlDbType.DateTime2).Value = bs.Date;
con.Open();
int i = cmd.ExecuteNonQuery();
con.Close();
if (i >= 1)
return true;
else
return false;
}
}
}
catch(Exception ex)
{
string e = ex.Message;
return false;
}
}
模特 Bestellung.cs:
In model Bestellung.cs :
public DateTime Date { get; set; }
在数据库架构中:
In Database Schema :
[Date] DATETIME NOT NULL,
在视图中:
In the View :
<label>DATE : </label>
<input id="datepicker" name="Text" type="text" value="mm/dd/yyyy" onfocus="this.value = '';" onblur="if (this.value == '') {this.value = 'mm/dd/yyyy';}" required=""
datepicker功能:
the datepicker Function :
<script>
$(function()
{
$("#datepicker").datepicker({ minDate: 0 });
});
</script>
我尝试过:
我试图将日期类型从DateTime更改为datetime2,但问题仍未解决。我认为这个问题与JS函数有关,因为当我调试时,参数Date总是在这样的时间内获得Null值: Date = {10.02.2017 00:00:00} 当我设置今天的日期。
What I have tried:
I tried to change the type of Date from DateTime to datetime2 but the problem is not resolved. I think that the issue is with the JS Function because when I debug, the parameter Date got always Null Value for the time like this : Date = {10.02.2017 00:00:00} when I set Today's date.
推荐答案
(function()
{
(function() {
(#datepicker)。datepicker({ minDate:0});
});
< / script>
("#datepicker").datepicker({ minDate: 0 }); }); </script>
我尝试过:
我试图将日期类型从DateTime更改为datetime2,但问题仍未解决。我认为这个问题与JS函数有关,因为当我调试时,参数Date总是在这样的时间内获得Null值: Date = {10.02.2017 00:00:00} 当我设置今天的日期。
What I have tried:
I tried to change the type of Date from DateTime to datetime2 but the problem is not resolved. I think that the issue is with the JS Function because when I debug, the parameter Date got always Null Value for the time like this : Date = {10.02.2017 00:00:00} when I set Today's date.
这篇关于ASP MVC中的日期时间问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!