简体   繁体   中英

Hello . I can't get the data from Jquery datatable .Can you help me?

//Name Surname Adres Email No data available in table Showing 0 to 0 of 0 entries

public class PersonModel
{
    public string Name { get; set; }
    public string Surname { get; set; }
    public string Adres { get; set; }
    public string Email { get; set; }
    public string DateTime { get; set; }
}
----
public ActionResult Index()
    {
        return View();
    }

    [HttpPost]
    public JsonResult AjaxMethod(string name,string surname,string adres,string email)
    {
        Session["Name"] = name;
        Session["Surname"] = surname;
        Session["Adres"] = adres;
        Session["Email"] = email;
        PersonModel person = new PersonModel
        {
            Name = Session["Name"].ToString(),
            Surname = Session["surname"].ToString(),
            Adres = Session["adres"].ToString(),
            Email = Session["email"].ToString(),
            DateTime = DateTime.Now.ToString()
        };

        return Json(person);
    }
---
<body>
<input type="text" id="txtName" />
<input type="text" id="txtSurname" />
<input type="text" id="txtAdres" />
<input type="text" id="txtEmail" />
<input type="button" id="btnSet" value="Set Session" />
<table id="myTable" class="table table-condensed">
<thead>
    <tr>
        <th>Name</th>
        <th>Surname</th>
        <th>Adres</th>
        <th>Email</th>
    </tr>
</thead>
</table>
    $(function () {
        $("#btnSet").click(function () {
            var obj = { name: $("#txtName").val(), surname: $("#txtSurname").val(), adres: 
$("#txtAdres").val(), email: $("#txtEmail").val() };
            $.ajax({
                type: "POST",
                url: "/Home/AjaxMethod",
                data: JSON.stringify(obj),
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (data, textStatus, jqXHR) {
                    $('#myTable').dataTable({
                        data: data,

                        columns: [
                            { "data": "Name" },
                            { "data": "Surname" },
                            { "data": "Adres" },
                            { "data": "Email" }

                        ],
                        searching: false
                    });

                }
                
            });
        });
    });
</script>

data is not coming, how can I get it? I guess making a mistake in js I can post to data class but after i couldn't see at datatable. It could be a mistake at Columns I get an error when I write the code below No data available in table Showing 0 to 0 I am waiting your suggestions.

var obj = { name: $("#txtName").val(), surname: $("#txtSurname").val(), adres: $("#txtAdres").val(), email: $("#txtEmail").val() };
               
 data: JSON.stringify(obj),

you convert obj into one string, but in AjaxMethod you catch multi propertys...

try with

public JsonResult AjaxMethod(string id)

and then deserialize this object in C#

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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