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
@{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)
})
@ViewBag.Rowseffected1
<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>
<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) {
alert('success');
}),
error: (function (xhr) {
alert('error');
})
});
}
</script>
</body>
</html>
What I have tried:
Controller Code:
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
{
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";
}
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);
}
}
}