Y G

Y G

  • NA
  • 236
  • 28.4k

ASP.NET MVC USING JQUERY AJAX CALL(CRUD)

Oct 11 2016 9:50 AM
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
 
 
 
namespace crudExcel1.Models                    //person class
{
[Table("person")]
public class person
{
[Key]
public int personId { get; set; }
public string personName { get; set; }
public string mobileNumber { get; set; }
public Nullable<byte> howManyAddress { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<personAddress> personAddresses { get; set; }
}
}
 
 
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
namespace crudExcel1.Models                      //person ADDRESS CLASS
{
[Table("personAddress")]
public class personAddress
{
[Key]
public int addressId { get; set; }
public Nullable<int> sequenceNo { get; set; }
public string country { get; set; }
public string city { get; set; }
public string addressStreet { get; set; }
[ForeignKey("personId")]
public Nullable<int> personId { get; set; }
public virtual person person { get; set; }
}
}
 
 
 
//DBCONTEXT  CLASS  
 
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.Entity;
namespace crudExcel1.Models
{
public class excelcrud1Entities:DbContext
{
public excelcrud1Entities()
: base("name=excelcrud1Entities")
{
}
public virtual DbSet<person> people { get; set; }
public virtual DbSet<personAddress> personAddresses { get; set; }
}
}
 
 
 
//INDEX.CSHTML
 
 
 
<table cellpadding="10" cellspacing="10">
<tr><td>Person Name</td><td>:</td><td><input type="text" id="personName" required /></td></tr>
<tr><td>Mobile Number</td><td>:</td><td><input type="text" id="mobileNo" maxlength="10" required /></td></tr>
</table>
How many Addresses ? :<select id="ddl" required>
<option value="Select">Select</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
<br />
<br />
<table id="tabEmp" border="1" cellpadding="10" cellspacing="10" width="100"></table>
<br />
<br />
<input type="button" id="btnSave" value="Save" onclick="Save()" disabled />
<script src="~/Scripts/jquery-1.10.2.min.js">
</script>
<script>
$(document).ready(function () {
$("#personName").focusout(function () {
debugger
if ($(this).val() == "") {
alert("please enter the name");
}
});
$("#mobileNo").keyup(function () {
debugger
$("#errorNumeric").hide();
var inputVal = $(this).val();
var numericVerify = /^(\d*[0-9])?$/;;
debugger;
if (!numericVerify.test(inputVal)) {
debugger;
$(this).after('<span id="errorNumeric">"please enter numeric values only!"</span>');
}
});
$("#mobileNo").focusout(function () {
if ($(this).val().length != 10) {
alert("please enter 10 digit valid mobile number");
}
});
if ($("#ddl").change(function () {
debugger
$("#tabEmp").empty();
$("#tabEmp").append("<tr><td>Sequence No</td><td>Country</td><td>City</td><td>Address/Street</td></tr>");
if ($("#ddl").val() == "Select") {
$("#tabEmp").empty();
$("#btnSave").prop('disabled', true)
}
else {
$("#btnSave").prop('disabled', false);
var NoofAddress = $("#ddl").val();
for (var i = 0; i < NoofAddress; i++) {
var markup = '<tr><td><input type="text" id=sequenceNo' + i + '></td><td><input type="text" id=country' + i + '></td><td><input type="text" id=city' + i + '></td><td><input type="text" id=address' + i + '></td></tr>';
$("#tabEmp").append(markup);
markup = "";
if ($("#ddl").val() == "Select") {
$("#tabEmp").append(markup);
}
}
};
}));
$("#btnSave").click(function () {
var array = [];
var noofaddresses = $("#ddl").val();
var pName = $("#personName").val();
var mNo = $("#mobileNo").val();
for (var i = 0; i < noofaddresses; i++) {
array.push({
"sequenceNo": $("#sequenceNo" + i).val(),
"country": $("#country" + i).val(),
"city": $("#city" + i).val(),
"addressStreet": $("#address" + i).val()
})
}
$.ajax({
method: "POST",
url: '@Url.Action("getJson")',
contentType: "application/json",
data: JSON.stringify({                                //CAN ANYONE PLEASE TELL ME HOW TO PASS                                                                         //THESE JSON VALUES TO DATABASE (SQL                                                                                                  //USING   //CONTROLLER)//FOR SAVING                                                                         //VALUES TO DATABASE
personName: pName,
mobileNumber: mNo,
personAddresses:array
}),
dataType: "json",
success: function (result) {
alert("success")
},
error: function (result) {
alert("data not inserted")
},
});
});
});
</script>
//CONTROLLER
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.Entity;
using System.Data.SqlClient;
using System.Web.Mvc;
using crudExcel1.Models;
namespace crudExcel1.Controllers
{
public class personController : Controller
{
excelcrud1Entities db = new excelcrud1Entities();
// GET: person
public ActionResult Index()
{
return View();
}
public JsonResult getJson()
{
return Json();
}
}
}

Answers (3)