简体   繁体   English

没有从ajax调用控制器获取数据作为MVC中的数组数据c#

[英]Not getting data from ajax call to controller as array data in MVC c#

## AJAX CALL in cshtml page ## ## AJAX CALL in cshtml page ##

function ValidateData() {
            var editedRows = [];
            $("#eventsTable tbody tr").each(function () {
                editedRows.push({
                    PK_WS_Event: $(this).find("td:eq(1)")[0].innerText,
                    Act_Id: $(this).find("td:eq(2)")[0].innerText,
                    WeeklyStatus: $(this).find("td:eq(3) option:selected").text()
               == "--Select--" ? "" : $(this).find("td:eq(3) 
          option:selected").text(),
                    WeeklyStatusSubType: $(this).find("td:eq(4)  
         option:selected").text() == "--Select--" ? "" : $(this).
         find("td:eq(4) option:selected").text(),
                    AccountName: $(this).find("td:eq(5)")[0].innerText,
                    AccountNumber: $(this).find("td:eq(6)")[0].innerText,
                    WeeklyStatusDesc: $(this).find("td:eq(7)")
             [0].textContent.trim(),
                });
            });

            $.ajax({
                type: 'post',
                url: "/WeeklyStatus/IndexSubmit",
                data: JSON.stringify({ editedRows: editedRows }),
                contentType: 'application/json',
                dataType: "html",
                success: function (response) { 
                },
                error: function (xhr, status, error) {
                    alert('fail');
                }
            });
        }



    ## Server Code Controller and ##
    ----------
       [HttpPost]
            public IActionResult IndexSubmit(IList<WeeklyStatusEvents> 
          editedRows)
            {
                return null;
            }

My ViewModel as like as below.. 我的ViewModel如下所示..

public class ReportViewModel
{
    public int SelectedStatusId { get; set; }
    public int SelectedSubTypeId { get; set; }


    public List<WeeklyStatusEvents> statusEvents { get; set; }

}

And WeeklyStatusEvents model as shown below 和WeeklyStatusEvents模型如下所示

public class WeeklyStatusEvents
{

    public string PK_WS_Event { get; set; }
    public string Act_Id { get; set; }
    //public bool INDExcludeFromRpt { get; set; }
    public string WeeklyStatus { get; set; }
    public string WeeklyStatusSubType { get; set; }
    public string AccountName { get; set; }
    public string AccountNumber { get; set; }
    public string WeeklyStatusDesc { get; set; }


}

So Here, I am getting count as 0...... 所以在这里,我算作0 ......

I have created class with all these properties. 我创建了具有所有这些属性的类。 and Same property name I was used in ajax call as well. 和我在ajax调用中使用的相同的属性名称。

Even I am not getting data. 即使我没有得到数据。 Can you provide the solution what i missed to get the data. 你能提供我错过的解决方案来获取数据吗?

Replace 更换

data: JSON.stringify({ editedRows: editedRows }),

with

data: JSON.stringify(editedRows),

The difference is that you want to send an array of objects instead of an object which contains an array. 不同之处在于您要发送一个对象数组而不是包含数组的对象。

In order to see the diferences between those two, I recommend you to inspect what the following lines return: 为了查看这两者之间的差异,我建议您检查以下行返回的内容:

console.log(JSON.stringify(editedRows) 
console.log(JSON.stringify({ editedRows: editedRows })

And add [FromBody] in front of your parameter in your action. 并在动作中的参数前添加[FromBody]。

Add [FromBody] in front of your List and try again 在列表前添加[FromBody],然后重试

[HttpPost]
public ActionResult IndexSubmit([FromBody] List<WeeklyStatusEvents> obj)
{
   Vreturn null;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM