我正在对使用 C#创建的个人项目的表单进行一些验证测试,问题出在表单的后端,即使字段为空,它也可以发送数据
我清楚地表明,我已经在前面完成了一些Javascript函数的验证,另一个是我对使用DataAnnotations不感兴趣。
在下面的链接中我已将Form here制作成了
。应该在其中验证控制器功能的功能
/// <summary>
/// Function create new service
/// </summary>
/// <param name="name">Nombre del servicio</param>
/// <param name="content">Descripcion del servicio</param>
/// <returns></returns>
[HttpPost]
public JsonResult CreateService(string name,string content)
{
TK_CT_SERVICES service = new TK_CT_SERVICES();
service.NAME = name;
service.CONTENT = content;
var result = ServiceModel.CreateNewService(service);
return Json(result);
}
更新:
尝试以下方法,但没有结果
if (string.IsnullOrEmpty(name))
{
return Json(new
{
error = "The service could not be created,please enter the name."
});
}
service.NAME = name;
更新2:
代码Ajax
function createNewService() {
$("#submit").css("display","none");
$("#load").css("display","block");
const serviceName = document.getElementById("serviceName").value;
const serviceDescription = document.getElementById("serviceDescription").value;
$.ajax({
url: document.getElementById("createService").value,type: "POST",dataType: "json",data: {
name: serviceName,content: serviceDescription,},success: function(data) {
//alert("Se creo el servicio.");
//window.location = "..";
$("#message").text("Se han creado correctamente el servicio.");
$("#serviceResultModal").modal({
backdrop: "static",keyboard: false
});
$("#submit").css("display","block");
$("#load").css("display","none");
},error: function() {
alert("Ocurrió un error,porfavor inténtalo de nuevo.");
$("#submit").css("display","none");
}
});
}