Ajax没有在Jquery中使用序列化数据提交日期
本文关键字:序列化 数据 提交 日期 Jquery Ajax | 更新日期: 2023-09-27 18:27:06
我正在Ajax请求中使用Jquery在Controller中发送数据,所有字段都很好@Controller,但只有一个字段Date("AffectedDate")没有到来。
有人能告诉我哪里错了吗?
JQuery代码:
function saveData() {
var data = $("#editForm").serialize();
$.ajax({
type: 'GET',
url: '/Controller/SaveTaxRate/',
data: data,
success: function (result) {
alert('success');
},
error: function (jqXHR) {
alert('failure');
}
})
}
.Cs模型:
public class TaxRateModel
{
public int CountryId { get; set; }
public string CountryName { get; set; }
public double? Taxes { get; set; }
public float? AffectedTaxRate { get; set; }
public Nullable<System.DateTime> AffectedDate { get; set; }
}
控制器代码:
public bool SaveTaxRate(TaxRateModel taxRateModel)
{
// My Code here..
}
HTML代码:
<div class="row-fluid">
<div class="span12">
<div class="span3">
<p><strong>Country: </strong></p>
</div>
<div class="span5">
@Html.TextBoxFor(Model => Model.CountryName, new { @placeholder = "Change the Country Name" })
</div>
</div>
</div>
<div class="row-fluid">
<div class="span12">
<div class="span3">
<p><strong>Tax-Rate: </strong></p>
</div>
<div class="span5">
@Html.TextBoxFor(Model => Model.Taxes, new { @placeholder = "Chnage the Tax-Rate", @id = "TaxRate" })
</div>
</div>
</div>
<div class="row-fluid">
<div class="span12">
<div class="span3">
<p><strong>Affected Tax-Rate: </strong></p>
</div>
<div class="span5">
@Html.TextBoxFor(Model => Model.AffectedTaxRate, new { @placeholder = "Change the TaxRates" })
</div>
</div>
</div>
<div class="row-fluid">
<div class="span12">
<div class="span3">
<p><strong>Affected Date: </strong></p>
</div>
<div class="span5">
@Html.TextBoxFor(model => model.AffectedDate, "{0:dd/MM/yyyy}", new { @placeholder = "Change the Affected Date" })
</div>
</div>
</div>
试试这个。我也用过。
var data = new FormData($("#editForm").get(0));
$.ajax({
type: "POST",
url: '/Controller/SaveTaxRate/',
data: data,
dataType: "json",
contentType: false,
processData: false,
success: function (result) {
alert('success');
},
error: function (jqXHR) {
alert('failure');
}
})
而不是
var data = $("#editForm").serialize();
DateTime的解析是针对Formvalues的区域性解析。
查看您在CultureInfo.CurrentCulture 中的内容
以下是框架中的代码:
if (rawValue == null && request.Form != null)
{
culture = CultureInfo.CurrentCulture;
rawValue = request.Form.GetValues(name);
}
http://weblogs.asp.net/melvynharbour/mvc-modelbinder-and-localization
如果你需要改变这种行为,你可以通过提供自己的Modelbinder:来实现
ModelBinders.Binders.Add(typeof(DateTime), new MyModelBinder());
也在http://weblogs.asp.net/melvynharbour/mvc-modelbinder-and-localization.
编辑
由于请求是通过get发送的,并且参数是在querystring中传递的,因此使用不变区域性来解析日期。
该框架的这一部分使用
CultureInfo culture = CultureInfo.InvariantCulture;
if (request.QueryString != null)
{
rawValue = request.QueryString.GetValues(name);
}
我想你们应该试试星期五的答案和使用帖子。