In Web Config
<connectionStrings>
<add name="DOTNETEntities" providerName="System.Data.SqlClient" connectionString="Data Source=DHIRENDRAKUMAR;initial catalog=DOTNET;user id=sa;password=P@ssw0rd" />
</connectionStrings>
Add Class in Model public class DotNetDBDataContext : DbContext { public DotNetDBDataContext() : base("name=DOTNETEntities") { } } public class EmployeeGames { public int EmpGameId { get; set; } public string EmpName { get; set; } public string Games { get; set; } } Add Controller public class EmpGamesController : Controller { DotNetDBDataContext EmpGameContext = new DotNetDBDataContext(); List
<EmployeeGames> EmpGame = null; // GET: EmpGames public ActionResult Index() { EmpGame = EmpGameContext.Database.SqlQuery
<EmployeeGames>("SP_GetEmployeeGames").ToList(); ViewBag.EmpGamesData = EmpGame; return View(); } public ActionResult AddGames(int Id) { EmployeeGames Empobj = new EmployeeGames(); Empobj = EmpGameContext.Database.SqlQuery
<EmployeeGames>("SP_GetParticularEmployeeGames @EmpGameId", new SqlParameter("EmpGameId", Id)).FirstOrDefault(); ViewBag.EmpGamesData = Empobj; return View(); } [HttpPost] public ActionResult UpdateGames(EmployeeGames EmpGames) { bool isSuccess=false; int result; result = EmpGameContext.Database.SqlQuery
<int>("SP_UpdateEmployeeGames @EmpGameId,@Games", new SqlParameter("EmpGameId", EmpGames.EmpGameId), new SqlParameter("Games", EmpGames.Games)).FirstOrDefault
<int>(); if (result > 0) isSuccess = true; return new JsonResult { Data = isSuccess, JsonRequestBehavior = JsonRequestBehavior.AllowGet }; } } Add View @{ Layout = null; }
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
<script type="text/javascript">
function AddMoreGame(EmpGameId)
{
//var url [email protected]("AddGames", "EmpGames") +"?Id=" + EmpGameId;
window.location.href = "AddGames?Id=" + EmpGameId;
// window.location.href = url;
}
</script>
</head>
<body>
<div>
<table>
<tr>
<td headers="Employee Name"></td>
<td headers=" Games Played"></td>
<td headers="Action"></td>
</tr>
@foreach (var Item in ViewBag.EmpGamesData)
{
<tr>
<td>@Item.EmpName</td>
<td>@Item.Games</td>
<td><a href="#" onclick="AddMoreGame(@Item.EmpGameId)">Add</a></td>
</tr>
}
</table>
</div>
</body>
</html>
Add AddGames View @model ManualEmpGames.Models.EmployeeGames @{ Layout = null; } @{ ManualEmpGames.Models.EmployeeGames obj = new ManualEmpGames.Models.EmployeeGames(); if (ViewBag.EmpGamesData != null) { obj = ViewBag.EmpGamesData; } }
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>AddGames</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.9.1/jquery-ui.min.js"></script>
<script>
$(document).ready(function()
{
var iCnt = 0;
// CREATE A "DIV" ELEMENT AND DESIGN IT USING JQUERY ".css()" CLASS.
var container = $(document.createElement('div')).css({
padding: '5px',
margin: '20px',
width: '170px',
border: '1px dashed',
borderTopColor: '#999',
borderBottomColor: '#999',
borderLeftColor: '#999',
borderRightColor: '#999'
});
$('#btAdd').click(function()
{
for (var iCnt = 0; iCnt < parseInt(document.getElementById('txtTotalGame').value); iCnt++)
{
// ADD TEXTBOX.
$(container).append('<input type=text class="input" id=tb' + iCnt + ' value="" placeholder="Enter Game Name"/><input type="button" id="btRemove" value="Remove Element" class="bt" />');
// SHOW SUBMIT BUTTON IF ATLEAST "1" ELEMENT HAS BEEN CREATED.
if (iCnt == 1) {
var divSubmit = $(document.createElement('div'));
$(divSubmit).append('<input type=button class="bt" onclick="GetTextValue()" id=btSubmit value=Submit />');
}
// ADD BOTH THE DIV ELEMENTS TO THE "main" CONTAINER.
$('#main').after(container, divSubmit);
}
// AFTER REACHING THE SPECIFIED LIMIT, DISABLE THE "ADD" BUTTON.
// (20 IS THE LIMIT WE HAVE SET)
});
// REMOVE ELEMENTS ONE PER CLICK.
$('#btRemove').click(function()
{
if (iCnt != 0)
{
$('#tb' + iCnt).remove();
iCnt = iCnt - 1;
}
if (iCnt == 0)
{
$(container)
.empty()
.remove();
$('#btSubmit').remove();
$('#btAdd')
.removeAttr('disabled')
.attr('class', 'bt')
}
});
});
// PICK THE VALUES FROM EACH TEXTBOX WHEN "SUBMIT" BUTTON IS CLICKED.
var divValue, values = '';
function GetTextValue()
{
$(divValue)
.empty()
.remove();
values = '';
$('.input').each(function()
{
divValue = values += this.value + ','
});
debugger;
var Employee =
{
EmpGameId: '@obj.EmpGameId',
Games: '@obj.Games' + divValue
}
$.ajax({
url: '@Url.Action("UpdateGames", "EmpGames")',
type: 'POST',
data:
{
EmpGames: Employee
},
dataType: 'json',
cache: false,
success: function(responce)
{
if (responce != null && responce == true)
{
var url = "Index";
window.location.href = url;
}
}
});
$(divValue).append('<p><b>Your selected values</b></p>' + values);
$('body').append(divValue);
}
</script>
</head>
<body>
@{
<div>
<table style="border:double">
<tr>
<td>@obj.EmpName</td>
<td></td>
<td></td>
</tr>
</table>
<div id="main">
<input type="text" id="txtTotalGame" value="0" placeholder="Enter How Many Games" />
<input type="button" id="btAdd" value="Add Element" class="bt" />
</div>
</div>
}
</body>
</html>