Category / Section
How to resolve 'Value cannot be null' exception during post back
1 min read
How to resolve the “.
T
Use a condition to check whether the Post is external AJAX Post. It can be done by checking whether any of the values in PagingParams are null using the code if(args.ID == null) because for external AJAX Post PagingParams will always be null. If it is null don’t return GridAction instead return respective action result here we return Partial View so that this exception is resolved.
CSHTML:
<div id="NavToPage"> <input type="text" id="PageNumber" /> <input type="button" value="Next Page" onclick="pageAction()" /> </div> //Creating an External AJAX Post <script type="text/javascript"> function pageAction() { var pageObj = $("#PageNumber"); $.ajax({ type: 'POST', url: 'GridFeatures', data:{pagenum:pageObj.val()}, success: function (data) { $("#Grid").html(data); } }); } </script>
CONTROLLER:
public ActionResult GridFeatures() { var data = new KBEditing.Models.StudentDataContext().Student.Take(100).ToList(); ViewData["ProductName"] = "Grid"; return View(data); } [AcceptVerbs(HttpVerbs.Post)] public ActionResult GridFeatures(PagingParams args, string pagenum) //The cause of this exception is PagingParams is Null { IEnumerable data = OrderRepository.GetAllRecords().Take(200).ToList(); if (args.ID != null) { return data.GridActions<KBEditing.Models.Student>(); } else { return PartialView("GridPartial", data); //Here GridAction should not be returned. } }