Обратные вызовы jqXHR.success (), jqXHR.error () и jqXHR.complete () будут устаревшими в jQuery 1.8. Чтобы подготовить ваш код для их возможного удаления, используйте взамен jqXHR.done (), jqXHR.fail () и jqXHR.always ().
Используйте ModelState Validation для проверки вашей модели
Отправьте форму, используя метод OnClick, чтобы избежать e.reeventDefault ().
Ниже приведен полный пример в соответствии с вашим требованием (пожалуйста, прочитайте весь встроенный комментарий) -
Модель: -
public class RunViewModel
{
[Required]
[Display(Name = "Instance name : ")]
public string InstanceName { get; set; }
[Required]
[Display(Name = "Database name : ")]
public string DatabaseName { get; set; }
[Required]
[Display(Name = "SQL folder path : ")]
public string FolderPath { get; set; }
/// <summary>
/// Liste ordonnée des fichiers, regroupés par version, à lancer sur la base de données suivant la version actuelle de la base de données.
/// </summary>
public IOrderedEnumerable<IGrouping<Version, string>> SqlFilesGroupedByVersion { get; set; }
}
public class ValidationError
{
public string PropertyName { get; set; }
public string[] ErrorList { get; set; }
}
Контроллер: -
public class RunController : Controller
{
[HttpGet]
// GET: Run/Save
public ActionResult Save()
{
var model = new RunViewModel();
return View(model);
}
// POST: Run/Save
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Save(RunViewModel model)
{
var status = "Failed";
var massage = "";
try
{
//If modelstate validation need
if (!ModelState.IsValid)
{
return Json(new
{
status,
massage,
model,
errorList = GetModelStateErrors(ModelState).ToList()
});
}
else
{
//Write your code
status = "success";
massage = "Database has been updated !";
}
}
catch (Exception ex)
{
massage = ex.Message;
}
return Json(new
{
status,
massage,
});
}
public IEnumerable<ValidationError> GetModelStateErrors(ModelStateDictionary modelState)
{
var errors = (from m in modelState
where m.Value.Errors.Count() > 0
select
new ValidationError
{
PropertyName = m.Key,
ErrorList = (from msg in m.Value.Errors
select msg.ErrorMessage).ToArray()
})
.AsEnumerable();
return errors;
}
}
Save.cs html: -
@model RunViewModel
@{
ViewData["Title"] = "Run";
}
<h1 class="text-info">@ViewData["Title"]</h1>
<style>
.field-validation-valid{
color:#FF0000;
}
</style>
<form id="run-form">
@Html.AntiForgeryToken()
<div class="form-group">
<label asp-for="InstanceName"></label>
<input asp-for="InstanceName" class="form-control" placeholder="Enter an instance name here..." />
<span asp-validation-for="InstanceName"></span>
</div>
<div class="form-group">
<label asp-for="DatabaseName"></label>
<input asp-for="DatabaseName" class="form-control" placeholder="Enter a database name here..." />
<span asp-validation-for="DatabaseName"></span>
</div>
<div class="form-group">
<label asp-for="FolderPath"></label>
<input asp-for="FolderPath" class="form-control" />
<span asp-validation-for="FolderPath"></span>
</div>
<div class="col-3 offset-4">
<button type="button" id="btnSubmin" class="btn btn-primary">Run!</button>
</div>
</form>
@section Scripts {
<script>
$("#btnSubmin").on("click", function () {
var valToken = $('input:hidden[name="__RequestVerificationToken"]').val();
var model = getFormData('run-form');
$.ajax({
type: 'POST',
url: '/Run/Save',
contentType: 'application/x-www-form-urlencoded',//set a Content-Type of application/json; charset=UTF-8 When Call API,application/x-www-form-urlencoded is the default contentType
headers: { RequestVerificationToken: valToken },
dataType: 'json',
data: model,//you need to pass json string
}).done(function (data) {
if (data.status === "success") {
alert("Database has been updated !"); //TODO : Mettre un div vert avec un message à la place.
} else {
displayValidationErrors(data.errorList);
}
}).fail(function (data) {
alert("An error occured 2!"); //TODO : Mettre un div rouge avec un message à la place.
});
/*
The jqXHR.success(), jqXHR.error(), and jqXHR.complete() callbacks will be deprecated in jQuery 1.8. To prepare your code for their eventual removal, use jqXHR.done(), jqXHR.fail(), and jqXHR.always() instead.
*/
});
//Show Validation Message
function displayValidationErrors(errors) {
$.each(errors, function (idx, validationError) {
$("span[data-valmsg-for='" + validationError.propertyName + "']").text(validationError.errorList[0]);// if not working then captilized ex: errorList to ErrorList,propertyName to PropertyName
});
}
//Convert to Json From InputForm
function getFormData(formId) {
var $form = $("#" + formId);
var unindexed_array = $form.serializeArray();
var indexed_array = {};
$.map(unindexed_array, function (n, i) {
indexed_array[n['name']] = n['value'];
});
return indexed_array;
}
</script>
}
(проверено)