asp.net mvc api json formatting - asp.net-mvc

I have a query that returns something like this:
OBJECT | QTY | ......
Random_Name_1 | 5 | .....
Random_Name_2 | 3 | ......
I want to return a j-son like this
{"Success": true, "Message": "Total rows 8", "Data": { "Random_Name_1": {"QTY": 5 ... }, "Random_Name_2": {"QTY": 3 ... } } }
How can i accomplish this in a web API made on ASP.NET MVC4?

You could try this:
// your data
var q = new[]
{
new {OBJECT = "Random_Name_1", QTY = 5, TYPE = "A"},
new {OBJECT = "Random_Name_2", QTY = 3, TYPE = "B"},
new {OBJECT = "Random_Name_3", QTY = 8, TYPE = "B"}
};
// construct the object
var obj = new
{
Success = true,
Message = string.Format("Total rows {0}", q.Count()),
Data = q.ToDictionary(
item => item.OBJECT,
item => item.GetType().GetProperties()
.Where(p => p.Name != "OBJECT")
.ToDictionary(p => p.Name, p => p.GetValue(item, null)))
};
// serialize the object (note that you don't need to do that from web api)
var json = JsonConvert.SerializeObject(obj);
Note taht if you already know the structure of data (i.e. the columns) then you can make it more efficient by avoiding reflection. Means:
var obj = new
{
Success = true,
Message = string.Format("Total rows {0}", q.Count()),
Data = q.ToDictionary(
item => item.OBJECT,
item => new { item.QTY, item.TYPE })
};

Here is how i solved it, on monday I'll probably change it to be reflective..
var x = tw.spCall(type).ToList();
if (x.Count() == 0)
return new { Success = false, Message = "No data!" };
DataSet temp = new DataSet();
foreach (var y in x) {
if (temp.Tables[y.EQUIPMENT] == null) {
temp.Tables.Add(y.EQUIPMENT);
temp.Tables[y.EQUIPMENT].Columns.Add("id_key");
temp.Tables[y.EQUIPMENT].Columns.Add("PartNumber");
...
};
DataRow row = temp.Tables[y.EQUIPMENT].Rows.Add();
row.SetField("id_key",y.id_key);
row.SetField("PartNumber",y.PartNumber);
...
}
return JsonConvert.SerializeObject(temp, Newtonsoft.Json.Formatting.None);

Related

Add and Update in ASP.NET Core with Entity Framework

I am doing add and update JSON data into the SQL table.
Below Code working for adding and updating a new set of records
List<CatalogProduct> ListKp = new List<CatalogProduct>();
using (var transaction = _context.Database.BeginTransaction())
{
try
{
int numP = 0;
var catalogProducts = _context.CatalogProducts.ToList();
foreach (var kp in ListKp)
{
if (!catalogProducts.Any(x => x.Name == kp.Name))
{
_context.CatalogProducts.Add(kp);
}
else
{
//Use AutoMapper automatically do the mapping
var config = new MapperConfiguration(cfg => cfg.CreateMap<CatalogProduct, CatalogProduct>().ForMember(c => c.Id, opt => opt.Ignore()));
var oldone = catalogProducts.FirstOrDefault(c => c.Name == kp.Name);
var mapper = config.CreateMapper();
oldone = mapper.Map<CatalogProduct, CatalogProduct>(kp, oldone);
_context.CatalogProducts.Update(oldone);
}
}
numP = _context.SaveChanges();
transaction.Commit();
return Json("No conflicts. " + numP + " product details saved.");
}
catch (Exception ex)
{
transaction.Rollback();
return Json("Error occurred." + ex.Message);
throw new Exception();
}
}
Sample JSON data
{
"title": "Brown eggs",
"type": "dairy",
"description": "Raw organic brown eggs in a basket",
"filename": "0.jpg",
"height": 600,
"width": 400,
"price": 28.1,
"rating": 4
},
{
"title": "Sweet fresh stawberry",
"type": "fruit",
"description": "Sweet fresh stawberry on the wooden table",
"filename": "1.jpg",
"height": 450,
"width": 299,
"price": 29.45,
"rating": 4
},
First I will add [type] key object values from the above JSON into the products table [NAME] field. It will add a new set of records.
When I try to update the products table [NAME] field with [title] key object values, again it will add a new set of records.
Need to update products table [NAME] field without adding again.
I don't know how to check already existing records in a table with model list values. Already spent much time on this. I am new to EF Core, please anybody can help me
Complete Code
[HttpPost]
public IActionResult InsertProductDetails()
{
using WebClient wc = new WebClient();
string contentString = wc.DownloadString(baseurl);
List<Dictionary<string, string>> ListJsonProductContent = new List<Dictionary<string, string>>();
var token = JToken.Parse(contentString);
if (token.Type == JTokenType.Array) // "["
{
ListJsonProductContent = JsonConvert.DeserializeObject<List<Dictionary<string, string>>>(contentString);
}
else if (token.Type == JTokenType.Object) // "{"
{
var ObjectResponse = JsonConvert.DeserializeObject<Dictionary<string, object>>(contentString);
foreach (var x in ObjectResponse)
{
string key = x.Key.ToString();
string val = x.Value.ToString();
foreach (var dicItemML in JsonConvert.DeserializeObject<List<Dictionary<string, string>>>(val))
{
ListJsonProductContent.Add(dicItemML);
}
}
}
List <K360MappingMaster> ListMappedDataDb = new List<K360MappingMaster>();
var VLinqQuery = from KMM in _context.K360MappingMasters
where KMM.ThirdPartyBaseUrlName != null && KMM.ThirdPartyBaseUrlName == baseurl
select KMM;
ListMappedDataDb = VLinqQuery.ToList();
foreach (var dicItemML in ListJsonProductContent)
{
Dictionary<string, string> updItem = new Dictionary<string, string>();
foreach (var itemMl in dicItemML)
{
if (ListMappedDataDb.Select(s => s.ApiCatalog).ToList().Contains(itemMl.Key))
{
if (updItem.ContainsKey(ListMappedDataDb.Where(s => s.ApiCatalog == itemMl.Key).Select(s => s.K360Catalog).FirstOrDefault()))
{
if (ListMappedDataDb.Where(s => s.ApiCatalog == itemMl.Key).Select(s => s.K360Catalog).FirstOrDefault() == "Specification")
{
updItem[ListMappedDataDb.Where(s => s.ApiCatalog == itemMl.Key).Select(s => s.K360Catalog).FirstOrDefault()] += "<p>" + itemMl.Key + " :" + itemMl.Value + "<p>";
}
else
{
updItem[ListMappedDataDb.Where(s => s.ApiCatalog == itemMl.Key).Select(s => s.K360Catalog).FirstOrDefault()] += " " + itemMl.Value;
}
}
else
{
if (ListMappedDataDb.Where(s => s.ApiCatalog == itemMl.Key).Select(s => s.K360Catalog).FirstOrDefault() == "Specification")
{
updItem.Add(ListMappedDataDb.Where(s => s.ApiCatalog == itemMl.Key).Select(s => s.K360Catalog).FirstOrDefault(), "<p>" + itemMl.Key + " :" + itemMl.Value + "<p>");
}
else
{
updItem.Add(ListMappedDataDb.Where(s => s.ApiCatalog == itemMl.Key).Select(s => s.K360Catalog).FirstOrDefault(), itemMl.Value);
}
}
}
dicItemML.Remove(itemMl.Key);
}
foreach (var itemM2 in updItem)
{
dicItemML.Add(itemM2.Key, itemM2.Value);
}
}
List<CatalogProduct> ListKp = new List<CatalogProduct>();
foreach (var dicItem in ListJsonProductContent)
{
CatalogProduct Ctgkp = new CatalogProduct
{
Name = dicItem.ContainsKey("Name") ? dicItem["Name"] : "No Product",
Slug = dicItem.ContainsKey("Name") ? string.Concat(dicItem["Name"].Where(c => !char.IsWhiteSpace(c))).ToLower() : "No Slug",
Price = dicItem.ContainsKey("Price") ? decimal.Parse(dicItem["Price"], CultureInfo.InvariantCulture) : default,
ShortDescription = dicItem.ContainsKey("ShortDescription") ? dicItem["ShortDescription"] : null,
Description = dicItem.ContainsKey("Description") ? dicItem["Description"] : null,
Specification = dicItem.ContainsKey("Specification") ? dicItem["Specification"] : null,
RatingAverage = dicItem.ContainsKey("RatingAverage") ? double.Parse(dicItem["RatingAverage"], CultureInfo.InvariantCulture) : null};
ListKp.Add(Ctgkp);
}
using (var transaction = _context.Database.BeginTransaction())
{
try
{
int numP = 0;
var catalogProducts = _context.CatalogProducts.ToList();
foreach (var kp in ListKp)
{
if (!catalogProducts.Any(x => x.Name == kp.Name))
{
_context.CatalogProducts.Add(kp);
}
else
{
//Use AutoMapper automatically do the mapping
var config = new MapperConfiguration(cfg => cfg.CreateMap<CatalogProduct, CatalogProduct>().ForMember(c => c.Id, opt => opt.Ignore()));
var oldone = catalogProducts.FirstOrDefault(c => c.Name == kp.Name);
var mapper = config.CreateMapper();
oldone = mapper.Map<CatalogProduct, CatalogProduct>(kp, oldone);
_context.CatalogProducts.Update(oldone);
}
}
numP = _context.SaveChanges();
(from q in _context.K360MappingMasters
where q.ThirdPartyBaseUrlName == baseurl
select q).ToList().ForEach(x => x.InsertStatusFlag = true);
_context.SaveChanges();
transaction.Commit();
return Json("No conflicts. " + numP + " product details saved.");
}
catch (Exception ex)
{
transaction.Rollback();
return Json("Error occurred." + ex.Message);
throw new Exception();
}
}
For the core code:
var catalogProducts = _context.CatalogProducts.ToList();
foreach (var kp in ListKp)
{
if (!catalogProducts.Any(x => x.Name == kp.Name))
_context.CatalogProducts.Add(kp);
else
{
//Use AutoMapper automatically do the mapping
var config = new MapperConfiguration(cfg => cfg.CreateMap<CatalogProduct, CatalogProduct>().ForMember(c => c.Id, opt => opt.Ignore()));
var oldone = catalogProducts.FirstOrDefault(c => c.Name == kp.Name);
var mapper = config.CreateMapper();
oldone = mapper.Map<CatalogProduct, CatalogProduct>(kp, oldone);
_context.CatalogProducts.Update(oldone);
}
}
The first detail will be whether your DbContext was set up to disable change tracking proxies. For instance if the following code is in your DbContext constructor or when the DbContext is created:
Configuration.AutoDetectChangesEnabled = false;
Configuration.ProxyCreationEnabled = false;
Many teams turn off proxies when they encounter performance problems tripping over lazy loading. (instead of fixing the data access approach to avoid the lazy loading) Disabling proxies and change tracking makes update scenarios uglier. If these are off by default then I would recommend enabling them temporarily for this operation:
_context.Configuration.AutoDetectChangesEnabled = true;
_context.Configuration.ProxyCreationEnabled = true;
var catalogProducts = _context.CatalogProducts.ToList(); is going to fetch all catalog products from your DB. Depending on your data size this may not be practical. I'd suggest reading these one by one or consolidating the product names from the XML into a list and fetch the desired ones using Contains. Fetching each item by name /w change tracking enabled can be simplified to:
// Avoid re-initializing automapper EVERY loop iteration.
var config = new MapperConfiguration(cfg => cfg.CreateMap<CatalogProduct, CatalogProduct>()
.ForMember(c => c.Id, opt => opt.Ignore()));
var mapper = config.CreateMapper();
foreach (var kp in ListKp)
{
var existingCatalogProduct = _context.CatalogProducts.SingleOrDefault(x => x.Name == kp.Name);
if (existingCatalogProduct == null)
_context.CatalogProducts.Add(kp);
else
mapper.Map<CatalogProduct, CatalogProduct>(kp, existingCatalogProduct);
}
// ...
_context.SaveChanges();
If the Product entity has any navigation properties I would recommend setting those to Ignore as well.
We don't need to call Update when using change tracking. EF will automatically compose UPDATE statements for changed values on records if those values actually change as a result of the map copying over new values. With Mapper.Map<src,dest> I'm not sure what the return value references, if it passes back "dest" or a new object reference. You can ignore the return value, the method will update the values in the "dest" object reference. If the return value is a different object reference than "dest" then that could explain why it is treating the record as a new row as it would be a separate reference.
When you are expecting a single record, use the Single / SingleOrDefault rather than the First variant methods. First should only be used if you expect possible multiples and should always be used with an OrderBy clause.
Update:
If Name is not enough to uniquely identify a record to update vs. insert then you can change the filter to provide enough detail to try and identify the row before determining if an insert is required:
var existingCatalogProduct = _context.CatalogProducts
.SingleOrDefault(x => x.Name == kp.Name
&& x.ProductType = kp.ProductType
&& x.Category = kp.Category /*etc*/ );
This way if you find an existing catalog product row you can update the remaining details based with the Map call, or insert a new entry if no row is returned.

Orderby clause not working with drop down list in mvc

I am populating a drop down list using Linq and the orderby clause doesn't seem to work.
public List<Hello> getManagers()
{
var que = (from man in db.Table1
where man.Role == "Manager"
orderby man.Name
select new Hello
{
Managers = man.Name
}).Distinct().ToList();
return que;
}
Controller Class:
public ActionResult Index()
{
rp = new RequestProcess();
ViewBag.ID = fillSelectedList("", "ID", rp);
ViewBag.Managers = fillSelectedList("", "Managers", rp);
return View(""); //View 1
}
public static List<SelectListItem> fillSelectedList(string selValue, string type, RequestProcess rp )
{
List<SelectListItem> list = new List<SelectListItem>();
SelectListItem obj = new SelectListItem();
if (type == "Managers") {
var tempList= rp.getManagers();
tempList.ForEach(x =>
{
obj = new SelectListItem();
obj.Text = x.Managers;
obj.Value = x.Managers;
obj.Selected = x.Managers == selValue ? true : false;
list.Add(obj);
});
}
return list;
}
I am still receiving an un-ordered list. Any fixes?
The result is not ordered, because method Distinct does not return ordered results. What you need to do instead is to first call Disctinct, and only then OrderBy:
var que = (from man in db.Table1
where man.Role == "Manager"
select new Hello
{
Managers = man.Name
}).Distinct() // <- First distinct ...
.OrderBy(x => x.Managers) // <- ... then order by
.ToList();
As mentioned in the answer above, you need to sort the result after Distinct().
Also note that you are mixing Lambda expression and LINQ to Entities Queries... you may want to consider choosing one of them for consistency (though there is no syntax error if you mix them). This is the same query using lambda expression:
var que = _context.Table1
.Where(m => m.Role == "Manager")
.Select(h => new Hello { Managers = h.Name })
.Distinct()
.OrderBy(o => o.Managers)
.ToList();

Having an issue getting an ActionResult that uses parameters to return specific queryable items

I am trying to get this Action Result to use the parameters given (Year, Qtr, Div) and only return those results. I use something similar to this in other ActionResults, but they do not return results in Json. I am not quite sure what I need to do in order to get this result to return only the results that match the parameters given. What it is doing is returning all records instead of filtering them by the parameters.
public ActionResult GLAcctsISDs_Read([DataSourceRequest] DataSourceRequest request, int? Year, int? Qtr, string Div)
{
var glacctsisds = db.GLAcctsISDs.AsQueryable();
if (Year.HasValue)
glacctsisds = glacctsisds.Where(x => x.Year == Year);
if (Qtr.HasValue)
glacctsisds = glacctsisds.Where(x => x.Qtr == Qtr);
if (!string.IsNullOrEmpty(Div))
glacctsisds = glacctsisds.Where(x => x.Div == Div);
DataSourceResult result = glacctsisds.ToDataSourceResult(request, gLAcctsISD => new {
ID = gLAcctsISD.ID,
Div = gLAcctsISD.Div,
Year = gLAcctsISD.Year,
Qtr = gLAcctsISD.Qtr,
GLAcct = gLAcctsISD.GLAcct,
GLDescr = gLAcctsISD.GLDescr,
TBDebit = gLAcctsISD.TBDebit,
TBCredit = gLAcctsISD.TBCredit,
IncExpAdjDebit = gLAcctsISD.IncExpAdjDebit,
IncExpAdjCredit = gLAcctsISD.IncExpAdjCredit,
AdlTrialDebit = gLAcctsISD.AdlTrialDebit,
AdjTrialCredit = gLAcctsISD.AdjTrialCredit,
RemainExpDebit = gLAcctsISD.RemainExpDebit,
PendingIncomeCredit = gLAcctsISD.PendingIncomeCredit,
ProjRemainExpDebit = gLAcctsISD.ProjRemainExpDebit,
NetIncomeCredit = gLAcctsISD.NetIncomeCredit,
DivGLNotes = gLAcctsISD.DivGLNotes
});
return Json(result);
}
Thanks in advance for the help.
try serializing "result" object to json.
string jsonString = Newtonsoft.Json.JsonConvert.SerializeObject(result);
return Json(jsonString);
For anyone who wants to know how to do this here you go:
Parameters are passed in to the controller as year, qtr, and div:
public ActionResult KenodGLAcctISDList( int? year, int? qtr, string div)
{
ViewBag.YYear = year;
ViewBag.QQtr = qtr;
ViewBag.DDiv = div;
return View();
}
public ActionResult GLAcctsISDs_Read([DataSourceRequest]DataSourceRequest request)
{
var glacctsisds = db.GLAcctsISDs.AsQueryable();
DataSourceResult result = glacctsisds.ToDataSourceResult(request, gLAcctsISD => new {
ID = gLAcctsISD.ID,
Div = gLAcctsISD.Div,
Year = gLAcctsISD.Year,
Qtr = gLAcctsISD.Qtr,
GLAcct = gLAcctsISD.GLAcct,
GLDescr = gLAcctsISD.GLDescr,
TBDebit = gLAcctsISD.TBDebit,
TBCredit = gLAcctsISD.TBCredit,
IncExpAdjDebit = gLAcctsISD.IncExpAdjDebit,
IncExpAdjCredit = gLAcctsISD.IncExpAdjCredit,
AdlTrialDebit = gLAcctsISD.AdlTrialDebit,
AdjTrialCredit = gLAcctsISD.AdjTrialCredit,
RemainExpDebit = gLAcctsISD.RemainExpDebit,
PendingIncomeCredit = gLAcctsISD.PendingIncomeCredit,
ProjRemainExpDebit = gLAcctsISD.ProjRemainExpDebit,
NetIncomeCredit = gLAcctsISD.NetIncomeCredit,
DivGLNotes = gLAcctsISD.DivGLNotes
});
return Json(result);
}
Then passed to the view as Viewbags and sorted by the KendoUI Grid code
.Read(read => read.Action("GLAcctsISDs_Read", "KendoGLAcctISD"))
.Filter(filter =>
{
filter.Add(f => f.Year).IsEqualTo(ViewBag.YYear);
filter.Add(f => f.Qtr).IsEqualTo(ViewBag.QQtr);
})
.Update(update => update.Action("GLAcctsISDs_Update", "KendoGLAcctISD"))

ASP.Net MVC 4 Kendo UI Scheduler multiple events get added when update event

I'm new to kendo Ui. I've used the scheduler control in my ASP.Net MVC 4 project and implemented using Ajax calls as defined in following tutorial.
http://docs.telerik.com/kendo-ui/getting-started/using-kendo-with/aspnet-mvc/helpers/scheduler/ajax-editing.
My problem is when I try to edit an event, it will edit the event, plus creates one or more duplicate records in the database with the same data. How should I avoid it.
Following is my Index.cshtml
#(Html.Kendo().Scheduler<Portal.Presentation.Web.BoundedContext.QC.MVC.Areas.Razor.Models.LeavePlan>()
.Name("scheduler")
.Date(DateTime.Now)
.Height(600)
.AllDaySlot(true)
.Views(views =>
{
views.DayView();
views.WeekView(weekView => weekView.Selected(true));
views.MonthView();
})
.Timezone("Etc/UTC")
.Resources(resource =>
{
resource.Add(m => m.PlanTypeId)
.Title("Plan Type")
.DataTextField("Text")
.DataValueField("Value")
.DataColorField("Color")
.BindTo(new[] {
new { Text = "Annual Leave", Value = 1, Color = "#f8a398" } ,
new { Text = "Casual Leave", Value = 2, Color = "#51a0ed" } ,
new { Text = "Sick Leave", Value = 2, Color = "#55a098" } ,
new { Text = "Travel", Value = 3, Color = "#56ca85" }
});
})
.DataSource(d => d
.Model(m => {
m.Id(f => f.TaskID);
m.Field(f => f.ResourceId).DefaultValue(1);
//Set the recurrence ID field from the model:
//m.RecurrenceId(f => f.RecurrenceID);
})
.Read("Tasks_Read", "LeavePlan")
.Create("Tasks_Create", "LeavePlan")
.Destroy("Tasks_Destroy", "LeavePlan")
.Update("Tasks_Update", "LeavePlan")
))
Following is my update method.
public ActionResult Tasks_Update([DataSourceRequest]DataSourceRequest request, LeavePlan leave)
{
if (ModelState.IsValid)
{
// Create a new Task entity and set its properties from the posted TaskViewModel
var entity = new ResourceLeavePlan
{
StartDate = leave.Start
,Title = leave.Title
,EndDate = leave.End.ToUniversalTime()
,ResourceLeavePlanId = (int)leave.TaskID
,IsAllDay=leave.IsAllDay
,RecurrenceId=leave.ResourceId
,Description=leave.Description
,RecurrenceException=leave.RecurrenceException
,LeaveTypeId=(int)leave.PlanTypeId
,ResourceId = resourceId
};
_resourceLeavePlanService.Update(entity);
}
return Json(new[] { leave }.ToDataSourceResult(request, ModelState));
}
And help on this would be appreciated.
The model is same as the model in the tutorial in the above link.
and also would be grateful if some one can explain me the use of "RecurrenceId" in the scheduler
You are just missing unique TaskID in your entity object.
var entity = new ResourceLeavePlan
{
TaskID = leave.TaskID // Just try adding this line please and it would work
,StartDate = leave.Start
,Title = leave.Title
,EndDate = leave.End.ToUniversalTime()
,ResourceLeavePlanId = (int)leave.TaskID
,IsAllDay=leave.IsAllDay
,RecurrenceId=leave.ResourceId
,Description=leave.Description
,RecurrenceException=leave.RecurrenceException
,LeaveTypeId=(int)leave.PlanTypeId
,ResourceId = resourceId
};

Some errors in controller (asp.net mvc)

I am getting some errors in my controller.
At first, I got Suppliers List, then I got Id for all Suppliers, then I got all Users for every Supplier.
public ActionResult Grid(bool? active)
{
var suppliers = Context.Suppliers.AsNoTracking()
.WhereIf(active != null, e => e.Active == active)
.Select(e => new SupplierRow
{
Id = e.Id,
FullName = e.FullName,
Active = e.Active,
Visits = e.Visits,
})
.ToList();
List<int> supplierIds = new List<int>();
foreach (SupplierRow sr in suppliers)
{
supplierIds.Add(sr.Id);
}
var users = Context.Users.AsNoTracking()
.Where(e => supplierIds.Contains(e.SupplierId))
.Select(e => new UserRow
{
Id = e.Id,
FullName = e.FullName,
Email = e.Email,
Name = e.Name,
Status = e.Status,
Role = e.Role,
SupplierId = e.SupplierId
}).toList();
foreach (UserRow ur in users)
{
foreach (SupplierRow sr in supplier)
{
if (ur.SupplierId == sr.Id)
{
sr.Users.Add(ur);
}
}
}
return PartialView("_Grid", suppliers);
}
here
and here
What's wrong with my code? How to fix that?
The problem is that you are trying to add Guid object to a collection that only accepts int values. Your User.SupplierId is an object of type Guid? (or Nullable<Guid>), while Supplier.Id is Guid. Fix the collection by declaring it as:
List<Guid> supplierIds = new List<Guid>();
Then in you code use:
foreach(SupplierRow sr in suppliers)
{
supplierIds.Add(sr.Id);
}
Do the same thing for users except that you will have to use SupplierId.HasValue and SupplierId.Value to check whether it has a value and to read the value. This is because it is declared as nullable Guid.

Resources