Jquery ajax在MVC中向数据库发送空值 [英] Jquery ajax is sending null value to database in MVC
本文介绍了Jquery ajax在MVC中向数据库发送空值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正处于jquery ajax的学习阶段,我想向数据库插入值,但它正在向数据库发送空值。请指导我。
谢谢
我尝试了什么:
这是我的代码。
< br >
< hr >
< div class = form-group >
< br >
< br >
< br >
< br >
< span class =code-keyword>< br >
< br >
< br >
< br >
< / div > ;
$( docu准备好)。 function (){
$( #insertForm)。validate({
rules:{
' textt':{
required: true ,
minlength: 5 ,
maxlength: 8
},
' text1':{
required: true ,
minlength: 5 ,
maxlength: 8
},
' text2':{
required: true ,
email: true
},
' text3': {
required: true ,
minlength: 5 ,
maxlength : 8
}
},
条消息:{
' textt':{
required: 输入名称,
minlength: 最小长度5 ,
maxlength: 最大长度8
},
< span class =code-string>' text1':{
required: 输入用户名,
minlength: 最小长度5,
maxlength: 最大长度8
},
' text2':{
required:< span class =code-string> 需要有效电子邮件,
电子邮件: 请输入有效的电子邮件abc@gmail.com
},
' text3':{
required: Passoword是必需的,
minlength:< span class =code-string> 最小密码长度5,
maxlength: 最大密码长度8
}
},
submitHandler:功能(表单){
form.submit();
}
});
$( #btn1)。click( function (){
var isvalid = $( #insertForm)。valid();
if (无效){
Employee_Ajax();
}
else {
bootbox.alert ( 未输入数据);
}
});
function Employee_Ajax(){
var employee = {
textt:$( txt1)。val(),
text1:$( txt2)。val(),
text2:$ ( txt3)。val(),
text3:$( txt4)。val()
}
$ .ajax({
url: @ Url.Action( EmployeesData ,主页 ) ,
数据: JSON .stringify( employees,员工),
类型: POST,
contentType: application / json; charset = utf-8,
dataType: JSON,
成功:功能(数据){
if (data!= ){
bootbox.alert( 数据成功保存!!!!) ;
}
else {
bootbox.alert( 数据未保存!ERROR);
}
},
错误:功能(失败){
控制台跨度> .log中(失败);
}
});
}
});
解决方案
( document )。ready( function (){
( #insertForm)。validate({
rules:{
' textt':{
required: true ,
minlength: 5 ,
maxlength: 8
},
' text1':{
required: true ,
minlength: 5 ,
maxlength: 8
},
' text2':{
required: true ,
email: true
},
' text3':{
必需: true ,
minlength: 5 ,
maxlength: 8
}
},
条消息:{
' textt':{
required: 进入Nam e,
minlength: 最小长度5,
maxlength: 最大长度8
},
' text1':{
required: 输入用户名,
minlength: 最小长度5,
maxlength: 最大长度8
},
' text2':{
required: 需要有效电子邮件,
电子邮件: 请输入有效的电子邮件abc@gmail.com
},
' text3':{
required: Passoword是必需的,
minlength: 最小密码长度5,
maxlength: 最大密码长度8
}
},
submitHandler: function (form){
form.submit();
}
});
( # btn1)。click( function (){
var isvalid =
i am at learning phase of jquery ajax and i want to insert values to database but it is sending null values to database. please guide me.
thanks
What I have tried:
here is my code.
<br>
<hr>
<div class="form-group">
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
</div>
$(document).ready(function () {
$("#insertForm").validate({
rules: {
'textt': {
required: true,
minlength: 5,
maxlength: 8
},
'text1': {
required: true,
minlength: 5,
maxlength: 8
},
'text2': {
required: true,
email: true
},
'text3': {
required: true,
minlength: 5,
maxlength: 8
}
},
messages: {
'textt': {
required: "Enter the Name",
minlength: "Minimum Length 5",
maxlength: "Maximum Length 8"
},
'text1': {
required: "Enter Username",
minlength: "Minimum Length 5",
maxlength: "Maximum Length 8"
},
'text2': {
required: "Valid Email Required",
email: "Please Enter valid email abc@gmail.com"
},
'text3': {
required: "Passoword is required",
minlength: "Minimum Length of Password 5",
maxlength: "Maximum Length of Password 8"
}
},
submitHandler: function (form) {
form.submit();
}
});
$("#btn1").click(function () {
var isvalid = $("#insertForm").valid();
if (isvalid) {
Employee_Ajax();
}
else {
bootbox.alert("Data is not entered");
}
});
function Employee_Ajax() {
var employee ={
textt: $("txt1").val(),
text1: $("txt2").val(),
text2: $("txt3").val(),
text3: $("txt4").val()
}
$.ajax({
url: "@Url.Action("EmployeesData", "Home")",
data: JSON.stringify("employees", employee),
type: "POST",
contentType: "application/json;charset=utf-8",
dataType: "JSON",
success: function (data) {
if (data != "") {
bootbox.alert("Data Saved Successfully!!!!");
}
else {
bootbox.alert("data not saved! ERROR");
}
},
error: function (fail) {
console.log(fail);
}
});
}
});
解决方案
(document).ready(function () {
("#insertForm").validate({ rules: { 'textt': { required: true, minlength: 5, maxlength: 8 }, 'text1': { required: true, minlength: 5, maxlength: 8 }, 'text2': { required: true, email: true }, 'text3': { required: true, minlength: 5, maxlength: 8 } }, messages: { 'textt': { required: "Enter the Name", minlength: "Minimum Length 5", maxlength: "Maximum Length 8" }, 'text1': { required: "Enter Username", minlength: "Minimum Length 5", maxlength: "Maximum Length 8" }, 'text2': { required: "Valid Email Required", email: "Please Enter valid email abc@gmail.com" }, 'text3': { required: "Passoword is required", minlength: "Minimum Length of Password 5", maxlength: "Maximum Length of Password 8" } }, submitHandler: function (form) { form.submit(); } });
("#btn1").click(function () { var isvalid =
这篇关于Jquery ajax在MVC中向数据库发送空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文