Я просто для того, чтобы остановить выполнение кода на серверной стороне, если входные данные пусты. Любая помощь ценится. < /P>
Это мой cshtml: < /p>
@page
@model Products.Pages.IndexModel
@{
var product = Model.product;
}
@Html.AntiForgeryToken()
product-id:
@section Scripts {
@{
await Html.RenderPartialAsync("_ValidationScriptsPartial");
}
$(document).ready(function () {
$("#productsContainer").on("click", ".openProductModal", function () {
$(".ui-dialog-titlebar").hide();
// Capture selected product data
var productId = $(this).data("product-id");
var imageurl = $(this).data("imageurl");
$('#imageurl').attr('src',imageurl);
$("#product-id").text(productId);
$("#modal_dialog").dialog({
buttons: {
Confirm: function () {
// var elem = document.getElementsByTagName("input")[0];
// if(elem.hasAttribute("required")){ /* This line breaks script */
// alert("Name and email required.");
// return;
// }
var form = $(this);
var validated = true;
$('input[type="text"]',this).each(function(){
if($(this).val().length < 1){
validated = false;
alert("Name and email required.");
return;
/* this works because it checks the length of the input, but that defeats the purpose of the REQUIRED attribute.
Plus, even when it hits "return", it still call server-side method */
}
});
$.ajax({
type: "POST",
url: "/Index?handler=Send",
dataType: "json",
beforeSend: function (xhr) {
xhr.setRequestHeader("XSRF-TOKEN",
$('input:hidden[name="__RequestVerificationToken"]').val());
},
success: function (response) {
$("#modal_dialog").dialog('close');
}
});
},
Close: function () {
$(this).dialog('close');
}
},
modal: true,
width: 400
});
});
});
}
< /code>
Это мой cshtml.cs < /p>
namespace Products.Pages
{
public class Customer
{
[Required(AllowEmptyStrings = false)]
public string? FullName { get; set; }
[Required(AllowEmptyStrings = false)]
public string? EmailAddress { get; set; }
}
public class IndexModel : PageModel
{
public List availableProducts = new List();
public Product product = new Product();
public void OnGet()
{
Product products = new Product();
availableProducts = products.GetProducts();
product = availableProducts.First();
}
public ActionResult OnPostSend()
{
return new JsonResult("true");
}
}
}
Подробнее здесь: https://stackoverflow.com/questions/796 ... -ajax-call
Мобильная версия