How to compare the data of my model (list) with other data (list) in this case?
Here is what I have:
My json view model
public class JsonViewModel { public List<JsonItem> Items { get; set; } } public class JsonItem { public string Name { get; set; } public int Unit { get; set; } public decimal Price { get; set; } public IEnumerable<Item> ItemStock { get; set; } }
My main model
public class Item { public int ItemId { get; set; } public string Name { get; set; } public int QuantityInPack { get; set; } public int Quantity { get; set; } public decimal Price { get; set; } public bool IsHidden { get; set; } }
which should be displayed like this:
Item.QuantityInPack = JsonItem.UnitItem.Price = JsonItem.Price
where Item.Name = JsonItem.Name
controller
public ActionResult Index() { // 1. Perform HTTP request to retrieve the JSON. var webClient = new WebClient(); string rawJson = webClient.DownloadString("http://my_json_data"); // 2. Parse the JSON. var jsonRootObject = JsonConvert.DeserializeObject<JsonViewModel>(rawJson); // 3. Map to viewmodel var viewModel = new JsonViewModel { Items = jsonRootObject.Items.Select(i => new JsonItem { Name = i.Name, Unit = i.Unit, Price = i.Price }).ToList() }; /// var TestItem = db.Items.ToList(); /// TestItem.QuantityInPack = JsonItem.Unit /// TestItem.Price = JsonItem.Price /// where Item.Name = JsonItem.Name /// /// (I know it a bad, but I wanted to explain what I mean) /// Here i should map data in some way /// /// // 4. Return mapped model to view return View( TestItem??? ); }
Dipix source share