Form with list of lists - c#

I have been struggling with what seems to me to be a simple thing. I need to create a form with list of lists.
This is the model that I want to make a form of:
public OrderViewModel()
{
DeliveryAddress = new ProjectDeliveryAddressViewModel();
OrderItems = new List<OrderItemView>();
}
public int ProjectDeliveryId { get; set; }
public long ProjectId { get; set; }
[Display(ResourceType = typeof(Res.ViewModels.Common), Name = "CustomerOrderNumber")]
public string CustomerOrderNumber { get; set; }
[Display(ResourceType = typeof(Res.ViewModels.Common), Name = "DeliveryDate")]
public string RequestedDeliveryDate { get; set; }
public ProjectDeliveryAddressViewModel DeliveryAddress { get; set; }
public IList<OrderItemView> OrderItems { get; set; }
}
public class OrderItemView
{
public OrderItemView()
{
Additionalitems = new List<AdditionalItem>();
}
public long ProjectItemId { get; set; }
public string ItemName { get; set; }
public string ImageData { get; set; }
public int Quantity { get; set; }
public decimal Price { get; set; }
public string AaCompany { get; set; }
public string SupplierName { get; set; }
public int SupplierModelId { get; set; }
public IList<AdditionalItem> Additionalitems { get; set; }
}
public class AdditionalItem
{
public string Name { get; set; }
public string Id { get; set; }
public bool Value { get; set; }
public decimal Price { get; set; }
public int Quantity { get; set; }
}
What is the simplest way of doing this so that I can easily post the form to my mvc controller later on?

Related

How to use .length of an array value

I have a variable result which contains a JSON object:
result = JsonConvert.DeserializeObject<PullRequestModel>(responseData);
I would like to use for loop over result as:
#for (var key = 0; key < result.value.length; key++)
{
}
But C# thinks .length is part of the PullRequestModel, and the code is not working.
How do I get around this problem?
This is the PullRequestModel
namespace TestApp.Model.PullRequestModel
{
public class Avatar
{
public string href { get; set; }
}
public class CompletionOptions
{
public string mergeCommitMessage { get; set; }
public bool deleteSourceBranch { get; set; }
public bool squashMerge { get; set; }
public string mergeStrategy { get; set; }
public bool triggeredByAutoComplete { get; set; }
public List<object> autoCompleteIgnoreConfigIds { get; set; }
}
public class CreatedBy
{
public string displayName { get; set; }
public string url { get; set; }
public Links _links { get; set; }
public string id { get; set; }
public string uniqueName { get; set; }
public string imageUrl { get; set; }
public string descriptor { get; set; }
public string href { get; set; }
}
public class Iterations
{
public string href { get; set; }
}
public class LastMergeCommit
{
public string commitId { get; set; }
public string url { get; set; }
}
public class LastMergeSourceCommit
{
public string commitId { get; set; }
public string url { get; set; }
}
public class LastMergeTargetCommit
{
public string commitId { get; set; }
public string url { get; set; }
}
public class Links
{
public Avatar avatar { get; set; }
public Self self { get; set; }
public Repository repository { get; set; }
public WorkItems workItems { get; set; }
public SourceBranch sourceBranch { get; set; }
public TargetBranch targetBranch { get; set; }
public Statuses statuses { get; set; }
public SourceCommit sourceCommit { get; set; }
public TargetCommit targetCommit { get; set; }
public MergeCommit mergeCommit { get; set; }
public CreatedBy createdBy { get; set; }
public Iterations iterations { get; set; }
}
public class MergeCommit
{
public string href { get; set; }
}
public class Project
{
public string id { get; set; }
public string name { get; set; }
public string state { get; set; }
public string visibility { get; set; }
public DateTime lastUpdateTime { get; set; }
}
public class Repository
{
public string id { get; set; }
public string name { get; set; }
public string url { get; set; }
public Project project { get; set; }
public string href { get; set; }
}
public class Reviewer
{
public string reviewerUrl { get; set; }
public int vote { get; set; }
public bool hasDeclined { get; set; }
public bool isRequired { get; set; }
public bool isFlagged { get; set; }
public string displayName { get; set; }
public string url { get; set; }
public Links _links { get; set; }
public string id { get; set; }
public string uniqueName { get; set; }
public string imageUrl { get; set; }
public bool isContainer { get; set; }
public List<VotedFor> votedFor { get; set; }
}
public class PullRequestModel
{
public List<Value> value { get; set; }
public int count { get; set; }
}
public class Self
{
public string href { get; set; }
}
public class SourceBranch
{
public string href { get; set; }
}
public class SourceCommit
{
public string href { get; set; }
}
public class Statuses
{
public string href { get; set; }
}
public class TargetBranch
{
public string href { get; set; }
}
public class TargetCommit
{
public string href { get; set; }
}
public class Value
{
public Repository repository { get; set; }
public int pullRequestId { get; set; }
public int codeReviewId { get; set; }
public string status { get; set; }
public CreatedBy createdBy { get; set; }
public DateTime creationDate { get; set; }
public DateTime closedDate { get; set; }
public string title { get; set; }
public string description { get; set; }
public string sourceRefName { get; set; }
public string targetRefName { get; set; }
public string mergeStatus { get; set; }
public bool isDraft { get; set; }
public string mergeId { get; set; }
public LastMergeSourceCommit lastMergeSourceCommit { get; set; }
public LastMergeTargetCommit lastMergeTargetCommit { get; set; }
public LastMergeCommit lastMergeCommit { get; set; }
public List<Reviewer> reviewers { get; set; }
public List<object> labels { get; set; }
public string url { get; set; }
public Links _links { get; set; }
public CompletionOptions completionOptions { get; set; }
public bool supportsIterations { get; set; }
public DateTime completionQueueTime { get; set; }
}
public class VotedFor
{
public string reviewerUrl { get; set; }
public int vote { get; set; }
public string displayName { get; set; }
public string url { get; set; }
public Links _links { get; set; }
public string id { get; set; }
public string uniqueName { get; set; }
public string imageUrl { get; set; }
public bool isContainer { get; set; }
}
public class WorkItems
{
public string href { get; set; }
}
}
This is the cause of the whole problem, if anyone is curious:
#if(result == null)
{
<p> No pull requests found</p>
}
else
{
#foreach (var item in result.value)
{
<div class="accordion">
<input type="checkbox" id="tab1">
<label class="accordion-label" for="tab1"> #item.title </label>
<div class="accordion-content">
<WorkItems PersonalAccessToken="#PersonalAccessToken" WorkItemUrl="#item._links.workItems.href"></WorkItems>
</div>
</div>
}
}
The above code works (it's a Blazor app) I loop trough all Pull requests and show associated Work Items for the PR.
But In order to crete a simple accordion, the I must increment id="tab1"> because in each loop, so my idea was to ditch #foreach and use #for loop, so I can make use of the key to increment the value of id="tab1">
Hope it makes sense
result = JsonConvert.DeserializeObject<PullRequestModel>(responseData);
#for (var key = 0; key < result.value.Count; key++)
{
}
List<> exposes the Count property not Length property.
It would appear you are deserializing a single object and not an enumerable type. Please try using the following if you want to return something to loop through:
result = JsonConvert.DeserializeObject<List<PullRequestModel>>(responseData);
#for (var key = 0; key < result.Count; key++)
{
}
Count is a property of of the List object in this case.

Including foreign key values into a DTO for a single record

It's been a while since I have done this, but I know there is an easy way to do this that I have forgotten. Below I have a class designed to populate a single record of a data object. But I cannot get the values from another table (related by foreign key) to populate using the lambda statement because I am missing something (the two values being pulled in from another table below can be seen as dto.LeaseName and dto.CarName). How should I write the lambda for the object dm?
public StationUnloadingLogDTO GetSingleRecordforLog(int Id)
{
StationUnloadingLogDTO dto = new StationUnloadingLogDTO();
StationUnloadingLog dm = new StationUnloadingLog();
dm = entity.StationUnloadingLog
.Where(x => x.Id == Id)
.FirstOrDefault();
dto.Id = dm.Id;
dto.DateLogged = dm.DateLogged;
dto.DriverName = dm.DriverName;
dto.TruckNumber = dm.TruckNumber;
dto.CarName = dm.Carrier.CarName;
dto.CarrierId = dm.CarrierId;
dto.SpecificGravity = dm.SpecificGravity;
dto.LactMeterOpen = dm.LactMeterOpen;
dto.LactMeterClose = dm.LactMeterClose;
dto.EstimatedBarrels = dm.EstimatedBarrels;
dto.TicketNumber = dm.TicketNumber;
dto.LeaseNumber = dm.LeaseNumber;
dto.LeaseName = dm.Company.CmpName;
dto.StationId = dm.StationId;
return dto;
}
Here are the related data classes
namespace Data.Models
{
public partial class Company
{
public Company()
{
StationUnloadingLog = new HashSet<StationUnloadingLog>();
}
public string CmpId { get; set; }
public string CmpName { get; set; }
public string CmpAddress1 { get; set; }
public string CmpAddress2 { get; set; }
public int? CmpCity { get; set; }
public string CmpZip { get; set; }
public string CmpPrimaryphone { get; set; }
public ICollection<StationUnloadingLog> StationUnloadingLog { get; set; }
}
public class StationUnloadingLogDTO
{
public int Id { get; set; }
public DateTime? DateLogged { get; set; }
public string DriverName { get; set; }
public string TruckNumber { get; set; }
public string CarrierId { get; set; }
public string CarName { get; set; }
public decimal? SpecificGravity { get; set; }
public decimal? LactMeterOpen { get; set; }
public decimal? LactMeterClose { get; set; }
public int? EstimatedBarrels { get; set; }
public string TicketNumber { get; set; }
public string LeaseName { get; set; }
public string LeaseNumber { get; set; }
public string StationId { get; set; }
}
public partial class StationUnloadingLog
{
public int Id { get; set; }
public DateTime? DateLogged { get; set; }
public string DriverName { get; set; }
public string TruckNumber { get; set; }
public string CarrierId { get; set; }
public decimal? SpecificGravity { get; set; }
public decimal? LactMeterOpen { get; set; }
public decimal? LactMeterClose { get; set; }
public int? EstimatedBarrels { get; set; }
public string TicketNumber { get; set; }
public string LeaseNumber { get; set; }
public string StationId { get; set; }
public Carrier Carrier { get; set; }
public Company Company { get; set; }
public Tractorprofile Tractorprofile { get; set; }
}
public partial class Carrier
{
public Carrier()
{
StationUnloadingLog = new HashSet<StationUnloadingLog>();
}
public string CarId { get; set; }
public string CarName { get; set; }
public string CarAddress1 { get; set; }
public string CarAddress2 { get; set; }
public int? CtyCode { get; set; }
public string CarZip { get; set; }
public string CarContact { get; set; }
public ICollection<StationUnloadingLog> StationUnloadingLog { get; set; }
}
You should query for your record with child entities like this.
dm = DbSet<StationUnloadingLog>
.Where(x => x.Id == Id).Include(x => x.Carrrier)
.FirstOrDefault();

Entity Framework creates a new record per foreign key in another table while seeding

I'm trying to seed a database using EF.
I have a table that holds products (phones) and a Category table that differentiates between different types of products.
public class Category
{
public int CategoryId { get; set; }
public string Name { get; set; }
public DateTimeOffset? CreationDate { get; set; }
public DateTimeOffset? UpdateDate { get; set; }
public virtual List<IProduct> Products{ get; set; }
public Category()
{
this.CreationDate = DateTimeOffset.UtcNow;
}
}
public interface IProduct
{
int ProductId { get; set; }
string Brand { get; set; }
string Model { get; set; }
decimal? Price { get; set; }
string Image { get; set; }
int CategoryId { get; set; }
Category Category { get; set; }
}
public class Phone: IProduct
{
public int ProductId { get; set; }
public int CategoryId { get; set; }
public Category Category { get; set; }
public string Brand { get; set; }
public string Model { get; set; }
public string network_technology { get; set; }
public string bands_2G { get;set; }
public string bands_3G{ get; set; }
public string bands_4G { get; set; }
public string network_speed { get; set; }
public string GPRS { get; set; }
public string EDGE { get; set; }
public string announced { get; set; }
public string status { get; set; }
public string dimentions { get; set; }
public float? weight_g { get; set; }
public float? weight_oz { get; set; }
public string SIM { get; set; }
public string display_type { get; set; }
public string display_resolution { get; set; }
public string display_size { get; set; }
public string OS { get; set; }
public string CPU { get; set; }
public string Chipset { get; set; }
public string GPU { get; set; }
public string memory_card { get; set; }
public string internal_memory { get; set; }
public string RAM { get; set; }
public string primary_camera { get; set; }
public string secondary_camera { get; set; }
public string loud_speaker { get; set; }
public string audio_jack { get; set; }
public string WLAN { get; set; }
public string bluetooth { get; set; }
public string GPS { get; set; }
public string NFC { get; set; }
public string radio { get; set; }
public string USB { get; set; }
public string sensors { get; set; }
public string battery { get; set; }
public string colors { get; set; }
public decimal? Price { get; set; }
public string Image { get; set; }
}
I don't know what am I doing wrong but after I update the database from nuget console, a new Category record is created per seeded product(phone). That's exactly the opposite of what I want. I want all the phones to have one categoryId that refers to Phones category. does anyone know what's wrong?
Entity Type Configurations (fluent api):
public class CategoryConfiguration : EntityTypeConfiguration<Category>
{
public CategoryConfiguration()
{
ToTable("Categories");
HasKey(m => m.CategoryId);
}
}
public class PhoneConfiguration : EntityTypeConfiguration<Phone>
{
public PhoneConfiguration()
{
ToTable("Phones");
HasKey(m => m.ProductId);
}
}
Seed method:
protected override void Seed(BestPhone.Data.BestPhoneDbContext context)
{
context.Categories.AddOrUpdate(new Category(){Name = "Phones", CategoryId = 1});
...
//getting records from a csv file and holding them in an array.
var records = csvReader.GetRecords<Phone>().ToArray();
foreach (var record in records)
{
record.CategoryId = 1;
}
context.Phones.AddRange(records);
context.SaveChanges();
}
}
Try to add the next method on your CategoryConfiguration class:
public void Configure(EntityTypeBuilder<Category> builder)
{
builder
.HasMany(s => s.Products)
.WithOne(t => t.Category)
.HasForeignKey(t => t.CategoryId);
}
I'm not sure but it seems a system does not take into account your foreign key during seeding.

Convert in ObservableCollection (C# uwp)

I am writing an app for UWP.
I tried to use data binding according to this answer link.
Here are my classes:
public class Billing
{
public string first_name { get; set; }
public string last_name { get; set; }
public string company { get; set; }
public string address_1 { get; set; }
public string address_2 { get; set; }
public string city { get; set; }
public string state { get; set; }
public string postcode { get; set; }
public string country { get; set; }
public string email { get; set; }
public string phone { get; set; }
}
public class Shipping
{
public string first_name { get; set; }
public string last_name { get; set; }
public string company { get; set; }
public string address_1 { get; set; }
public string address_2 { get; set; }
public string city { get; set; }
public string state { get; set; }
public string postcode { get; set; }
public string country { get; set; }
}
public class RootObject
{
public int id { get; set; }
public int parent_id { get; set; }
public string status { get; set; }
public string order_key { get; set; }
public string currency { get; set; }
public string version { get; set; }
public bool prices_include_tax { get; set; }
public string date_created { get; set; }
public string date_modified { get; set; }
public int customer_id { get; set; }
public double discount_total { get; set; }
public double discount_tax { get; set; }
public double shipping_total { get; set; }
public double shipping_tax { get; set; }
public double cart_tax { get; set; }
public double total { get; set; }
public double total_tax { get; set; }
public Billing billing { get; set; }
public Shipping shipping { get; set; }
public string payment_method { get; set; }
public string payment_method_title { get; set; }
public string transaction_id { get; set; }
public string customer_ip_address { get; set; }
public string customer_user_agent { get; set; }
public string created_via { get; set; }
public string customer_note { get; set; }
public string date_completed { get; set; }
public string date_paid { get; set; }
public string cart_hash { get; set; }
public List<object> line_items { get; set; }
public List<object> tax_lines { get; set; }
public List<object> shipping_lines { get; set; }
public List<object> fee_lines { get; set; }
public List<object> coupon_lines { get; set; }
}
public ObservableCollection<RootObject> Orders { get; set; }
Here is the code:
List<RootObject> rootObjectData = JsonConvert.DeserializeObject<List<RootObject>>(products);
foreach (RootObject root in rootObjectData)
{
string date = root.date_created;
string name = root.billing.first_name + root.billing.last_name ;
Orders = new ObservableCollection<RootObject> { new RootObject { date_created = date,billing = name } };
}
With billing = name I have this error: Cannot implicitly convert type 'string' to 'Milano.InWork.Billing'
How can I fix this error?
Maybe this is simple, but I don't find a solution.
Thanks for the help!!
With billing = name I have this error: Cannot implicitly convert type 'string' to 'Milano.InWork.Billing'
Of course this error will occur, your billing in the class RootObject has the data type Billing, which is another class you defined.
Just from your code I think you only want to show the first_name and last_name as string in your root.billing, then you can change the code public Billing billing { get; set; } in your RootObject class to public string billing { get; set; }.

How to parse Json from the RootObject?

I am trying to parse JSON response so I created some classes.
Actually I want Leg and Flight class element value. I am trying to get those element value from RootObject but I don't know how to do this. I googled but I am little bit confuse.
I paste my JSON respose , Classes !!
JSON Response :
http://pastebin.com/fjjLxkd2
Classes :
public class Detail
{
}
public class Airport
{
public string kind { get; set; }
public string code { get; set; }
public string city { get; set; }
public string name { get; set; }
}
public class City
{
public string kind { get; set; }
public string code { get; set; }
public string name { get; set; }
}
public class Aircraft
{
public string kind { get; set; }
public string code { get; set; }
public string name { get; set; }
}
public class Tax
{
public string kind { get; set; }
public string id { get; set; }
public string name { get; set; }
}
public class Carrier
{
public string kind { get; set; }
public string code { get; set; }
public string name { get; set; }
}
public class Data
{
public string kind { get; set; }
public List<Airport> airport { get; set; }
public List<City> city { get; set; }
public List<Aircraft> aircraft { get; set; }
public List<Tax> tax { get; set; }
public List<Carrier> carrier { get; set; }
}
public class Flight
{
public string carrier { get; set; }
public string number { get; set; }
}
public class Leg
{
public string kind { get; set; }
public string id { get; set; }
public string aircraft { get; set; }
public string arrivalTime { get; set; }
public string departureTime { get; set; }
public string origin { get; set; }
public string destination { get; set; }
public string originTerminal { get; set; }
public int duration { get; set; }
public int onTimePerformance { get; set; }
public int mileage { get; set; }
public string meal { get; set; }
public bool secure { get; set; }
public string destinationTerminal { get; set; }
public string operatingDisclosure { get; set; }
}
public class Segment
{
public string kind { get; set; }
public int duration { get; set; }
public Flight flight { get; set; }
public string id { get; set; }
public string cabin { get; set; }
public string bookingCode { get; set; }
public int bookingCodeCount { get; set; }
public string marriedSegmentGroup { get; set; }
public List<Leg> leg { get; set; }
public int connectionDuration { get; set; }
}
public class Slouse
{
public string kind { get; set; }
public int duration { get; set; }
public List<Segment> segment { get; set; }
}
public class Fare
{
public string kind { get; set; }
public string id { get; set; }
public string carrier { get; set; }
public string origin { get; set; }
public string destination { get; set; }
public string basisCode { get; set; }
}
public class BagDescriptor
{
public string kind { get; set; }
public string commercialName { get; set; }
public int count { get; set; }
public string subcode { get; set; }
public List<string> description { get; set; }
}
public class FreeBaggageOption
{
public string kind { get; set; }
public List<BagDescriptor> bagDescriptor { get; set; }
public int pieces { get; set; }
}
public class SegmentPricing
{
public string kind { get; set; }
public string fareId { get; set; }
public string segmentId { get; set; }
public List<FreeBaggageOption> freeBaggageOption { get; set; }
}
public class Passengers
{
public string kind { get; set; }
public int adultCount { get; set; }
}
public class Tax2
{
public string kind { get; set; }
public string id { get; set; }
public string chargeType { get; set; }
public string code { get; set; }
public string country { get; set; }
public string salePrice { get; set; }
}
public class Pricing
{
public string kind { get; set; }
public List<Fare> fare { get; set; }
public List<SegmentPricing> segmentPricing { get; set; }
public string baseFareTotal { get; set; }
public string saleFareTotal { get; set; }
public string saleTaxTotal { get; set; }
public string saleTotal { get; set; }
public Passengers passengers { get; set; }
public List<Tax2> tax { get; set; }
public string fareCalculation { get; set; }
public string latestTicketingTime { get; set; }
public string ptc { get; set; }
}
public class TripOption
{
public string kind { get; set; }
public string saleTotal { get; set; }
public string id { get; set; }
public List<Slouse> slice { get; set; }
public List<Pricing> pricing { get; set; }
}
public class Trips
{
public string kind { get; set; }
public string requestId { get; set; }
public Data data { get; set; }
public List<TripOption> tripOption { get; set; }
}
public class RootObject
{
public string kind { get; set; }
public Trips trips { get; set; }
}
Code :
var obj0 = JsonConvert.DeserializeObject<RootObject>(responsedata);
Here I got only Trip class element . I want the Leg and Flight class element.
If you want to find information for a specific leg, you need to use Linq to traverse the tree. For example, if you just know the leg id, you could do something like this:
var allLegs = obj0.trips.tripOption.SelectMany(to => to.slice.SelectMany(sl => sl.segment.Select(sg => sg.leg)));
var leg = allLegs.FirstOrDefault(l => l.id == "yourId");
The query for retrieving a flight would be similar. You could also filter by tripOption id to get a specific tripOption and then retrieve flights or legs that are associated with it.

Categories