How can I map JSON object (returned from Web API) to a View Model. [ I am using automapper to map my c# business objects to View Model. I am looking for somthing similar to map JSON to View Model]
I am getting the following error : "Cannot deserialize the current JSON object into type 'System.Collections.Generic.IEnumerable`1[CLAW.BLL.MLAReports.Models.CMAReport]' because the type requires a JSON array".
Here's the signature for my controller in Webapi.
namespace ReportsAPI.Controllers
{
public class ReportsController : ApiController
{
[HttpPost]
public CMAReportVM Reports([FromBody] StatsCriteria criteria)
{
var cmaReport = Service3.GetCMAReport(criteria.Masnums);
//Create Map to enable mapping business object to View Model
Mapper.CreateMap<CMAReport, CMAReportVM>();
// Maps model to VM model class
var cmaVM = Mapper.Map<CMAReport, CMAReportVM>(cmaReport);
reutn cmaVM;
}
}
}
Here's the signature for the controller at client side :
namespace MASReports.Controllers
{
public ActionResult Reports(StatsCriteria criteria)
{
var client = new HttpClient();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var response = client.PostAsJsonAsync("http://localhost:52765/api/reports", criteria.Masnums.ToString()).Result;
// The below line I get error.
var represlt= response.Content.ReadAsAsync<IEnumerable<CMAReportVM>>().Result;
return View("CMAReportVM", represlt);
}
}
Here's the signature for the model :
namespace MLAReports.Models
{
public class CMAReportVM:ReportsVM
{
public CMAReportVM() {}
public List<Cma_Item> CmaList { get; set; }
public string TimeStampString { get; set; }
public Dictionary<string, string> CriteriaNameValue { get; set; }
}
}
namespace CLAW.BLL.MLAReports.Models
{
public class Cma_Item : IStatsGridable<Cma_Fields>
{
public Cma_Item() {}
#region Properties and Fields
public string PropTypeName { get; set; }
public string StatusName { get; set; }
public string LowP { get; set; }
public string LowXStLP { get; set; }
public string HighP { get; set; }
public decimal? AvgLPFnl { get; set; }
public decimal? AvgSPFnl { get; set; }
#endregion
#region IStatsGrid Properties
public List<string> Headers
{
get
{ return _headers; }
set
{ _headers = value; }
}
private IEnumerable<Cma_Fields> _rowModels;
public IEnumerable<Cma_Fields> RowModels
{
get
{ return _rowModels;}
set
{ _rowModels = value;}
}
private List<string> _headers;
private string _id;
#endregion
}
}
namespace CLAW.BLL.MLaReports.Models
{
public interface IStatsGridable<T>
{
List<string> Headers { get; set; }
string Id { get; set; }
IEnumerable<T> RowModels { get; set; }
}
}
namespace CLAW.BLL.MLAReports.Models
{
public class Cma_Fields
{
public Cma_Fields() { }
#region Properties and Fields
public string Mlanum { get; set; }
public string Address { get; set; }
public string BR { get; set; }
public string BA { get; set; }
public string SF { get; set; }
public string TRId
{
get
{ return _trid; }
set
{_trid = value; }
}
#endregion
}
}
InnerException :
{"Cannot deserialize the current JSON object (eg {\\"name\\":\\"value\\"}) into type 'System.Collections.Generic.IEnumerable`1[CLAW.BLL.MLAReports.Models.CMAReport]' because the type requires a JSON array (eg [1,2,3]) to deserialize correctly.\\r\\nTo fix this error either change the JSON to a JSON array (eg [1,2,3]) or change the deserialized type so that it is a normal .NET type (eg not a primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.\\r\\nPath 'CmaList', line 1, position 11."}
Your reports controller signature is returning a single item
public CMAReportVM Reports([FromBody] StatsCriteria criteria)
but you're trying to read it as IEnumerable
var represlt= response.Content.ReadAsAsync<IEnumerable<CMAReportVM>>().Result;
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.