Asked by:
Null Value is passing to Controller

Question
-
User-797751191 posted
Hi
I have below code and Parameter is of string datatype
public JsonResult DeleteUser(string UsrName)
{
return Json(usrDB.DeleteUser(UsrName), JsonRequestBehavior.AllowGet);
}
function Delete(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); } }); } }Thanks
Wednesday, October 23, 2019 7:04 AM
All replies
-
User665608656 posted
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:
@{ 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 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).attr('id') + "'}", type: "POST", contentType: "application/json;charset=UTF-8", dataType: "json", success: function (result) { alert(result); location.href = "/C_1023_2160919/Index"; }, error: function (errormessage) { alert(errormessage.responseText); } }); } } </script> </head> <body> <table id="tblCustomers" class="table" cellpadding="0" cellspacing="0" style="border-collapse:collapse;text-align:center" border="1"> <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> </tr> @foreach (var customer in Model) { <tr> <td class="EmplId"> <span>@customer.EmplId</span> </td> <td class="FirstName"> <span>@customer.FirstName</span> </td> <td class="FirstName"> <span>@customer.LastName</span> </td> <td class="FirstName"> <span>@customer.EmailAddr</span> </td> <td> @Html.ActionLink("Delete", "Delete", null, new { id = customer.EmplId, @class = "delete", onclick = "Delete(this);" }) </td> </tr> } </table> </body> </html>
public class C_1023_2160919Controller : Controller { // GET: C_1023_2160919 public ActionResult Index() { Entities2 entity = new Entities2(); List<Employee> customers = entity.Employees.ToList(); return View(customers); } [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.
Wednesday, October 23, 2019 9:23 AM -
User-797751191 posted
Hi Yong
I have below html
function loadData() { $.ajax({ url: "/Home/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.UserName + '</td>'; html += '<td>' + item.Password + '</td>'; html += '<td>' + item.FirstName + '</td>'; html += '<td>' + item.LastName + '</td>'; html += '<td><a href="#" onclick="return getbyName(\'' + item.UserName + '\')">Edit</a> | <a href="javascript:void(0)" onclick="Delete(\'' + item.UserName + '\')">Delete</a></td>'; html += '</tr>'; }); $('.tbody').html(html); }, error: function (errormessage) { alert(errormessage.responseText); } }); }
Thanks
Wednesday, October 23, 2019 1:49 PM -
User1120430333 posted
/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.Wednesday, October 23, 2019 6:01 PM -
User665608656 posted
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:
@{ 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.
Thursday, October 24, 2019 5:44 AM