Hi comunidadmexi...,
You need to use HasValue to check Nullable DateTime.
Check this example. Now please take its reference and correct your code.
Model
public class PersonModel
{
public bool Truefalse { get; set; }
[Column(TypeName = "date")]
[Display(Name = "A date")]
[Required]
[DataType(DataType.Date)]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")]
public DateTime? A { get; set; }
[Required]
public string B { get; set; }
}
Controller
public class HomeController : Controller
{
// GET: Home
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Index(PersonModel person)
{
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
string query = "INSERT INTO Test (tA,tB) VALUES(@tA,@tB)";
using (SqlConnection con = new SqlConnection(conString))
{
SqlCommand command = new SqlCommand(query);
command.Connection = con;
if (person.A.HasValue)
{
command.Parameters.AddWithValue("@tA", person.A);
}
else
{
command.Parameters.AddWithValue("@tA", DBNull.Value);
}
if (!string.IsNullOrEmpty(person.B))
{
command.Parameters.AddWithValue("@tB", person.A);
}
else
{
command.Parameters.AddWithValue("@tB", DBNull.Value);
}
con.Open();
command.ExecuteNonQuery();
con.Close();
}
return View();
}
}
View
@model Date_Null_MVC.Models.PersonModel
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css" />
<script type="text/javascript">
$(function () {
EnableDisable($("[id*=Truefalse]"));
$("[id*=Truefalse]").click(function () {
EnableDisable($(this));
});
$("[id*=btnConfirm]").click(function () {
if ($('#B').val() == '' && $('#B').attr('disabled') == undefined) {
alert('B value is required.');
return false;
}
});
});
function EnableDisable(element) {
if ($(element).is(':checked')) {
$('#A').attr('disabled', 'disabled');
$('#B').removeAttr('disabled');
$('#B').prop("required", true);
} else {
$('#A').removeAttr('disabled');
$('#B').attr('disabled', 'disabled');
$('#B').prop("required", false);
}
}
</script>
</head>
<body>
@using (Html.BeginForm("Index", "Home", FormMethod.Post))
{
<div class="container">
<div> </div>
<div class="row">
<div class="col-md-12">
<div class="form-group" style="background-color: lightgoldenrodyellow; border:3px solid; font-weight:bold;">
<h5 style="font-weight: bold; text-indent: 20px;">
True/False @Html.CheckBoxFor(m => m.Truefalse, true)
</h5>
</div>
</div>
</div>
<div class="row">
<div class="col-md-3">
<div class="form-group">
@Html.LabelFor(m => m.A)
@Html.TextBoxFor(m => m.A, "{0:dd/MM/yyyy}", new { @Class = "Mytextarea2", placeholder = "A" })
@Html.ValidationMessageFor(m => m.A, "", new { @class = "text-danger" })
</div>
</div>
</div>
<div class="row">
<div class="col-md-3">
<div class="form-group">
@Html.LabelFor(m => m.B)
@Html.TextAreaFor(m => m.B, new { style = "width: 420px; height: 100px;", placeholder = "B" })
</div>
</div>
</div>
<div class="row">
<div class="col-md-3">
<div class="form-group">
<input id="btnConfirm" type="submit" value="Confirm" class="btn btn-default" />
</div>
</div>
</div>
</div>
}
</body>
</html>