简体   繁体   English

将空数据从$ .post发送到控制器模型

[英]Sending null data from $.post to controller model

My Model - 我的模特-

 public class Phasessfilter
{
    public string Searchterm {get;set;}
    public string ob1 {get;set;}
    public string ob2 {get;set;}
    public string ob3 {get;set;}
    public List<string> ob4 { get; set; }
    public List<string> ob5 { get; set; } 
}

Controller - 控制器-

  public JsonResult GtRlts(Phasessfilter jop)
    {
    }

this is my script 这是我的剧本

    var recr = "";
    var study = "";
    var results = "";
    var phases1 = [];
    var fund = [];
  var values =  { Searchterm: Searchterm, ob1: recr, ob2: study, ob3: results, ob4: phases1, ob5: fund }

    $.post("GtRlts", values, function (abc) {

   }

So i am getting values for Searchterm , ob1 ,ob2 ,ob3 but i am getting null values for ob4 and ob5 .why is List not taking the array values or am i doing something wrong . 所以我正在获取Searchterm,ob1,ob2,ob3的值,但是我正在获取ob4和ob5的空值。为什么List不采用数组值或我做错了什么。 PS - i dont want to use $.ajax PS-我不想使用$ .ajax

Change your model to 将模型更改为

public class Phasessfilter
{
    public string Searchterm {get;set;}
    public string ob1 {get;set;}
    public string ob2 {get;set;}
    public string ob3 {get;set;}
    public List<string> ob4 { get; set; }
    public List<string> ob5 { get; set; } 
    public Phasessfilter()
    {
        ob4 = new List<string>();
        ob5 = new List<string>();
    }
}

Hey the code written is absolutely fine you just need to test with the values in it and it works just fine. 嘿,编写的代码绝对不错,您只需要测试其中的值即可,并且效果很好。

here is sample Js code 这是示例Js代码

 var searchterm = "test1";
    var recr = "test2";
    var study = "test3";
    var results = "test4";
    var phases1 = ["Test5","Test6"];
    var fund = ["Test6", "Test7"];
    debugger;
    var values = { Searchterm: searchterm, ob1: recr, ob2: study, ob3: results, ob4: phases1, ob5: fund }

    $.post(path + "/Dashboard/Home/GtRlts", values, function (abc) {

    });

and the output for a reference rest of the code is same. 其余代码的输出相同。

在此处输入图片说明

You have just define array variable so it get null value 您刚刚定义了数组变量,所以它获取空值

var phases1 = [];
var fund = [];

You have to add items in it: 您必须在其中添加项目

var phases1 = [1,2];
var fund = [3,4];

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

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