functionDelete(UsrName){var ans = confirm("Are you sure you want to delete this Record?");if(ans){
$.ajax({
url:'/Home/DeleteUser/',
data: JSON.stringify({UserName:UsrName}),
type:"POST",
contentType:"application/json;charset=UTF-8",
dataType:"json",
success:function(result){
loadData();},
error:function(errormessage){
alert(errormessage.responseText);}});}}
According to your code, you didn't explain the issue you were having, but I combined the data loading code and the deletion method you provided to create a case that you can refer to:
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
<script src="~/Scripts/jquery-3.3.1.min.js"></script>
<script type="text/javascript">
function loadData() {
$.ajax({
url: "/C_1023_2160919/List",
type: "GET",
contentType: "application/json;charset=utf-8",
dataType: "json",
success: function (result) {
var html = '';
$.each(result, function (key, item) {
html += '<tr>';
html += '<td>' + item.EmplId + '</td>';
html += '<td>' + item.FirstName + '</td>';
html += '<td>' + item.LastName + '</td>';
html += '<td>' + item.EmailAddr + '</td>';
html += '<td><a href="#" onclick="return getbyName(\'' + item.EmplId + '\')">Edit</a> | <a href="javascript:void(0)" onclick="Delete(\'' + item.EmplId + '\')">Delete</a></td>';
html += '</tr>';
});
$('.tbody').html(html);
},
error: function (errormessage) {
alert(errormessage.responseText);
}
});
}
function Delete(UsrName) {
event.preventDefault();
var ans = confirm("Are you sure you want to delete this Record?");
if (ans) {
$.ajax({
url: '/C_1023_2160919/DeleteUser',
data: "{ UsrName:'" + UsrName + "'}",
type: "POST",
contentType: "application/json;charset=UTF-8",
dataType: "json",
success: function (result) {
alert(result);
loadData();
},
error: function (errormessage) {
alert(errormessage.responseText);
}
});
}
}
</script>
</head>
<body onload="loadData()">
<table id="tblCustomers" class="table" cellpadding="0" cellspacing="0" style="border-collapse:collapse;text-align:center" border="1">
<thead>
<tr>
<th style="width:100px">EmplId</th>
<th style="width:150px">FirstName</th>
<th style="width:100px">LastName</th>
<th style="width:150px">EmailAddr</th>
<th> </th>
</tr>
</thead>
<tbody class="tbody"></tbody>
</table>
</body>
</html>
public class C_1023_2160919Controller : Controller
{
// GET: C_1023_2160919
public ActionResult Index()
{
return View();
}
[HttpGet]
public JsonResult List()
{
Entities2 entity = new Entities2();
List<Employee> customers = entity.Employees.ToList();
return Json(customers, JsonRequestBehavior.AllowGet);
}
[HttpPost]
public JsonResult DeleteUser(string UsrName)
{
return Json(DeleteUserMethod(UsrName), JsonRequestBehavior.AllowGet);
}
public string DeleteUserMethod(string UsrName)
{
string deleteMessage = string.Empty;
int id = Convert.ToInt32(UsrName);
try
{
using (EntityData entities = new EntityData())
{
Employee customer = (from c in entities.Employees
where c.EmplId == id
select c).FirstOrDefault();
entities.Employees.Remove(customer);
entities.SaveChanges();
}
deleteMessage = "delete successfully!";
}
catch (Exception ex)
{
deleteMessage = ex.Message.ToString();
}
return deleteMessage;
}
}
Here is the result of this work demo:
Best Regards,
YongQing.
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
Member
140 Points
518 Posts
Null Value is passing to Controller
Oct 23, 2019 07:04 AM|jsshivalik|LINK
Hi
I have below code and Parameter is of string datatype
Thanks
Contributor
3710 Points
1043 Posts
Re: Null Value is passing to Controller
Oct 23, 2019 09:23 AM|Yongqing Yu|LINK
Hi jsshivalik,
According to your code, I found that the parameter you passed from Ajax is just a string type UsrName, not an entity .
I made a case of deleting data in MVC. You can refer to the following code:
Here is the result of this work demo:
Best Regards,
YongQing.
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
Member
140 Points
518 Posts
Re: Null Value is passing to Controller
Oct 23, 2019 01:49 PM|jsshivalik|LINK
Hi Yong
I have below html
Thanks
Contributor
4933 Points
4205 Posts
Re: Null Value is passing to Controller
Oct 23, 2019 06:01 PM|DA924|LINK
/Home/GetbyName?UsrName=" + '$('#UserName').val(result.UserName).ToString();
You can't do something like above? It's been sometime since I have used Jquery
And Username is spelled wrong in the action method too.
Contributor
3710 Points
1043 Posts
Re: Null Value is passing to Controller
Oct 24, 2019 05:44 AM|Yongqing Yu|LINK
Hi jsshivalik,
According to your code, you didn't explain the issue you were having, but I combined the data loading code and the deletion method you provided to create a case that you can refer to:
public class C_1023_2160919Controller : Controller { // GET: C_1023_2160919 public ActionResult Index() { return View(); } [HttpGet] public JsonResult List() { Entities2 entity = new Entities2(); List<Employee> customers = entity.Employees.ToList(); return Json(customers, JsonRequestBehavior.AllowGet); } [HttpPost] public JsonResult DeleteUser(string UsrName) { return Json(DeleteUserMethod(UsrName), JsonRequestBehavior.AllowGet); } public string DeleteUserMethod(string UsrName) { string deleteMessage = string.Empty; int id = Convert.ToInt32(UsrName); try { using (EntityData entities = new EntityData()) { Employee customer = (from c in entities.Employees where c.EmplId == id select c).FirstOrDefault(); entities.Employees.Remove(customer); entities.SaveChanges(); } deleteMessage = "delete successfully!"; } catch (Exception ex) { deleteMessage = ex.Message.ToString(); } return deleteMessage; } }
Here is the result of this work demo:
Best Regards,
YongQing.
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.