I have 2 fields total quantity which updates on dropdown selection and a field to enter quantity which should be lesser or equal to the total quantity. Dropdown selection fetches the data via ajax call. I understand that dynamically updated values in the DOM needs to be forced to parse validate again. But it seems not be working . All other validations works as normal in the form. Similar questions in SO which I have referred to SO1 SO2 Code tried:
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.PurchaseOrderID, "PurchaseOrderID", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-4">
@Html.DropDownListFor(u => u.PurchaseOrderID, Model.PurchaseOrders, "--Select--")
@Html.ValidationMessageFor(model => model.PurchaseOrderID, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.POTotalQuantity, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-4">
@Html.EditorFor(model => model.POTotalQuantity, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Quantity, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-4">
@Html.EditorFor(model => model.Quantity, new { htmlAttributes = new { @class = "form-control", @type = "number", min = "1", max = Model.POTotalQuantity.ToString() } })
@Html.ValidationMessageFor(model => model.Quantity, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.ExpectedDeliveryDate, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-3">
@Html.EditorFor(model => model.ExpectedDeliveryDate, "{0:dd/mm/yyyy}", new { htmlAttributes = new { @class = "form-control datepicker" } })
@Html.ValidationMessageFor(model => model.ExpectedDeliveryDate, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-primary" />
</div>
</div>
</div>
}
Controller:
public JsonResult PopulatePODetails(int POID)
{
var purchaseOrder = db.PurchaseOrders.Find(POID);
var poTotalQty = db.PurchaseOrderLineItems.Where(p => p.PurchaseOrderID == purchaseOrder.ID).Sum(q => q.Quantity);
// Build an anonymous object
var data = new
{
POTotalQty = poTotalQty
};
return Json(data, JsonRequestBehavior.AllowGet);
}
JS:
$(document).ready(function () {
//Populating PO quantity on PO selection
$('#PurchaseOrderID').change(function () {
if (($(this).val() == "") || ($(this).val() == "0")) {
//need to clear textbox values
$('#POTotalQuantity').val('');
}
else {
var poId = $(this).val();
var url = '@Url.Action("PopulatePODetails", "DeliverySchedules")';
$.getJSON(url, { POID: poId }, function (response) {
$('#POTotalQuantity').val(response.POTotalQty);
$.validator.unobtrusive.parse($(response));
});
}
});
});
Model:
public class DeliveryScheduleVM
{
public int ID { get; set; }
//omitted other properties
public IEnumerable<SelectListItem> PurchaseOrders { get; set; }
[DisplayName("PO Total Qty")]
public int POTotalQuantity { get; set; }// this is used as the upper limit
[Required]
public int Quantity { get; set; }
}