Binding pivot query to view in ASP.Net MVC - asp.net-mvc

I'm sticking on how to best present some data that's being dynamically generated from two different tables.
Given my query:
var assets = assetRepo.Find(x => x.LoginId == User.Identity.Name);
var accounts = repository.Find(x => x.AccStatus == "A" && x.LoginId == User.Identity.Name);
var query = from asst in assets
join acct in accounts on asst.AccountId equals acct.AccountId
select new
{
Account = acct.AccountNumber,
Status = acct.AccStatus,
Make = asst.Make,
Model = asst.Model,
Submodel = asst.SubModel,
Registration = asst.Registration,
Balance = acct.BalanceOutstanding,
NextPayment = acct.NextPayment,
Date = String.Format("{0:dd MMM yyyy}", acct.NextPaymentDate),
Due = acct.ArrearsBal
};
What would be the best (i.e. cleanest) way to bind this to the view? Would a custom class be required or is there a way to specify and iterate over a collection of anonymous types?

Creating custom class can give you additional benefits. You can use DisplayAttribute to set column headers and order. Then you can create view (or template to use with DisplayFor) that takes list of objects of any type and uses reflection to read annotations and display view nicely.
class Report {
[Display(Name="Account",Order=1)]
public string Account {get; set;}
[Display(Name="Next payment",Order=2)]
public Date NextPayment {get; set;}
}
It looks also clean. You will be able to use this annotations not only for grid, but also for excel exports or other data operations.

Related

Trying to use Entity Framework method for multiple adds to database

I have an ASP.NET MVC app using Entity Framework from our SQL Server backend.
Goal is to create ~18 WPackage entries via a foreach loop:
foreach (var dbitem in dbCList)
The code works for a single WPackage entry, but we have a request from the customer to create 300+ WPackages, so trying to use the Entity Framework code for a single "Add" and loop to create 300+ adds.
The T-SQL would be very challenging as there are many keys created on the fly/at row creation, so for activities >> resources, we'd have to insert the activity, grab or remember the activity key, then add resources with that newly created activity key.
Each WPackage (this is the main parent table) could have one or more of the following child table entries:
1+ activities
each activity would have 1+ resource
1+ budgets
1+ Signatures
1+ CostCodes
Our schema or model diagram would be:
WPackage
--Activities
-----Resources (child of Activities)
--CostCodes
--Budgets
--Signatures
The following code fails on:
dbContextTransaction.Commit();
with an error:
The transaction operation cannot be performed because there are pending requests working on this transaction.
[HttpPost]
public ActionResult Copy([Bind(Include = "ID,WBSID,...***fields excluded for brevity")] Package model)
{
if (ModelState.IsValid)
{
try
{
using (var dbContextTransaction = db.Database.BeginTransaction())
{
var dbCList = db.Packages.Join(db.WBS,
*expression omitted for brevity*)
// this dbClist will build about 18 items in the collection for below loop
foreach (var dbitem in dbCList)
{
int testWPID = dbitem;
WPackage prvWP = db.WPackages.Find(dbitem);
int previousWPID = dbitem;
WPackage previousWP = db.WPackages.Find(dbitem);
model.ID = dbitem;
db.WPackages.Add(model);
db.SaveChanges();
var budgets = db.Budgets.Where(i => i.WPID == previousWPID);
foreach (Budget budget in budgets)
{
budget.WPID = model.ID;
db.Budgets.Add(budget);
}
var costCodes = db.CostCodes.Where(i => i.WPID == previousWPID);
foreach (CostCode costCode in costCodes)
{
costCode.WPID = model.ID;
db.CostCodes.Add(costCode);
}
var activities = db.Activities.Where(i => i.WPID == previousWPID);
// *code excluded for brevity*
var previousActivityID = activity.ID;
db.Activities.Add(activity);
db.SaveChanges();
var resources = db.Resources.Where(i => i.ActivityID == previousActivityID);
foreach (Resource resource in resources)
{
resource.WPID = model.ID;
resource.ActivityID = activity.ID;
resource.ActivityNumber = activity.ActivityNumber;
db.Resources.Add(resource);
db.SaveChanges();
}
}
var signatures = db.RolesAndSigs
.Where(i => i.KeyId == previousWPID && i.Type == "WPL")
.OrderBy(i => i.Role)
.OrderBy(i => i.Person);
foreach (RolesAndSig signature in signatures)
{
db.RolesAndSigss.Add(signature);
}
db.SaveChanges();
dbContextTransaction.Commit();
}
}
}
}
I've also tried to have the Commit() run outside the foreach dbitem loop like:
db.SaveChanges();
//dbContextTransaction.Commit();
}
dbContextTransaction.Commit();
...but this returns error of:
[EXCEPTION] The property 'ID' is part of the object's key information and cannot be modified.
The code you posted has some issues that don't make sense, and probably aren't doing what you think they are doing. The crux of the issue you are facing is that Entity Framework tracks all references to entities it loads and associates:
Firstly this code:
int testWPID = dbitem;
WPackage prvWP = db.WPackages.Find(dbitem);
int previousWPID = dbitem;
WPackage previousWP = db.WPackages.Find(dbitem);
prvWP and previousWP will be pointing to the exact same reference, not two copies of the same entity. Be careful when updating either or any other reference retrieved or associated with that same ID. They all point to the same instance. If you do want a stand-alone snaphot reference you can use AsNoTracking().
Next, when you do something like this in a loop:
model.ID = dbitem;
db.WPackages.Add(model);
In the first iteration, "model" is not an entity. It is a deserialized block of data with the Type of the Package entity. As soon as you call .Add(model) that reference will now be pointing to a newly tracked entity reference. In the next loop you are telling EF to change that tracked entity reference's ID to a new value, and that is illegal.
What it looks like you want to do is create a copy of this model for each of the 18 expected iterations. For that what you want to do would be something more like:
foreach (var dbitem in dbCList)
{
var newModel = new WPackage
{
ID = dbItem,
WBSID = model.WBSID,
/// copy across all relevant fields from the passed in model.
};
db.WPackages.Add(newModel);
// ...
}
It would be quite worthwhile to leverage navigation properties for the related entities rather than using explicit joins and trying to scope everything in an explicit transaction with multiple SaveChanges() calls. EF can manage all of the FKs automatically rather than essentially using it as a wrapper for individual ADO CRUD operations.
You will need to be explicit between when you want to "clone" an object reference vs. "copy" a reference. For example, if I have a Customer that has an Address, and Addresses have a Country reference, when I clone a Customer, I will want to clone a new Address record for that Customer, however ensure that the Country reference is copied across. If I have a record for Jack at an 123 Apple Street, London in England, and go to clone Jack to make a record for Jill at the same address, they might be at the same location now, but not always, so I want them to point at different Address records in case Jill moves out. Still, there should only be one record for "England". (Jill may move to a different country, but her address record would just point at a different Country Id)
Wrong:
var jill = context.Customers.Single(c => c.Name == "Jack");
jill.Name = "Jill";
context.Customers.Add(jill);
This would attempt to rename Jack into Jill, then "Add" the already tracked instance, resulting in an exception.
Will work, but still Wrong:
var jack = context.Customers.AsNoTracking().Single(c => c.Name == "Jack");
var jill = jack;
jill.Name = "Jill";
context.Customers.Add(jill);
This would technically work by loading Jack as an untracked entity, and would save Jill as a new record with a new Id. However this is potentially very confusing. Depending on how the AddressId/Address is referenced we could end up with Jack and Jill referencing the same single Address record. Bad if you want Jack and Jill to have different addresses.
Right:
var jack = context.Customers
.Include(c => c.Address)
.ThenInclude(a => a.Country)
.Single(c => c.Name == "Jack");
var jill = new Customer
{
Name = "Jill",
// copy other fields...
Address = new Address
{
StreetNumber = jack.Address.StreetNumber,
StreetName = jack.Address.StreetName,
Country = jack.Address.Country
}
};
context.Customers.Add(jill);
The first detail is to ensure when we load Jack that we eager load all of the related details we will want to clone or copy references to. We then create a new instance for Jill, copying the values from Jack, including setting up a new Address record. The Country reference is copied across as there should only be ever a single record for "England".
Edit: For something like a roll-over scenario if you have a package by year, let's use the example of a Package class below:
public class Package
{
[Key]
public int PackageId { get; set; }
[ForeignKey("PackageType")]
public int PackageTypeId { get; set; }
public int Year { get; set; }
// .. More package related details and relationships...
public virtual PackageType PackageType { get; set; }
}
A goal might be to make a new Package and related data for Year 2022 from the data from 2021, and apply any changes from a view model passed in.
Find is a poor choice for this because Find wants to locate data by PK. If you're method simply passes an entity to be copied from (I.e. the data from 2021) then this can work, however if you have modified that data from 2021 to represent values you want for 2022 that could be dangerous or misleading within the code. (We don't want to update 2021's data, we want to create a new record set for 2022) To make a new Package for 2022 we just need the updated data to make up that new item, and a way to identify a source for what to use as a template. That identification could be the PK of the row to copy from (ProductId), or derived from the data passed in. (ProductTypeId, and Year-1) In both cases if we want to consider related data with the "copy from" product then it would be prudent to eager load that related data in one query rather than going back to the database repeatedly. Find cannot accommodate that.
For instance if I want to pass data to make a new product I pass a ProductTypeId, and a Year along with any values to use for the new structure. I can attempt to get a copy of the existing year to use as a template via:
var existingProduct = context.Products
.Include(x => x.Activities) // Eager load related data.
.Include(x => x.CostCodes)
// ...
.Single(x => x.ProductTypeId == productTypeId && x.Year = year - 1);
or if I passed a ProductId: (such as if I could choose to copy the data from a selected year like 2020 instead)
var existingProduct = context.Products
.Include(x => x.Activities)
.Include(x => x.CostCodes)
// ...
.Single(x => x.ProductId == copyFromProductId);
Both of these examples expect to find one, and only one existing product. If the request comes in with values that it cannot find a row for, there would be an exception which should be handled. This would fetch all of the existing product information that we can copy from, alongside any data that was passed into the method to create a new Product.

Multiple Databases in ASP.NET MVC

I'm very first experience in ASP.NET MVC and after I read through a few articles I decided I try my new project with MVC.
And I used ADO.net entity data model and I created Create/ Delete/ Details/ Edit/ Index. It works fine.
So I plan to improve in user interface because some of the fields are comes from another databases such as HR Database, etc... for employee information.
for e.g : to choose Employee's Name on my form, I have to use DropDownList and that data comes from another database, HR as I mentioned in above. I have no idea how to access difference database in one model and here I asked How to solve multiple databases in one edmx for ASP.net MVC?
However I tried to create one more model for the other database and try to join with Linq.
//
// GET: /ARS/
public ActionResult Index()
{
var employee = new List<EMPLOYEE>(chr.EMPLOYEEs);
var reqform = new List<RequestForm>(ars.RequestForms.Include(r => r.Genre));
var requestforms = from r in reqform
join e in employee on r.PrjDeptMgr equals e.EmployeeID
select new
{
r.FormID,
r.GenreID,
r.JobNo,
r.Description,
r.StartDate,
r.EndDate,
r.PrjDeptMgr,
r.IsAgreed,
r.Genre
};
//var requestforms = ars.RequestForms.Include(r => r.Genre);
return View(requestforms.ToList());
}
But I got this error
The model item passed into the dictionary is of type 'System.Collections.Generic.List1[<>f__AnonymousType29[System.Int32,System.Int32,System.String,System.String,System.DateTime,System.DateTime,System.String,System.Boolean,Access_Request_System.Models.Genre]]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[Access_Request_System.Models.RequestForm]'.
Really no idea for this case... Please!
Your view file requires IEnumerable<RequestForm>, but the type you're passing does not match the required type (it's anonymous).
Try the following changes.
List<RequestForm> requestforms = (from r in reqform
join e in employee on r.PrjDeptMgr equals e.EmployeeID
select new RequestForm
{
FormID = r.FormID,
Genre = r.GenreID,
JobNo = r.JobNo,
Description = r.Description,
StartDate = r.StartDate,
EndDate = r.EndDate,
PrjDeptMgr = r.PrjDeptMgr,
IsAgreed = r.IsAgreed,
Genre = r.Genre
}).ToList();
return View(requestForms);

How to join multiple tables using LINQ-to-SQL?

I'm quite new to linq, so please bear with me.
I'm working on a asp.net webpage and I want to add a "search function" (textbox where user inputs name or surname or both or just parts of it and gets back all related information). I have two tables ("Person" and "Application") and I want to display some columns from Person (name and surname) and some from Application (score, position,...). I know how I could do it using sql, but I want to learn more about linq and thus I want to do it using linq.
For now I got two main ideas:
1.)
var person = dataContext.GetTable<Person>();
var application = dataContext.GetTable<Application>();
var p1 = from p in Person
where(p.Name.Contains(tokens[0]) || p.Surname.Contains(tokens[1]))
select new {Id = p.Id, Name = p.Name, Surname = p.Surname}; //or maybe without this line
//I don't know how to do the following properly
var result = from a in Application
where a.FK_Application.Equals(index) //just to get the "right" type of application
//this is not right, but I don't know how to do it better
join p1
on p1.Id == a.FK_Person
2.) The other idea is just to go through "Application" and instead of "join p1 ..." to use
var result = from a in Application
where a.FK_Application.Equals(index) //just to get the "right" type of application
join p from Person
on p.Id == a.FK_Person
where p.Name.Contains(tokens[0]) || p.Surname.Contains(tokens[1])
I think that first idea is better for queries without the first "where" condition, which I also intended to use. Regardless of what is better (faster), I still don't know how to do it using linq. Also in the end I wanted to display / select just some parts (columns) of the result (joined tables + filtering conditions).
I really want to know how to do such things using linq as I'll be dealing also with some similar problems with local data, where I can use only linq.
Could somebody please explain me how to do it, I spent days trying to figure it out and searching on the Internet for answers.
var result = from a in dataContext.Applications
join p in dataContext.Persons
on p.Id equals a.FK_Person
where (p.Name.Contains("blah") || p.Surname.Contains("foo")) && a.FK_Application == index
select new { Id = p.Id, Name = p.Name, Surname = p.Surname, a.Score, a.Position };
Well as Odrahn pointed out, this will give you flat results, with possibly many rows for a single person, since a person could join on multiple applications that all have the same FK. Here's a way to search all the right people, and then add on the relevant application to the results:
var p1 = from p in dataContext.Persons
where(p.Name.Contains(tokens[0]) || p.Surname.Contains(tokens[1]))
select new {
Id = p.Id, Name = p.Name, Surname = p.Surname,
BestApplication = dataContext.Applications.FirstOrDefault(a => a.FK_Application == index /* && ???? */);
};
Sorry - it looks like this second query will result in a roundtrip per person, so it clearly won't be scalable. I assumed L2S would handle it better.
In order to answer this properly, I need to know if Application and Person are directly related (i.e. does Person have many Applications)? From reading your post, I'm assuming that they are because Application seems to have a foreign key to person.
If so, then you could create a custom PersonModel which will be populated by the fields you need from the different entities like this:
class PersonModel
{
string Name { get; set; }
string Surname { get; set; }
List<int> Scores { get; set; }
List<int> Positions { get; set; }
}
Then to populate it, you'd do the following:
// Select the correct person based on Name and Surname inputs
var person = dataContext.Persons.Where(p => p.Name.Contains("firstname") || p.Name.Contains("surname")).FirstOrDefault();
// Get the first person we find (note, there may be many - do you need to account for this?)
if (person != null)
{
var scores = new List<int>();
var positions = new List<int>();
scores.AddRange(person.Applications.Select(i => i.Score);
positions.AddRange(person.Applications.Select(i => i.Position);
var personModel = new PersonModel
{
Name = person.Name,
Surname = person.Surname,
Scores = scores,
Positions = positions
};
}
Because of your relationship between Person and Application, where a person can have many applications, I've had to account for the possibility of there being many scores and positions (hence the List).
Also note that I've used lambda expressions instead of plain linqToSql for simple selecting so that you can visualise easily what's going on.

Asp.Net MVC 2 Dropdown Displaying System.Web.MVC.SelectListItem

I have a table that contains a list of EquipmentIDs and another table that has maintenance records.
When the user edits a maintenance record I want there to be a drop down list of all of the equipment IDs from the table.
The dropdown list populates, and it populates with the correct amount of entries, however they all say System.Web.MVC.SelectListItem instead of the value of the ID.
Here is the code that generates the list:
public ActionResult Edit(int id)
{
MaintPerformed maintPerformed = maintPerformedRepository.GetMaintPerformed(id);
IList<EquipmentID> IDs = equipmentIDRepository.GetEquipmentIDAsList();
IEnumerable<SelectListItem> selectEquipList =
from c in IDs
select new SelectListItem
{
//Selected = (c.EquipID == maintPerformed.EquipID),
Text = c.EquipID,
Value = c.Sort.ToString()
};
ViewData["EquipIDs"] = new SelectList(selectEquipList, maintPerformed.ID);
return View(maintPerformed);
}
Here is the entry in the .aspx page for the Dropdown list:
%: Html.DropDownList("EquipIDs") %>
Here is how I am generating the list from the table:
public List<EquipmentID> GetEquipmentIDAsList()
{
return db.EquipmentIDs.ToList();
}
It appears that everything is working correctly with the exception of assigning the text to be displayed in the drop down box.
What am I missing or not thinking correctly about?
SelectList and SelectListItem are actually mutually exclusive. You should be using one or the other. Etiher pass the constructor of SelectList your raw data (IDs) or don't use SelectList at all and just make ViewData["EquipIDs"] your enumerable of SelectListItem. If you go with the latter approach, you will have to tweak your code so that you are setting the selected item in the constructor of SelectListItem (as you had done, but commented out).
Either:
ViewData["EquipIDs"] = new SelectList(IDs, maintPerformed.ID, "EquipID", "Sort");
Or:
IEnumerable<SelectListItem> selectEquipList =
from c in IDs
select new SelectListItem
{
Selected = c.EquipID == maintPerformed.EquipID,
Text = c.EquipID,
Value = c.Sort.ToString()
};
ViewData["EquipIDs"] = selectEquipList;

Entity Framework - Select specific columns and return strongly typed without losing cast

I'm trying to do something similar to this post where I don't pull back all columns from a particular entity, however my framework makes use of inheritence and I lose scope of the entity type after it's been cast to an anonymous type.
The structure of my Entity Framework has a base entity called Action. From here I've created two inherited entities called Event and Activity. I want to pull back the last X Actions and pass them to my strongly typed view which accepts an Action and from there determines if its an Activity or Event and renders the correct partial view.
if(Model.GetType() == typeof(Event))
{
//render Event view
}
else if(Model.GetType() == typeof(Activity))
{
//render Activity view
}
I can pull the last 10 as an anonymous type and then cast:
var result = from a in new DataContext().Actions
where a.UserId == someGuidValue
select new { a.CreatedOn, a.Summary };
List<Action> list = result.AsEnumerable()
.Select(o => new Action {
CreatedOn = o.CreatedOn,
Summary = o.Summary
}).ToList();
However, once I pass the new List of Actions to my strongly typed view it loses scope of whether it's an Activity or an Event since it's been cast as an Action. My question is, without exposing the discriminator column, is there any way to cast each item to the proper type or am I going about this the wrong way?
A bit kludgy, but will work:
var result = from a in new DataContext().Actions
where a.UserId == someGuidValue
let IsEvent = a as Event != null
select new { a.CreatedOn, IsEvent, a.Summary };
List<Action> list = result.AsEnumerable()
.Select(o => o.IsEvent ?
(Action) new Event {
CreatedOn = o.CreatedOn,
Summary = o.Summary
}
: (Action) new Activity {
CreatedOn = o.CreatedOn,
Summary = o.Summary
}
}).ToList();
Example with type-specific columns, presuming that e.EventSpecific is of a nullable type.
var result = from a in new DataContext().Actions
where a.UserId == someGuidValue
let ev = a as Event
let IsEvent = ev != null
select new { a.CreatedOn, IsEvent, a.Summary, ev.EventSpecific };
List<Action> list = result.AsEnumerable()
.Select(o => o.IsEvent ?
(Action) new Event {
CreatedOn = o.CreatedOn,
Summary = o.Summary,
EventSpecific = o.EventSpecific
}
: (Action) new Activity {
CreatedOn = o.CreatedOn,
Summary = o.Summary,
EventSpecific = o.EventSpecific // will be null, but using o.EventSpecific saves casting
}
}).ToList();
If o.EventSpecific is of a non-nullable type, then you must convert it to a nullable type in the L2E query.
You are probably on the wrong way. At first I would assume that Action should be an abstract class and you should not be able to create instances of it at all. If you then only fetch a subset of the properties and the subset does no longer allow to discriminate between events and activities, it is probably the wrong way to try making events and activities out of them.
So it actually seems not to be a technical problem - it should be quite easy to include some discrimination information in the anonymous type - but a design problem. I suggest to rethink if it is required to discriminate the query result and if so if it is really a good idea to discriminate the result in absence of an discriminator.

Resources