Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Can any one help me out please???

here i am using web grid and partial views.....

actually when i click on save button in partial view the data should go to the Controller using ajax call and rowseffeted should come back to ajax call

At the same time data in web grid should update using model.......

i am not getting it......

here is my Code

In View

@using BussinessModel

@{
Student ObjStu = new Student();
}



@{var grid = new WebGrid(Model, canPage: true, rowsPerPage: 5, selectionFieldName: "selectedRow", ajaxUpdateContainerId: "gridContent");
grid.Pager(WebGridPagerModes.NextPrevious);}
@grid.GetHtml(tableStyle: "table",
headerStyle: "head",
alternatingRowStyle: "altRow",
selectedRowStyle: "selectRow",
htmlAttributes: new { id = "grid" },
fillEmptyRows: false,
mode: WebGridPagerModes.All,
firstText: "<< First",
previousText: "< Prev",
nextText: "Next >",
lastText: "Last >>",
columns: new[] {
grid.Column("RollNo",header: "RollNo", canSort: false),

grid.Column(header: "Name",format: @ @item.Name @Html.TextBox("Name_" + (string)item.RollNo, (string)item.Name, new { @style = "display:none" })),

grid.Column(header: "Branch",format: @ @item.Branch @Html.TextBox("Branch_" + (string)item.RollNo, (string)item.Branch, new { @style = "display:none" })),

grid.Column(header: "FeeRemaining",format: @ @item.FeeRemaining @Html.TextBox("FeeRemaining_" + (string)item.RollNo, (string)item.FeeRemaining, new { @style = "display:none" })),

grid.Column(header: "Action",format:@<text> EditUpdateCancelDelete)
})

Razor
@ViewBag.Rowseffected1


HTML
<table width="100%">
        <tr>
            <td width="30%">
                <table>
                    <tr>
                        <td>
                            <br />
                            <br />
                            <br />
                            <a id="Save" href="#">Save</a><br />
                            <br />
                            <br />
                            <br />
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <a id="Update" href="#">Update</a><br />
                            <br />
                            <br />
                            <br />
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <a id="Delete" href="#">Delete</a><br />
                            <br />
                            <br />
                            <br />
                        </td>
                    </tr>
                </table>
            </td>
            <td width="70%">
                <div id="SavePartialView" style="display:none">
                    @Html.Partial("~/Views/_SavePartialView.cshtml")
                </div>
                <div id="UpdatePartialView" style="display:none">
                    @Html.Partial("~/Views/_UpdatePartialView.cshtml")
                </div>
                <div id="DeletePartialView" style="display:none">
                    @Html.Partial("~/Views/_DeletePartialView.cshtml")
                </div>
            </td>
            <td></td>
        </tr>

    </table>
    <script type="text/javascript">
        $("#Save").click(function () {
            $("#SavePartialView").show();
            $("#UpdatePartialView").hide();
            $("#DeletePartialView").hide();
        });

        $("#Update").click(function () {
            $("#SavePartialView").hide();
            $("#UpdatePartialView").show();
            $("#DeletePartialView").hide();
        });
        $("#Delete").click(function () {
            $("#SavePartialView").hide();
            $("#UpdatePartialView").hide();
            $("#DeletePartialView").show();
        });
    </script>


In Partial view Code:

HTML
<html>
<head>
    <title></title>
    <script src="~/Scripts/jquery-2.2.1.js"></script>
    <script src="~/Scripts/jquery-2.2.1.min.js"></script>
</head>
<body>
    <div>




        @*design for the Save Partial View*@



        <table>
            <tr>
                <td>Roll No         :</td>
                <td>@Html.TextBox("RollNo")</td>
            </tr>
            <tr>
                <td>Name            :</td>
                <td>@Html.TextBox("Name")</td>
            </tr>
            <tr>
                <td>Branch          :</td>
                <td>@Html.TextBox("Branch")</td>
            </tr>
            <tr>
                <td>Fee Remaining   :</td>
                <td>@Html.TextBox("FeeRemaining")</td>
            </tr>
            <tr>
                <td></td>
                <td><input type="button" value="Save" id="BtnSave" onclick="Save()"/></td>
            </tr>
            <tr>
                <td></td>
            </tr>
        </table>

    </div>

    @*Ajax Call For Sending the data from the view to contoller*@



    <script type="text/javascript">
        function Save()
        {
            var RollNo = $("#RollNo").val();
            var Name = $("#Name").val();
            var Branch = $("#Branch").val();
            var FeeRemaining = $("#FeeRemaining").val();
            debugger;
            $.ajax({
                url:'/Index/SaveToDB/',
                type: "POST",
                dataType: 'html',
                contentType: 'application/json; charset=utf-8',
                data:JSON.stringify({
                    RollNo: RollNo, 
                    Name: Name,
                    Branch: Branch,
                    FeeRemaining: FeeRemaining
                }),
                success: (function (result) {
                    //      debugger;
                    alert('success');
                }),
                error: (function (xhr) {
                    //    debugger;
                    alert('error');
                })
            });
        }
    </script>
</body>

</html>


What I have tried:

Controller Code:

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Repository;
using BussinessModel;

namespace CurdProjectLevel.Controllers
{
    public class IndexController : Controller
    {
        //
        // GET: /Index/
        Student ObjStudent = new Student();
        public ActionResult Index()
        {
            RetriveRepositoryInfo Obj = new RetriveRepositoryInfo();
            return View("~/Views/Index/Index.cshtml",Obj.GetDB());
        }
        public ActionResult Delete( int id)
        {
            RetriveRepositoryInfo Obj = new RetriveRepositoryInfo();
            int RowsEffected = 0;
            List<Student> ObjSt = new List<Student>();
            
            try
            {
                
                 RowsEffected = Obj.Delete(id);
                if (RowsEffected == 1)
                {
                    ViewBag.Rowseffected1 = "Row Delected";
                    //ObjSt = Obj.GetDB();
                    //return View("~/Views/Index/Index.cshtml", ObjSt);
                }
                else
                {
                    ViewBag.Rowseffected1 = "Row Cannot Delete/Row Not Found";
                }
                
            }
            catch
            {
                throw;
            }
            return RedirectToAction("Index",RowsEffected);
        }
        public ActionResult SaveToDB(Student ObjStudent)
        {
            RetriveRepositoryInfo Obj = new RetriveRepositoryInfo();
            int RowsEffected = Obj.SaveToDB(ObjStudent);
            if (RowsEffected == 1)
            {
                ViewBag.Rowseffected1 = RowsEffected;
            }
            else
            {
                ViewBag.Rowseffected1 = "Row Cannot Save";
            }
            
            return View("~/Views/Index/Index.cshtml",RowsEffected);
             
        }
        

    }
}
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900