Hi
I have below Index View
When i click on say Edit button Partial View is called in Modal.
Suppose i left Description blank then Error message should get displayed as given in Data Annotation.
IT does not give any error message
@using (Html.BeginForm("CreateEdit", "Customer", FormMethod.Post))
{
@Html.AntiForgeryToken()
<input type="hidden" id="hfAU" name="hfAU" value="A" />
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
@*<button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span>Close</button>*@
<h4 class="modal-title"></h4>
</div>
<div class="modal-body" id="myModalBody">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>
<button type="submit" class="btn btn-primary">Save Changes</button>
</div>
</div>
</div>
</div>
}
$('body').on('click', '[id*=btnEdit]', function () {
hfAU = "U";
var data = $(this).parents('tr').find('td');
var Id = data.eq(0).html();
$('#hfId').val(Id);
var url = "/Customer/CreateEdit?Id=" + Id;
$("#myModalBody").load(url, function () {
$(".modal-title").html("Update");
$("#myModal").modal("show");
});
});
public class Customer
{
public Customer()
{
CreatedOn = DateTime.Now;
}
[Key]
[Required(ErrorMessage = "Id cannot be Blank. Max length should be less than or equal to 20."), MaxLength(20)]
[Display(Name = "Id")]
[Remote("IsExist", "Customer", ErrorMessage = "Id already exists")]
public string Id { get; set; }
[Display(Name = "Description")]
[DataType(DataType.Text)]
[Required(ErrorMessage = "Customer Name cannot be Blank. Max length should be less than or equal to 50"), MaxLength(50)]
public string Description { get; set; }
[Display(Name = "Active")]
public bool IsActive { get; set; }
[DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}")]
public DateTime CreatedOn { get; set; }
}
@model MyApplication.Models.Customer
<div class="form-horizontal">
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.Id, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Id, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Id, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Description, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Description, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Description, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.IsActive, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
<div class="checkbox">
@Html.EditorFor(model => model.IsActive)
@Html.ValidationMessageFor(model => model.IsActive, "", new { @class = "text-danger" })
</div>
</div>
</div>
[HttpGet]
public ActionResult CreateEdit(string Id)
{
return PartialView("_CreateEditCustomer", dbCustomer.GetById(Id));
}
Thanks