0

I am trying to pass data in action method CloseCase from cshtml page and want to pass an array of value at action side in controller.but I am getting an error. I am new to asp.net mvc so I can't say whether my approach is correct.

While passing value from ajax it is showing null value at CloseCase method. I want to get command.Parameters.Add(":ICD_CASE_ID", casedetail[0]); means casedetail[0] value but while debugging i found value at method CloseCase null values. Any idea would be appreciated.

.cshtml

$(".btnrowvalue2").click(function () {
            //var casedetails = new Array();
            var tr = $(this).closest('tr');
            var casedetail={};
            var casedetails=[];
            casedetail["FirstCol"] = tr.find('input[name="Chkb"]').val();
            casedetail["SecondCol"] = tr.find('input[name="CaseId"]').val();
            casedetail["ThirdCol"] = tr.find('input[name="Desc"]').val();
            casedetail["FourthCol"] = tr.find('input[name="NBill"]').val();
            casedetail["FifthCol"] = tr.find('input[name="TAmnt"]').val();
            casedetail["SixthCol"] = tr.find('input[name="Stat"]').val();
            casedetail["SeventhCol"] = tr.find('input[name="Cron"]').val();
            casedetail["EightCol"] = tr.find('input[name="Clon"]').val();
            casedetails.push(casedetail);
            //alert('Type1 : ' + FirstCol + ' ' + SecondCol + ' ' + ThirdCol);
            $.ajax({
                type:"POST",
                url:"/Home/CloseCase",
                data:JSON.stringify(casedetails),
                contentType:"application/json; charset=utf-8",
            datatype:"json",
            success:function(r){
                alert(r + " record(s) inserted.");
            }
            });
        });

controller side

 public JsonResult CloseCase(List<Ipcell> casedetail)
        {            
          
            try
            {
                conn.Open();
                string qry = "UPDATE ASE_S SET ICD_STATUS='O',ICD_CLOSED_ON=:ICD_CLOSED_ON,ICD_CLOSED_BY=:ICD_CLOSED_BY WHERE ICD_CASE_ID =:ICD_CASE_ID";
                OracleCommand command = new OracleCommand(qry, conn);
                command.Parameters.Add(":ICD_CLOSED_ON", DateTime.Now);
                command.Parameters.Add(":ICD_CLOSED_BY", Session[CommonConstants.SESSION_USER_ID]);
                command.Parameters.Add(":ICD_CASE_ID", casedetail[0]);
               int insertedRecords=command.ExecuteNonQuery();
                return Json(insertedRecords);
            }
            catch (Exception ex)
            {

            }
            finally
            {
                conn.Close();
            }
            return null;
        }

model class

public class Ipcell
    {        
        public string CaseId { get; set; }
        public string Descripton { get; set; }
        public int NoOfBill { get; set; }
        public decimal TotalAmount { get; set; }
        public DateTime From { get; set; }
        public DateTime To { get; set; }
        public string Type { get; set; }
        public string Status { get; set; }
        public DateTime CreatedOn { get; set; }
        public DateTime ClosedOn { get; set; }
    }
7
  • Welcome to StackOverflow. Please, read How to Ask, and try to provide a minimal reproducible example. Commented Aug 31, 2020 at 11:43
  • Can you share the json which is sent from view to Controller? Commented Aug 31, 2020 at 11:46
  • @ChetanRanpariya casedetail = {FirstCol: "on", SecondCol: "2", ThirdCol: "CAB", FourthCol: "1", FifthCol: "585.60", …} , iam reciving value in this way Commented Aug 31, 2020 at 11:49
  • 1
    ISpell class does not have properties FirstCol, SecondCol etc... Commented Aug 31, 2020 at 11:52
  • 1
    @ChetanRanpariya is right. That is your main issue. But also, it seems you are only passing one row/record to your Action, so I'd change this List<Ipcell> casedetail with this Ipcell casedetail. If you apply that change, then in this line command.Parameters.Add(":ICD_CASE_ID", casedetail[0]); replace casedetail[0] with casedetail.CaseId. Commented Aug 31, 2020 at 12:00

2 Answers 2

1

I have checked your code and i found the issue

Check my screenshot and implement it is working for me.

Ajax Call

var casedetail = {};
    var casedetails = [];
    var date = new Date();
    casedetail["CaseId"] = "1";
    casedetail["Descripton"] = "2";
    casedetail["NoOfBill"] = "3";
    casedetail["TotalAmount"] = "4";
    //casedetail["From"] = date.toISOString();
    //casedetail["To"] = date.toISOString();
    casedetail["Type"] = "7";
    casedetail["Status"] = "8";
    //casedetail["CreatedOn"] = date.toISOString();
    //casedetail["ClosedOn"] = date.toISOString();
    casedetails.push(casedetail);
    $.ajax({
        type: "POST",
        url: "../Home/CloseCase",
        data: JSON.stringify(casedetails),
        contentType: "application/json; charset=utf-8",
        datatype: "json",
        success: function (r) {
            alert(r + " record(s) inserted.");
        }
    });

Code

public JsonResult CloseCase(List<Ipcell> casedetail)
        {

            try
            {
                return Json(new { },JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {

            }
            finally
            {
            }
            return Json(new { }, JsonRequestBehavior.AllowGet);
        }

        public class Ipcell
        {
            public string CaseId { get; set; }
            public string Descripton { get; set; }
            public int NoOfBill { get; set; }
            public decimal TotalAmount { get; set; }
            public DateTime From { get; set; }
            public DateTime To { get; set; }
            public string Type { get; set; }
            public string Status { get; set; }
            public DateTime CreatedOn { get; set; }
            public DateTime ClosedOn { get; set; }
        }
Sign up to request clarification or add additional context in comments.

1 Comment

Screenshots are rarely useful, since people have to transcribe the text, and visually identify the differences between text and a picture of text. Instead, describe what you've changed, and include the changes as text, using pictures only as an extra detail.
0

I'd say that your problem is that your controller method doesn't recognizes your JSON string as a casedetail parameter.

Keep in mind that you're sending a string inside data on your AJAX call. The method, on its side, is waiting for a List of IpCell objects. I have serious doubts the inference engine would be able to convert on its own those data.

Try to change caseDetail to String on closeCase signature, and make a conversion inside the method to parse the JSON string to a list of IpCell elements (you'll probably want to create a method in your IpCell class to simplify this task).

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.