I have created JQuery Datatable to load the multiple records after page loads.
During edit the row after click or keypressed the specified row, the specified rows values is initiated into new edit view through parameter id value.
But not shown the id value on controller parameterized function.
The value of id is display in view during debugging mode,but when call the controller function the passing id value is not display in controller function.
why???? i do not know..
Can any body help for above problems????
PLease........
What I have tried:
My cshtml code is below:
$(document).ready(function () {
$.noConflict();
$.ajax({
url: 'Unit/GetUnit',
method: 'post',
dataType: 'json',
success: function (data) {
$('#datatable').DataTable({
select: {
},
keys: true,
data: data,
paging: false,
searching: false,
ordering: false,
scrollY: 200,
columns: [
{
data: 'UnitID',
width: '10px'
},
{
data: 'Name',
width: '300px',
}, },
],
columnDefs: [
{ className: 'dt-body-center', targets: [0] },
{ visible: false, targets: [0] },
{ className: 'dt-body-left', targets: [1] }
]
});
}
});
KeyEventHandler();
});
function KeyEventHandler() {
$('#datatable').on('key', function (e, datatable, key, cell, originalEvent) {
if (key == 13) {
var tbl = $('#datatable').DataTable();
var pID = tbl.row('tr').data()['UnitID'];
EditRow(pID);
}
});
}
function EditRow(id) {
if (confirm("Are You sure to edit this record..?")) {
$.ajax({
type: 'POST',
url: '@Url.Action("Edit", "Unit")',
data: { id: id },
datatype: 'json',
success: function (response) {
alert("Successfully updated.");
},
error: function (msg) {
alert(msg.responseText);
}
});
}
}
My controller code is below:
public ActionResult Edit(Int16? uID = null)
{
string cs = ConfigurationManager.ConnectionStrings["MainCon"].ConnectionString;
List<modUnit> mUnits = new List<modUnit>();
using (SqlConnection con = new SqlConnection(cs))
{
SqlCommand cmd = new SqlCommand("Select * From acM_Unit Where UnitID =" + uID, con);
cmd.CommandType = CommandType.Text;
con.Open();
SqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
modUnit unit = new modUnit();
unit.UnitID = Convert.ToInt32(rdr["Unitid"]);
unit.Name = rdr["Name"].ToString();
mUnits.Add(unit);
}
}
return View(mUnits);
}