'how to have data validation when the object is sent by jquery and the page is not strong typed
I have a form in modal form , which gets data from user and the make an Object in Jquery and pass it to controller
<div class="modal-body" id="myModalBody">
<div class="row form-group">
<div class="col-6 d-inline-block">
<label>Province</label>
<select name="Province" asp-items="@ViewBag.Province" id="ddlProvinces" class="form-control"></select>
@Html.ValidationMessage("ddlProvinces","Select Province")
</div>
<div class="col-6 d-inline-block">
<label>City</label>
<select id="ddlCities" class="form-control"></select>
@Html.ValidationMessage("ddlCities","Select City")
</div>
</div>
<div class="row form-group">
<div class="col-12 d-inline-block">
<label>Address</label>
<input type="text" id="address" class="form-control" value="" />
@Html.ValidationMessage("number","Enter Number")
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" id="save" class="btn btn-primary">Save</button>
</div>
this is juquery
$('#save').click(function () {
var postData = {
ProvinceId: $('#ddlProvinces').val().trim(),
CityId: $('#ddlCities').val().trim(),
Province: $('#ddlProvinces option:selected').text(),
City: $('#ddlCities option:selected').text(),
Address: $('#address').val().trim()
}
$.ajax({
type: "POST",
url: '/cart/SaveAddress',
data: postData,
success: function (data) {
if (data) {
$('#myModal').modal('hide');
}
}
})
})
is it possible to add data annotation to this form to check the model validity?I mean when the page is not strong typed and is not binded to a model like (@model userAddress). if yes would you please edit the code to see how it is?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
