Reputation: 478
I'm new to ASP.NET. I have done Json parsing when returning the data in WebAPI controller like this, `
public JsonResult getResult()
{
string dataconfig = Convert.ToString(ConfigurationManager.ConnectionStrings["Onviodb"]);
SqlConnection cnn = new SqlConnection(dataconfig);
SqlCommand cmd = null;
String sqlQuery = null;
SqlDataReader dataReader = null;
var list = new List<onvioAPI>();
try
{
cnn.Open();
sqlQuery = "Select * from tbl_Projects";
cmd = new SqlCommand(sqlQuery, cnn);
dataReader = cmd.ExecuteReader();
while (dataReader.Read())
{
onvioAPI od = new onvioAPI();
//od.id = Int32.Parse(dr["ID"].ToString());
od.aname = Convert.ToString(dataReader["Agency Contact Name"]);
od.vname = Convert.ToString(dataReader["Vendor Name"]);
od.pcity = Convert.ToString(dataReader["Project City"]);
od.pstate = Convert.ToString(dataReader["Project State"]);
od.vcity = Convert.ToString(dataReader["Vendor City"]);
list.Add(od);
}
cnn.Close();
}
catch (Exception ex)
{
Console.WriteLine("Can not open connection ! " + ex);
}
return Json(list,JsonRequestBehavior.AllowGet);
}
`
But there is an error showing like
The best overloaded method match for Json(List,JsonSerializerSettings) has some invalid aruguments
.
What should I do ?
Thanks in advance
Upvotes: 1
Views: 73
Reputation: 13488
It happened because there are two JsonResult
types: one from System.Web.Mvc
namespace(with enum JsonRequestBehavior
) and one from System.Web.Http.Results
(JsonResult<T>
) and I think you mixed up them. Solution is to do this:
using System.Web.Http;
//using System.Web.Mvc;
public IHttpActionResult getResult()
{
//your code..
var list = new List<onvioAPI>();
//your code..
return Json(list);
}
Upvotes: 1
Reputation: 613
Try this,
List<onvioAPI> list = new List<onvioAPI>();
This should work.
Upvotes: 0