I have a view :
@{
ViewBag.Title = "Home Page";
}
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<link href="~/Content/themes/base/jquery-ui.css" rel="stylesheet" />
<link href="~/Scripts/datatable/DataTables-1.10.20/css/dataTables.jqueryui.min.css" rel="stylesheet" />
@section Scripts{
<script src="~/Scripts/datatable/DataTables-1.10.20/js/jquery.dataTables.min.js"></script>
<script src="~/Scripts/datatable/DataTables-1.10.20/js/dataTables.jqueryui.min.js"></script>
<script>
$(document).ready(function () {
$("#contenttable").DataTable({
});
$("#submitdata").click(function () {
var datatabledata = $("#datatble-form").serialize();
alert(datatabledata);
$.ajax({
url: "/Home/getData",
type: "POST",
data: { model: datatabledata },
success: function () {
alert('success');
},
error: function () {
alert('failure');
}
});
});
});
</script>
}
</head>
<body>
<form id="datatble-form">
<table>
<tr>
<td>Friend Name</td>
<td><input name="FriendName" type="text" /></td>
</tr>
<tr>
<td>Friend Age</td>
<td><input name="FriendAge" type="text" /></td>
</tr>
<tr>
<td>Position</td>
<td><input name="Friendposition" type="text" /></td>
</tr>
<tr>
<td>Office</td>
<td><input name="Friendoffice" type="text" /></td>
</tr>
</table>
<input id="submitdata" type="button" value="Submit" />
</form>
</body>
</html>
My controller is :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Script.Serialization;
using datatableViewTocontroller.Models;
namespace datatableViewTocontroller.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult getData(string model)
{
JavaScriptSerializer jss = new JavaScriptSerializer();
MyClass myClass = jss.Deserialize<MyClass>(model);
return RedirectToAction("index");
}
}
}
and My Model is :
namespace datatableViewTocontroller.Models
{
public class MyClass
{
public string FriendName { get; set; }
public string FriendAge { get; set; }
public string Friendposition { get; set; }
public string Friendoffice { get; set; }
}
}
Ok now I ellaborate my problem.
I have a form with the inputs for name, age, position and office and Iam trying to submit the data to the controller by serialize(). The form is getting serialized ( because it is displaying the values in the alert ) and controller call is also successfull like :
but while deserialzing I have got this error :
Invalid JSON primitive: FriendName.
like this :
How can I resolve this issue ? please help..
2
Answers
Whatever you are getting in your action method of controller is not json data that’s why you are getting the error while deserializing it.
Solution
You can update your
getdata
method instead of passing string in parameter you can pass your model class which will bind data automatically and you will no longer required deserializing the json.And also change your click method as below
1.Add
serializeObject
function to serializeform data
tojson
2. No need to deserialize MyClass in action codes,
Screenshots of test: