我无法理解为什么日期始终为null。我的观点部分:
<td>
@Html.DisplayNameFor(x => x.Patient.DateOfBirth)
</td>
<td>
@Html.TextBoxFor(x => x.Patient.DateOfBirth, new { @id = "txtPatientDateOfBirth", @class = "normalText" })
</td>
我首先通过AJAX查找记录来加载日期:
success: function (data) {
if (data.DateOfBirth) {
$("#txtPatientDateOfBirth").datepicker("setDate", new Date(data.DateOfBirth));
}
if (data.ActiveDuty === "YES") {
$("#ddlPatientStatus").val("Active Duty");
}
$("#divPatientPick").dialog('close');
$("#txtPatientName").val(data.Name);
$("#txtPatientName").show()
}
然后我尝试用AJAX发布它:
$.ajax({
type: 'POST',
data: $("#formPreArrival").serializeArray(),
url: '@Url.Action("AddPreArrival")',
success: function (data) {
if (data.Errors === 'ERRORS') {
ErrorDialog("#MessageDialog", "#lblError", "There was an error detected in the submission of data. Please try again later. ", "Error");
return false;
}
else {
ErrorDialog("#MessageDialog", "#lblError", "Patient Pre-Arrival data has been successfully entered. ", "Record Added");
$("#formPreArrival")[0].reset();
$('body, html').animate({ scrollTop: $('form').offset().top }, 'slow');
return false;
}
}
但是模型字段的出生日期总是显示为空:
[HttpPost]
public ActionResult AddPreArrival(PreArrivalModel model)
{
string retval = "";
if (!ModelState.IsValid)
{
var errors = ModelState.Where(x => x.Value.Errors.Count > 0).Select(x => new { x.Key, x.Value.Errors }).ToArray();
model.Errors = "ERRORS";
retval = "ERRORS";
return Json(retval);
}
retval = Repository.AddPreArrival(model);
return Json(JsonConvert.SerializeObject(retval));
}
预先部分模型的部分:
[Display(Name = "Transfer Category:")]
public string CallTransferCategory { get; set; }
public PatientModel Patient { get; set; }
public TransferringFacilityModel TransferringFacility { get; set; }
public UserModel User { get; set; }
[Display(Name ="DEERS Verification: Is patient ELIGIBLE for care?")]
public bool DeersVerify { get; set; }
[Display(Name ="Comments:")]
public string Notes { get; set; }
public string Errors { get; set; }
[Display(Name = "DOB:")]
public DateTime? DateOfBirth { get; set; }
[Display(Name = "DOB:")]
public string DateOfBirthForDisplay
{
get
{
if (DateOfBirth.HasValue)
{
return DateOfBirth.Value.ToShortDateString();
}
return "";
}
}
患者模型部分:
[Display(Name = "Age:")]
public int Age
{
get
{
if (DateOfBirth.HasValue)
{
return DateTime.Today.Year - DateOfBirth.Value.Year;
}
return 0;
}
}
[Display(Name = "DOB:")]
public DateTime? DateOfBirth { get; set; }
[Display(Name = "DOB:")]
public string DateOfBirthForDisplay
{
get
{
if (DateOfBirth.HasValue)
{
return DateOfBirth.Value.ToShortDateString();
}
return "";
}
}