You must Sign In to post a response.
  • Category: ASP.Net MVC

    Working with json not working in mvc 4

    index.cshtml
    __________________
    <html>
    <head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
    <script src="~/Scripts/jquery-1.8.2.js"></script>
    <script type="text/javascript">
    $ (DOCUMENT).ready(function ()
    {

    // code for insert data in Table *****************************
    $("#btnSave").click(function (w) {
    debugger;
    var name = $("#txtName").val();
    // var gender = $("input.rb:checked").val();

    var address = $("#txtAddress").val();
    var department = $("#txtDept").val();
    //var city = $("#DdlCity").val();
    // convert your from in Json......................

    console.log($('form').serialize());
    $.post("Employee/Save_Data", { Name: name, Address: address, Department: department }, function (data) { $("#result").html(data) });
    w.preventDefault();
    });



    //******Code for Update data with SrNo *****************************
    $("#btnUpdate").click(function (w) {
    var name = $("#txtName").val();
    // var gender = $("input.rb:checked").val();

    var address = $("#txtAddress").val();
    var department = $("#txtDept").val();
    $.post("Employee/Update", { Name: name, Address: address, Department: department }, function (data) { $("#result").html(data) });
    w.preventDefault();
    });
    //******Code for Delete data with SrNo ************************
    $("#btnDelete").click(function (w) {
    debugger;
    var name = $("#txtName").val();
    $.post("Employee/Delete", { Name: name }, function (data) { $("#result").html(data) });
    w.preventDefault();
    });
    });
    </script>
    </head>
    <body>
    <div id="main">
    <form method="post" enctype="multipart/form-data">
    <div class="signup" id="d1">
    @using (Html.BeginForm())
    {
    <table width="100%">

    <tr>
    <td>Name:</td>
    <td>@Html.TextBox("txtName")</td>
    </tr>

    <tr>
    <td>Address :</td>
    <td>@Html.TextBox("txtAddress")</td>
    </tr>
    @*Code for create TextBox with Password Field in Mvc*@
    <tr>
    <td>Department :</td>
    <td>@Html.TextBox("txtDept")</td>
    </tr>


    <tr>
    <td colspan="2">
    <input type="submit" name="submit" id="btnSave" value="Save" />
    <input type="submit" name="submit" id="btnUpdate" value="Update" />
    <input type="submit" name="submit" id="btnDelete" value="Delete" />
    <input type="button" name="submit" id="btnSearch" value="Search" /></td>
    </tr>
    <tr>
    <td colspan="2">
    <div id="result" style="color: red;">
    </div>
    </td>
    </tr>
    </table>
    }
    </div></form>
    </div>
    </body>
    </html>
    _____________________________________________________________________________________________________
    in controller
    _____________________________________________________________________________________________________


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using System.Data;
    using MvcApplication7.Models;

    namespace MvcApplication7.Controllers
    {
    public class EmployeeController : Controller
    {
    //
    // GET: /Employee/
    dinDBEntities2 ddentities = new dinDBEntities2();
    public ActionResult Index()
    {
    return View();
    }
    [HttpPost]
    public JsonResult Save_Data(string name, string address, string department)
    {
    try
    {
    Employee us = new Employee
    {

    Name = name,
    Address = address,
    Department = department,

    };
    ddentities.Employees.Add(us);
    ddentities.SaveChanges();
    return Json("Regitration Successfully", JsonRequestBehavior.AllowGet);
    }
    catch
    {
    return Json("Some problem occur...try again", JsonRequestBehavior.AllowGet);
    }
    }
    //Code for display data from table...................................
    //Code for update data from table...................................

    [HttpPost]
    public string Update(string name, string address, string department)
    {
    string Name=name;
    var v = (from m in ddentities.Employees where m.Name == name select m).FirstOrDefault();
    if (v != null)
    {

    v.Address=address;
    v.Department=department;


    }
    ddentities.SaveChanges();
    return "Update with Name" + Name;
    }
    //Code for Delete data from table....
    [HttpPost]
    public string Delete(string Name)
    {
    string name = Name;
    var v = (from m in ddentities.Employees where m.Name == name select m).FirstOrDefault();
    if(v!=null)
    {
    ddentities.Employees.Remove(v);
    ddentities.SaveChanges();
    }
    return "Delete successfully with Name"+Name;
    }
    }
    }
  • #762444
    Hi Dineshkumar,

    I am not sure what kind of error you are getting while running the application. You haven't mentioned any error reason so it's hard to explain with your current situation.

    When I checked your code, it seems your parameter of the services is may wrong or it may be you are URL problem or your response of the data will not parse into JSON. There will be many cause of the error. Kindly, post the exact issue you are facing.

    Better, you can use simple "ajax" call looks like similar to your post method, but we can define lot of additional parameter. Here's the sample code for that request,



    var tSaleInfo =
    {
    iStoreID: parseInt(tStoreID),
    iTreatMentID: parseInt(tTreatMentID)
    };

    jQuery.support.cors = true;
    $.ajax({
    url: 'SERVICE URL',
    type: 'POST', //Method of post
    contentType: "application/json; charset=utf-8", // Content type
    dataType: 'json', // What kind of data you are trying to pass to your service
    data: JSON.stringify(tSaleInfo),
    success: function (tData) {
    //Get your response here and process the data
    },
    error: function (x, y, z) {
    //Put some alert so that you can see what kind of error is displaying
    }
    });


    Thanks,
    Damu

    Regards,
    V.M. Damodharan
    "Your talent will be worthless, when you have fear and tension."


Sign In to post your comments