Angular + bind to navigation property - angularjs

my server side code
public class Person
{
public int PersonId { get; set; }
public string FirstName { get; set; }
}
public class Employee
{
public int EmployeeId { get; set; }
public string EmployeePhoto { get; set; }
public virtual Person Person { get; set; }
[ForeignKey("Person")]
public virtual int PersonId { get; set; } // relates Employee to Person
}
I use breeze for dataservice on the clinet
var employee = manager.createEntity('Employee');
var person = manager.createEntity('Person');
employee.person = person;
the I bind to my viewModel (angular controller property like this)
vm.employee = employee
and I try to access in html like this
<input ng-model="vm.employee.person.firstName" placeholder="First Name" id="firstName" />
but it doesnt workout...
Help Plz

Related

EF core order by navigation property

I have an entity relationship like this.
public class Provider
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public virtual ICollection<ProviderPod> ProviderPods { get; set; } = new List<ProviderPod>();
}
public class ProviderPod
{
public int Id { get; set; }
public int ProviderId { get; set; }
public int PodId { get; set; }
public virtual Provider Provider { get; set; }
public virtual Pod Pod { get; set; }
}
public class Pod
{
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<ProviderPod> ProviderPods { get; set; } = new List<ProviderPod>();
}
I need to order the 'Provider' entity by it's navigation property ProviderPods' "Name" separated by a comma. Something like this
IQueryable<Provider> entityQuery = context.Providers.AsQueryable();
//Need to enter Appropriate query below
entityQuery = entityQuery.OrderByDescending(x => string.Join(", ", x.ProviderPods.Select(y => y.Pod.Name)));
var list = entityQuery.Take(12).ToList();
What would be the best way to achieve this ordering?

How to access foreign key values on MVC view?

I'm having trouble accessing foreign key values in my view without using a partial.
I have tblProperty as Primary_Key and tblCustomer as foreign_key. I want to access the values of my foreign keys in my view but can't figure out why.
Model
public partial class tblProperty
{
public tblProperty()
{
this.Images = new HashSet<Image>();
this.tblCustomers = new HashSet<tblCustomer>();
}
public int propertyID { get; set; }
public string address { get; set; }
public string description { get; set; }
public virtual ICollection<Image> Images { get; set; }
public virtual ICollection<tblCustomer> tblCustomers { get; set; }
}
public partial class tblCustomer
{
public int customerID { get; set; }
public string name { get; set; }
public decimal contactNumber { get; set; }
public string notes { get; set; }
public Nullable<int> propertyID { get; set; }
public virtual tblProperty tblProperty { get; set; }
}
controller
public class propertyController : Controller
{
propertyDBEntities2 dc = new propertyDBEntities2();
public ActionResult List()
{
var properties = dc.tblProperties.Include(p => p.tblCustomers);
return View(properties.ToList());
}
public ActionResult Details(int id = 0)
{
var properties = dc.tblProperties.Include(p => p.tblCustomers);
tblProperty property = dc.tblProperties.Find(id);
tblCustomer customer = dc.tblCustomers.Find(id);
if (properties == null)
{
return HttpNotFound();
}
return View(dc.tblProperties.Find(id));
}
public ActionResult Create()
{
return View();
}
[HttpPost, ValidateAntiForgeryToken]
public ActionResult Create(tblProperty e)
{
if (ModelState.IsValid)
{
using (dc)
{
dc.tblProperties.Add(e);
dc.SaveChanges();
}
}
return RedirectToAction("List");
}
view
(like model.name is trying to access name from tblCustomer)
#model myProject.tblProperty
#Html.DisplayFor(model => model.name)
tblProperty doesnt have name.
I guess you need
#Html.DisplayFor(model => model.tblCustomer.name)
But just debug it or use intellisense
EDIT:
In my project I create a dtoClass Data Transfer Object
So for my avl class I have a dtoAvl
avl Class:
public partial class avl
{
public avl()
{
this.cars = new HashSet<cars>();
}
public long avl_id { get; set; }
public Nullable<long> car_id { get; set; }
public Nullable<decimal> speed { get; set; }
// this class contain info regarding the road
public virtual manila_rto manila_rto { get; set; }
public virtual ICollection<cars> cars { get; set; }
}
I create a dtoAvl
public class dtoAvl
{
public long Avl_ID { get; set; }
public long? Car_ID { get; set; }
public string RoadName { get; set; } // came from manila_rto
public int Speed { get; set; }
}
My controler
List<dtoAvl> result = db.avls.Select(
r => new dtoAvl
{
Avl_ID = r.Avl_ID,
Car_ID = r.Car_ID,
Speed = r.Speed,
// here is a propery but can be a list
RoadName = r.manila_rto.name
}).ToList();
return PartialView(result);
View:
#model IEnumerable<dtoAvl>

how to display data from both two tables in database in a View in mvc4 razor

Can anybody guide me how to display data from two tables in database in view page of MVC4 using razor?i googled it but i didnt find answer for this
LeadDetail.cs
public partial class LeadDetail
{
public int LeadID { get; set; }
public string LeadName { get; set; }
public virtual logintable logintable { get; set; }
}
EmployeDetail.cs
public partial class EmployeDetail
{
public int EmployeID { get; set; }
public int UserID { get; set; }
public string EmployeeName { get; set; }
public virtual logintable logintable { get; set; }
}
Parentview.cs in viewmodels folder
public class Parentview
{
public List<LeadDetail> LeadDetails { get; set; }
public List<EmployeDetail> EmployeDetails { get; set; }
public ParentsInformationViewModel(List<LeadDetail> _LeadDetails, List<EmployeDetail> _EmployeDetails) //Should i pass all the required parameters that i want to display in view ????
{
LeadDetails = _LeadDetails;
EmployeDetails = _EmployeDetails;
}
Homecontroller.cs
public ActionResult view()
{
List<LeadDetail> LeadObj = new List<LeadDetail> ();
List<EmployeDetail> EmployeObj = new List<EmployeDetail> ();
// get list of parents here
Parentview ParentInfoVMObj = new Parentview();
ParentInfoVMObj.LeadDetails = LeadObj;
ParentInfoVMObj.EmployeDetails = EmployeObj;
return View(ParentInfoVMObj);
}
see below sample
First Table
public class Table1
{
public int Id{ get; set; }
public string Name{ get; set; }
}
Second Table
public class Table2
{
public int Id{ get; set; }
public string Name{ get; set; }
}
ViewModel
public class ViewModelForTwoTables
{
public List<Table1> table1Data { get; set; }
public List<Table2> table2Data { get; set; }
}
see below example
public ActionResult TeamStat()
{
var players = db.Players().ToList();
var seasons = db.Seasons().ToList();
var view = new TeamStat()
{
Players = players,
Seasons = seasons
};
return View(view);
}
in view
#foreach (var player in Model.Players) { ....
#foreach (var player in Model.Seasons) { ....

Any obvious reason why my breeze entity child nodes won't expand?

I can't figure out why my child nodes are either null or have a count of 0, even though there's associated data in the db.
Parent Class "Project"
public partial class Project
{
public Project()
public int Id { get; set; }
public string Name { get; set; }
public int ProjectOwnerId { get; set; }
public int CurrentMilestoneId { get; set; }
public int StatusId { get; set; }
public virtual Milestone CurrentMilestone { get; set; }
public virtual ProjectStatus Status { get; set; }
public virtual ICollection<ProjectContact> Contacts { get; set; }
public virtual ProjectAddress Address { get; set; }
}
Child node property "CurrentMilestone" comes back null
public partial class Milestone
{
public int Id { get; set; }
public int MasterMilestoneId { get; set; }
public string Name { get; set; }
public virtual MasterMilestone MasterMilestone { get; set; }
public virtual ICollection<Project> Projects { get; set; }
}
Child node property "Contacts" comes back with an array of 0, even though there's valid matching data.
public partial class ProjectContact
{
public int Id { get; set; }
public int ProjectId { get; set; }
public int PersonId { get; set; }
public string Title { get; set; }
public virtual Person Person { get; set; }
public virtual Project Project { get; set; }
}
Using the HotTowel angular/breeze I'm running this..
return EntityQuery.from("Projects")
.orderBy(orderBy)
.expand("currentMilestone.masterMilestone, projectOwnerCompany, contacts, address")
.using(self.manager).execute()
.then(querySucceeded, self._queryFailed);
function querySucceeded(data) {
projects = data.results;
return projects;
}
Controller:
[HttpGet]
public IQueryable<Project> Projects()
{
return _contextProvider.QueryAllReadOnly<Project>();
}
What's weird is some of them work, like Status works but leaseStatus doesn't (not shown, setup the same way) without expanding it.
Just to add more info, if I run this against the API, ..../Projects?$expand=Status%2CCurrentMilestone%2CLeaseStatus%2CCurrentMilestone%2FMasterMilestone%2CContacts%2CContacts%2FPerson&
In Fiddler the child nodes aren't expanded.
Figured it out..
Change the Controller from ReadOnly to non-readonly.
From
[HttpGet]
public IQueryable<Project> Projects()
{
return _contextProvider.QueryAllReadOnly<Project>();
}
To
[HttpGet]
public IQueryable<Project> Projects()
{
return _contextProvider.QueryAll<Project>();
}

Classes and relationship design in mvc 3.0

I have currently have the following classes:
public class Ticket
{
public int Id { get; set; }
[Required]
public string Name { get; set; }
public int ClientId { get; set; }
public virtual Client Client { get; set; }
public virtual ICollection<Item> Items { get; set; }
public Ticket()
{
Items = new List<Item>();
}
}
public class Client
{
public int Id { get; set; }
[Required]
public string Name { get; set; }
public virtual ICollection<Ticket> Tickets { get; set; }
}
public class Item
{
public int Id { get; set; }
[Required]
public string Name { get; set; }
public double Price { get; set; }
public virtual ICollection<Ticket> Tickets { get; set; }
}
My Problem/Question is the following, let's say I create a new Item: "Item1" and set its price to "3.00", I add that Item to a few "Tickets", i.e., "Ticket1": "Item1", "3.00"; "Ticket2": "Item1", "3.00", etc. If after adding these items to the tickets I change "Item1"'s price to "4.00" it would change the price of the ticket I already created, how can I have it change the price for only ticket created after the price change?
This is my ticket controller:
[HttpPost]
public ActionResult Create(TicketViewModel ticketViewModel)
{
if (ModelState.IsValid)
{
var ticket = new Ticket();
ticket = ticketViewModel.Ticket;
AddOrUpdateItems(ticket, ticketViewModel.Item);
context.Tickets.Add(ticket);
context.SaveChanges();
return RedirectToAction("Index");
}
return View(ticketViewModel);
}
private void AddOrUpdateItems(Ticket ticket, ICollection<AssignedItem> assignedItems)
{
foreach (var assignedItem in assignedItems)
{
if (assignedItem.Assigned)
{
var item = context.Items.Find(assignedItem.ItemId);
ticket.Items.Add(item);
}
}
}

Resources