use many model class in view - c#

I have that model class:
public class CV
{
public PrivateInformation privateInformation { get; set; }
public List<Education> education { get; set; }
public List<WorkingExperience> workingExperience { get; set; }
}
public class PrivateInformation
{
public string Name { get; set; }
public DateTime BirthDate { get; set; }
public string Address { get; set; }
public string PhoneNumber { get; set; }
public string Email { get; set; }
}
public class Education
{
public string UniversityName { get; set; }
public string Faculty { get; set; }
public string Specialization { get; set; }
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
public bool OnGoing { get; set; }
}
public class WorkingExperience
{
public string CompanyName { get; set; }
public string Position { get; set; }
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
public bool OnGoing { get; set; }
}
and there are another classes too. I want to make that user enter whole information from one page. (university can be more than 1 because I have list. working experience too).
But when I'm making CV as model class in view I can't access to Education.Name
How can I do that? Or is there any other way?

The common pattern for doing that is creating a ViewModel class
Basically, create a class with all the properties you need for your view, and use it.
https://msdn.microsoft.com/en-us/library/ff798384.aspx

Just create a view Model of entire model and initilize properties of other classes in it if you want to use other propeerties also like,
Public class PrivateInformationViewModel
{
ublic string UniversityName { get; set; }
public string Faculty { get; set; }
public string Specialization { get; set; }
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
public bool OnGoing { get; set; }
public Education EModel {get; set;}
}
Now through this view model you can access education properties also.

Related

How To Assign the value in OneModel to AnotherModel

How to Assign the value in one model to another model without using AutoMapper ,foreach,and instance and then it will access any way to send value in one model to another model its is possible and then my source model will be byte array ..
This is my source model...
public class OneDriveItem : OneDriveItemBase
{
public string Id { get; set; }
public string Name { get; set; }
public string ETag { get; set; }
public OneDriveIdentitySet CreatedBy { get; set; }
public DateTimeOffset CreatedDateTime { get; set; }
public OneDriveIdentitySet LastModifiedBy { get; set; }
public DateTimeOffset LastModifiedDateTime { get; set; }
public long Size { get; set; }
public string WebUrl { get; set; }
public OneDriveItem[] Children { get; set; }
public OneDriveRemoteItem RemoteItem{ get; set; }
}
This is my Target Model
public class OneDriveModels
{
public string Id { get; set; }
public string Etag { get; set; }
public string Name { get; set; }
public string WebUrl { get; set; }
public string CTag { get; set; }
public long Size { get; set; }
public DateTime CreatedDateTime { get; set; }
public DateTime LastModifiedDateTime { get; set; }
public string DownloadUrlAnnotation { get; set; }
}
You can decide not to look for a shortcut and do it with a constructor/factory method.
For example:
public class OneDriveModels
{
public static FromOneDriveItem( OneDriveItem source)
{
Id = source.Id
ETag = source.ETag
(...)
}

Model Containing a IEnumerable property insert using entityFramework

I have the following 2 models:
public class Alert
{
public int Id { get; set; }
public DateTime AlertDatetime { get; set; }
public bool Unread { get; set; }
public int? ReadByUserId { get; set; }
public DateTime? ReadDateTime { get; set; }
public DateTime ImportDateTime { get; set; }
public bool AlertHasRecords { get; set; }
//Error Reporting and Recording.
public bool Error { get; set; }
public string ErrorText { get; set; }
public virtual IEnumerable<AlertRecord> Records { get; set; }
}
public class AlertRecord
{
public int Id { get; set; }
public string HospitalNumber { get; set; }
public string ForeName { get; set; }
public string SurName { get; set; }
public DateTime DateOfBirth { get; set; }
public DateTime EventDateTime { get; set; }
public string CrisNumber { get; set; }
public DateTime CreationDateTime { get; set; }
//Link it back to the master Alert!
public int AlertId { get; set; }
public virtual Alert Alert { get; set; }
}
Once the "Alert" Object properties have values in them, I am trying to use EntityFramework to inset this object into my SQL DB like this:
class Program
{
private static Alert MainAlert = new Alert();
private static PrimaryDBContext db = new PrimaryDBContext();
static void Main(string[] args)
{
MainAlert = AlertObjectFactory.GetAlertObject();
db.Alerts.Add(MainAlert);
db.SaveChanges();
}
}
The AlertObjectFactory.cs and The Class responsible for building the list of AlertRecords are here(They are large class files)
https://gist.github.com/anonymous/67a2ae0192257ac51f39
The "Alert" Table is being populated with data, however the 4 records in the
IEnumerable Records
are not being inserted...
Is this functionality possible with EF?
Try changing your IEnumerable to something that implements ICollection such as List
See this answer for more details

How to setup a relational data model?

I am completely new to ASP.NET and was trying to cross-reference two different models with each other. I have 'Customer':
public class Customer
{
public int CustomerID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Address { get; set; }
public string Zipcode { get; set; }
public string City { get; set; }
public string Email { get; set; }
public string Phone { get; set; }
}
and 'Reservation':
public class Reservation
{
public int ReservationID { get; set; }
public string PetName { get; set; }
public DateTime Birthdate { get; set; }
public string Specie { get; set; }
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
public Customer Customer { get; set; }
}
Now, each Reservation should belong to a Customer (as you can see in the bottom line of the Reservation model) and should therefore contain the CustomerID. On the other hand should each Customer contain references to each Reservation that was made by him.
How can I setup this relation?
Use an ICollection for a one to many relationship.
public class Customer
{
public int CustomerID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Address { get; set; }
public string Zipcode { get; set; }
public string City { get; set; }
public string Email { get; set; }
public string Phone { get; set; }
public ICollection<Reservation> Reservations { get; set; }
}
Note I originally suggested a generic list, but there's a few subtle differences.

Entity Framework 5 creating model from database, instantiate navigation properties in a constructor

I am trying to force entity framework 5.0 to create a constructor on each of my generated poco classes. This constructor should instantiate any foreign key navigation properties I have.
e.g.
public partial class Event
{
public System.Guid EventId { get; set; }
public System.DateTime CreatedDate { get; set; }
public string CreatedUser { get; set; }
public int CreatedUserId { get; set; }
public string Title { get; set; }
public string EventDesc { get; set; }
public System.DateTime Start { get; set; }
public System.DateTime End { get; set; }
public string Source { get; set; }
public bool Editable { get; set; }
public string ClassName { get; set; }
public string Url { get; set; }
public bool IsDeleted { get; set; }
public bool IsObsolete { get; set; }
public bool AllDay { get; set; }
public System.DateTime ModifiedDate { get; set; }
public string ModifiedUser { get; set; }
public int RowVer { get; set; }
public virtual UserProfile UserProfile { get; set; }
}
should become:
public partial class Event
{
public Event()
{
this.UserProfile = new UserProfile();
}
public System.Guid EventId { get; set; }
public System.DateTime CreatedDate { get; set; }
public string CreatedUser { get; set; }
public int CreatedUserId { get; set; }
public string Title { get; set; }
public string EventDesc { get; set; }
public System.DateTime Start { get; set; }
public System.DateTime End { get; set; }
public string Source { get; set; }
public bool Editable { get; set; }
public string ClassName { get; set; }
public string Url { get; set; }
public bool IsDeleted { get; set; }
public bool IsObsolete { get; set; }
public bool AllDay { get; set; }
public System.DateTime ModifiedDate { get; set; }
public string ModifiedUser { get; set; }
public int RowVer { get; set; }
public virtual UserProfile UserProfile { get; set; }
}
I know it is possible, but not sure how. Any help would be most appreciated.
Thanks
When I retrieve from db in my repository (see below) I create a list of events, when I pass this list of events back via json I get a parse error due to event.UserProfile being null.
I could set it in code for each event, but that wouldn't be smart.
I need a link or an example if possible to help achieve what I need.
public List<Event> GetEvents(int userId, DateTime start, DateTime end)
{
List<Event> domainList = new List<Event>();
using (BookingModels dbEntities = new BookingModels())
{
var eventQuery = from dboEvents in dbEntities.Events
where dboEvents.Start >= start
&& dboEvents.End <= end
select dboEvents;
domainList = eventQuery.ToList<Event>();
}
return domainList;
}

Mapping JSON data to model and back in MVC 4

Pardon any misspeaking I may do. I am just learning asp.net c#, OOP, and MVC 4.
I am using the Annotator plugin on my site and I am trying to store and retrieve the results from a data base. When a new annotation is created it sends information like this to the controller.
{
"text":"asdas sd a dasd as dasd",
"ranges":
[{
"start":"/div/p[3]",
"startOffset":195,
"end":"/div/p[3]",
"endOffset":532
}],
"quote":"Some quote that was selected.",
"UserID":"1",
"ProjectDocID":"1"
}
Now, I hoped something like this would load all the data into a nice easy object.
// GET: /Annotation/Create
public ActionResult Create(Comment comment)
{
db.Comments.Add(comment);
db.SaveChanges();
return Json(comment);
}
The model I had looked like this (and I am change it however it needs to be changed).
public class Comment
{
[Key]
public int CommentID { get; set; }
public int ProjectDocID { get; set; }
public int UserID { get; set; }
public string text { get; set; }
public string Annotation { get; set; }
public string quote { get; set; }
public string start { get; set; }
public string end { get; set; }
public string startOffset { get; set; }
public string endOffset { get; set; }
public DateTime DateCreated { get; set; }
public virtual ICollection<CommentVote> CommentVote { get; set; }
public virtual ICollection<CommentReply> CommentReply { get; set; }
public ProjectDoc ProjectDoc { get; set; }
public virtual User User { get; set; }
}
What do I need to do to get the data from the server and map it to the model. I also need to be able to send it back in the format at the top so the plugin understands it when it asks for it from the Details action in the controller.
Not sure if this is the right way to do it or not, but it lets me store it in the database and that's all that matter for the moment. I updated my model as follows.
public class Comment
{
[Key]
public int CommentID { get; set; }
public int ProjectDocID { get; set; }
public int UserID { get; set; }
public string text { get; set; }
public string quote { get; set; }
public DateTime DateCreated { get; set; }
public virtual ICollection<CommentVote> CommentVote { get; set; }
public virtual ICollection<CommentReply> CommentReply { get; set; }
public ProjectDoc ProjectDoc { get; set; }
public virtual User User { get; set; }
public List<ranges> ranges { get; set; }
}
public class ranges {
public int ID { get; set; }
public string start { get; set; }
public string end { get; set; }
public string startOffset { get; set; }
public string endOffset { get; set; }
}
This matches the JSON object I am sending in to the controller with Ajax. Once it matches exactly the controller action above works.

Categories