How to use the mvc 3 dropdownlist - asp.net-mvc-3

Hello I'm new to mvc 3 and I need some pointers/help populating a dropdownlist. I have 3 classes which are my domain classes. Basically what I want to do is make a dropdownlist that will be populated with a specific members rented movies. What is the easiest way to do this? Is it to use the ViewBag or to make a ViewModel?
public class Member
{
public virtual int MemberId { get; set; }
public virtual long PersonalNr { get; set; }
public virtual string Name { get; set; }
public virtual string LastName { get; set; }
public virtual List<Rental> Rentals { get; set; }
}
public class Movie
{
public virtual int MovieId { get; set; }
public virtual string Name { get; set; }
public virtual bool IsInStock { get; set; }
}
public class Rental
{
public virtual int RentalId { get; set; }
public virtual int MovieId { get; set; }
public virtual Movie Movie { get; set; }
public virtual int MemberId { get; set; }
public virtual Member Member { get; set; }
public DateTime startDate { get; set; }
public DateTime dueDate { get; set; }
}

The easiest way is
#Html.DropDownListFor(
x => x.SelectedRentalId,
Model.Rentals
)
to know a little better on how to use it, including the select part, se Darin's answer
https://stackoverflow.com/a/6807331/28004

Related

how to filter nested list using Linq lambda

I have a class person which has a list of addresses and phones as the follow code.
In my query I want a list of person but not including the address and phone that are already deleted, but is always returning all addresses and phones even they flag as deleted. How could I filter those nested lists using lambda?
public class Person{
public int PersonId { get; set; }
public string Name { get; set; }
public virtual ICollection<Address> Addresses { get; set; }
public virtual ICollection<Phone> Phones{ get; set; }
public virtual Company Company { get; set; }
}
public class Address{
public int AddressId { get; set; }
public string Street { get; set; }
public bool Deleted { get; set; }
[ScriptIgnore]
public virtual Person Person { get; set; }
}
public class Phone{
public int PhoneId { get; set; }
public string Number{ get; set; }
public bool Deleted { get; set; }
[ScriptIgnore]
public virtual Person Person { get; set; }
}
return GetDbSet<Person>()
.Include("Address")
.Include("Phones")
.Where(i => i.Company.CompanyId == company.CompanyId)
.OrderByDescending(o => o.CreateTime).ToList();
Just add conditions in your Where clause to exclude deleted address and phone like:
Where(i => i.Company.CompanyId == company.CompanyId &&
i.Address.Any(r=> !r.Deleted) &&
i.Phone.Any(r=> !r.Deleted))

how to get data from multiple model in mvc4?

I've a problem in customize view in my application that upload images
i have three models
public int EmpId { get; set; }
public string EmployeeName { get; set; }
public virtual ICollection<Picture> picture{ get; set; }
and second
public int MacId { get; set; }
public string machine { get; set; }
and third
public int PicId { get; set; }
public string PicPath { get; set; }
public int MacId { get; set; }
public int EmpId { get; set; }
public virtual Employee Employee { get; set; }
and i want to make it show picture of mac and who is the employee who take it ?
Create New Model With the neccesary properties you want to bind to your view.
for eg:
class newModelName
{
public Model1TypeName model1 {get ; set; }
public Model2TypeName model2 {get ; set; }
public Model3TypeName model3 {get ; set; }
}
Now you can bind this new model to your view which will have all necessary information from all the three views.

Code-First Referencing Table

I am currently struggling with some mapping properties on my models. Here are my two models.
What I am looking to do is only have unique PersonTypes (ie MD, Nurse) in my table and the person model reference these personTypes.
public partial class Person
{
public Person()
{
this.PersonTypes = new List<PersonType>();
this.Contacts = new List<Contact>();
}
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public int FacilityId { get; set; }
[DataType(DataType.Text), MaxLength(200), Required]
public string FirstName { get; set; }
[DataType(DataType.Text), MaxLength(200)]
public string MiddleName { get; set; }
[DataType(DataType.Text), MaxLength(200), Required]
public string LastName { get; set; }
public int? SpecialtyId { get; set; }
public bool IsEnabled { get; set; }
// Mapped Properties
[ForeignKey("FacilityId")]
public virtual Facility Facility { get; set; }
[ForeignKey("SpecialtyId")]
public virtual Specialty Specialty { get; set; }
public virtual ICollection<PersonType> PersonTypes { get; set; }
public virtual ICollection<Contact> Contacts { get; set; }
}
public partial class PersonType
{
public PersonType()
{
}
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
[DataType(DataType.Text), MaxLength(200), Required]
public string Name { get; set; }
public bool IsEnabled { get; set; }
}
Person1 = MD, Nurse - Person2 = MD - Person3 = Nurse, CNP
I don't want to have MD in my PersonType Table 2 times from above example. Is this possible. Thanks.
From your example I gather that you have many-to-many relationship between Persons and PersonTypes. For EF CodeFirst to understand this you have to create symmetric navigation property in you PersonType: public virtual ICollection<Person> Persons.

Relationships between tables mvc3

I'm building website in MVC 3.
i am using EF code first in existing database.
my ET inside the model look like that:
public class Pages
{
[Required]
public int ID { get; set; }
public int ParentID { get; set; }
[Required]
public int PageType { get; set; }
[Required]
[DataType(DataType.Text)]
[DisplayName("כותרת")]
public string Title { get; set; }
public string SearchWords { get; set; }
public string Leng { get; set; }
public int? Sort { get; set; }
public string Modules { get; set; }
[ForeignKey("PageType")]
public virtual PagesType Type { get; set; }
public virtual IEnumerable<PagesType> Types { get; set; }
[ForeignKey("PageID")]
public ICollection<PageContent> PageContent { get; set; }
[ForeignKey("PageID")]
public virtual ICollection<ImagesTable> Images { get; set; }
}
public class PageContent
{
public int ID { get; set; }
public int PageID { get; set; }
public string Header { get; set; }
public string Text { get; set; }
[ForeignKey("ID")]
public virtual ICollection<Pages> Pages { get; set; }
}
as you see in my fist table that cold Pages i have a relationship to another table that named PageContent.
in my Pages class i had this code
[ForeignKey("PageID")]
public ICollection<PageContent> PageContent { get; set; }
now, when i trying to add new pageContent into new page i get an error.
see this code
public ActionResult AddPage(PageModel page)
{
SystemLogic cmd = new SystemLogic();
page.Leng = "he";
Models.Pages p = new Pages();
p.ParentID = page.ParentID;
PageContent pageContent = new PageContent();
pageContent.Text = page.Content;
p.PageContent.Add(pageContent);
The error is
Object reference not set to an instance of an object.
What i did wrong?
You will get the NRE at p.PageContent.Add(pageContent); because the collection is not initialized. Initialize the collections inside the constructor of Pages class.
public class Pages
{
public Pages()
{
PageContent = List<PageContent>();
Images = List<ImagesTable>();
}
[Required]
public int ID { get; set; }
public int ParentID { get; set; }
[Required]
public int PageType { get; set; }
[Required]
[DataType(DataType.Text)]
[DisplayName("כותרת")]
public string Title { get; set; }
public string SearchWords { get; set; }
public string Leng { get; set; }
public int? Sort { get; set; }
public string Modules { get; set; }
[ForeignKey("PageType")]
public virtual PagesType Type { get; set; }
public virtual IEnumerable<PagesType> Types { get; set; }
[ForeignKey("PageID")]
public ICollection<PageContent> PageContent { get; set; }
[ForeignKey("PageID")]
public virtual ICollection<ImagesTable> Images { get; set; }
}
Or before you add objects to the collection
if (p.PageContent == null) p.PageContent = new List<PageContent>();
p.PageContent.Add(pageContent);
You should consider using proper naming conventions(eg Page instead of Pages).

EF 4.1 - Model Relationships

I'm trying to create a quick ASP.NET MVC 3 application using the RC version of EF 4.1. I have two models:
public class Race
{
public int RaceId { get; set; }
public string RaceName { get; set; }
public string RaceDescription { get; set; }
public DateTime? RaceDate { get; set; }
public decimal? Budget { get; set; }
public Guid? UserId { get; set; }
public int? AddressId { get; set; }
public virtual Address Address { get; set; }
}
and
public class Address
{
public int AddressId { get; set; }
public string Street { get; set; }
public string StreetCont { get; set; }
public string City { get; set; }
public string State { get; set; }
public string ZipCode { get; set; }
public virtual Race Race { get; set; }
}
I get the following error when trying to insert a new Race:
Unable to determine the principal end
of an association between the types
'rcommander.Models.Race' and
'rcommander.Models.Address'. The
principal end of this association must
be explicitly configured using either
the relationship fluent API or data
annotations.
Shouldn't it recognize RaceId as the primary key of the Races table and AddressId as the FK to the Addresses table automatically? Am I missing something?
Thanks!
The problem here seems to be that EntityFramework can't recognize where the foreing key is, as you are holding cross references in both objects. Not being sure what you want to achieve, I may suggest something like this:
public class Race
{
public int RaceId { get; set; }
public string RaceName { get; set; }
public string RaceDescription { get; set; }
public DateTime? RaceDate { get; set; }
public decimal? Budget { get; set; }
public Guid? UserId { get; set; }
public int? AddressId { get; set; }
public virtual Address Address { get; set; }
}
public class Address
{
public int AddressId { get; set; }
public string Street { get; set; }
public string StreetCont { get; set; }
public string City { get; set; }
public string State { get; set; }
public string ZipCode { get; set; }
}
Skipping reference to Race in second entity.
The problem here is 1:1 relation between Address and Race! You probably want to map it as 1:N so you need to modify address to:
public class Address
{
public int AddressId { get; set; }
public string Street { get; set; }
public string StreetCont { get; set; }
public string City { get; set; }
public string State { get; set; }
public string ZipCode { get; set; }
public virtual ICollection<Race> Races { ... }
}
If you want to use 1:1 then you can't use AddressId in Race but AddressId in Address must be foreign key of Race because entity framework can achive 1:1 only be "sharing" primary key.
For one-to-one relationship, you need to add "[required]" attribute in the second class. See below:
public class Race
{
public int RaceId { get; set; }
public string RaceName { get; set; }
public string RaceDescription { get; set; }
public DateTime? RaceDate { get; set; }
public decimal? Budget { get; set; }
public Guid? UserId { get; set; }
public int? AddressId { get; set; }
public virtual Address Address { get; set; }
}
public class Address
{
public int AddressId { get; set; }
public string Street { get; set; }
public string StreetCont { get; set; }
public string City { get; set; }
public string State { get; set; }
public string ZipCode { get; set; }
[required]
public Race Race { get; set; }
}
There is a good post: Associations in EF Code First CTP5: Part 2 – Shared Primary Key Associations
http://weblogs.asp.net/manavi/archive/2010/12/19/entity-association-mapping-with-code-first-one-to-one-shared-primary-key-associations.aspx
It recognizes Id as the primary key by convention. So what you need to do:
public class Race
{
[Key]
public int RaceId { get; set; }
public string RaceName { get; set; }
public string RaceDescription { get; set; }
public DateTime? RaceDate { get; set; }
public decimal? Budget { get; set; }
public Guid? UserId { get; set; }
public int? AddressId { get; set; }
public virtual Address Address { get; set; }
}
and
public class Address
{
[Key]
public int AddressId { get; set; }
public string Street { get; set; }
public string StreetCont { get; set; }
public string City { get; set; }
public string State { get; set; }
public string ZipCode { get; set; }
[ForeignKey("RaceId")] // Maybe telling it what the ForeignKey is will help?
public virtual Race Race { get; set; }
}
The [Key] attribute indicates that it should be the PrimaryKey
If you don't want this, you need to rename your primary keys to simply public int Id {get; set; }
I think it would be solved also like this... I assumed that an address is not required to be associated with a race, but a race must always be associated with an address.
I had the same problem with Patients and Incidents and i solved it with InverseProperty which is actually the same with foreign key, but the other direction
public class Race
{
public int RaceId { get; set; }
public string RaceName { get; set; }
public string RaceDescription { get; set; }
public DateTime? RaceDate { get; set; }
public decimal? Budget { get; set; }
public Guid? UserId { get; set; }
public int AddressId { get; set; }
[ForeignKey("AddressId")]
public virtual Address Address { get; set; }
}
public class Address
{
public int AddressId { get; set; }
public string Street { get; set; }
public string StreetCont { get; set; }
public string City { get; set; }
public string State { get; set; }
public string ZipCode { get; set; }
public int? RaceId { get; set; }
[InverseProperty("RaceId")]
public Race Race { get; set; }
}

Resources