mvc model with a foreign key, connection lost - asp.net-mvc

I have 2 models, Artist and Album.
Artist have a collection of albums.
Album have a foreigm key to Artist (int artistId).
Now in my site you get to select the album for edition from its artist,
and when I send the album back to the controller, its artistId always 0 (default value of int) and I failed to save the album, because there is no such artist with 0 id. How I'm losing the id of the artist?
the artist
public class Artist
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Gender { get; set; }
public string NickName { get; set; }
public string Website { get; set; }
public string Facebook { get; set; }
public string Twitter { get; set; }
public string Wikipedia { get; set; }
public string Background { get; set; }
public long StartYearActivity { get; set; }
public virtual ICollection<Album> Albums { get; set; }
public DateTime Birthdate { get; set; }
}
the album
public class Album
{
public int Id { get; set; }
public int ArtistId { get; set; }
public string Genre { get; set; }
public string Wikipedia { get; set; }
public List<Song> Songs { get; set; }
public string Name { get; set; }
public int NumOfSongs { get { return this.Songs != null ? this.Songs.Count : 0; } }
public int Year { get; set; }
}
the save method for album when the artist id is null:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult EditAlbum([Bind(Include = "Id,Genre,Wikipedia,Name,Year")] Album album)
{
db.Entry(album).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(album);
}
What am I doing wrong?

Related

MVC View display items of a list contained within a list

I wonder if someone could help me please. I have a shopping basket which can contain products and each product can contain customers so for instance:
A basket contains the details of the 'purchaser' (FirstName, LastName, etc.) and
Product details (product name, date, etc.) and each product will have
Delegates (people actually attending the event....FirstName, LastName, etc.
What I'm trying to create is a form which shows all of the products with a list of fields underneath allowing the purchaser to enter/amend the names of the people that are attending the event:
What I'm struggling with though is to create the input fields for the attendees. I can use:
foreach(var dele in product.delegates)
{
<p>#dele.FirstName</p>
}
Which does display a list of the names of the attendees as expected but I would expect to be able to use something like:
for(var i=0, i < product.delegates.Count; i++)
{
#Html.TextBoxFor(x => x[i].FirstName)
}
But this displays the name (repeated) of the purchaser and not the attendees.
Could anyone assist please? Like I say I want to display a list of inputs that can amended and posted to an ActionResult to update the DB.
As a note I can see the model is populated with the information that I'm expecting.
public class ShoppingCart
{
public int ShoppingCartId { get; set; }
public DateTime Created { get; set; }
public string TransStatus { get; set; }
public bool Completed { get; set; }
public string ContactNumber { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Phone { get; set; }
public string Mobile { get; set; }
public string CompanyName { get; set; }
public string PostCode { get; set; }
public string Email { get; set; }
public string Address1 { get; set; }
public string Address2 { get; set; }
public string Address3 { get; set; }
public string City { get; set; }
public string County { get; set; }
public string StoreKey { get; set; }
public DateTime? SentToStore { get; set; }
public virtual ICollection<ShoppingCartProduct> Products { get; set; }
}
public class ShoppingCartProduct
{
public int ShoppingCartProductId { get; set; }
public int ShoppingCartId { get; set; }
public string ProductId { get; set; }
public string CourseId { get; set; }
public string ProductName { get; set; }
public string ProductType { get; set; }
public decimal ItemPrice { get; set; }
public string Location { get; set; }
public DateTime? EventDate { get; set; }
public int Quantity { get; set; }
public decimal TotalPrice => ItemPrice * Quantity;
public virtual ShoppingCart ShoppingCart { get; set; }
public virtual ICollection<ShoppingCartDelegate> delegates { get; set; }
}
public class ShoppingCartDelegate
{
public int ShoppingCartDelegateID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string email { get; set; }
public int ShoppingCartProductId { get; set; }
public virtual ShoppingCartProduct ShoppingCartProduct { get; set; }
}
Sorry buddy, it was me.....the objects were collections and not lists so I was never going to be able to iterate through them with an index. Sorted now. Thanks for you time :)

Retrieve specific record from database in MVC

Please look at my sample code below:
public class LoginModel
{
[Key]
public int LoginId { get; set; }
public string UserName { get; set; }
public string Password { get; set; }
}
public class RegisterModel
{
[Key]
public int RegisterId { get; set; }
public string UserName { get; set; }
public string Password { get; set; }
public string Email { get; set; }
}
public class Cart
{
[Key]
public int RecordId { get; set; }
public string CartId { get; set; }
public int ProductId { get; set; }
public int Count { get; set; }
}
There are many records in the cart table added by many users.
But i want to show the own record of a user from the cart table if he is logged in..how can I do this??
You have to insert a foreign-key property to your Cart class:
public class UserModel
{
[Key]
public int LoginId { get; set; }
public string UserName { get; set; }
public string Password { get; set; }
}
public class Cart
{
[Key]
public int RecordId { get; set; }
public string CartId { get; set; }
public int ProductId { get; set; }
public int Count { get; set; }
public virtual UserModel User { get; set; } // <-- added
}
Then you can use LINQ to query the Cart-items for a specific user:
var cartItems = dbContext.Cart.Where(a => a.User == loggedInUser);
var query = from a in db.Pages
where a.title.Contains(myTitle)
select a;
var item=query.FirstOrDefault();
if(item!=null)
return View(item);
else
return View("NotFound");

Display list of data on the basis of roles in MVC asp.net identity

I have a list which shows all the videos uploaded by Admin and Users, But I want to apply some condition to individually display User and Admin uploads list (just want to separate list on the basis of roles).
How can i do it?
This is the code which displays all the Videos
public ActionResult Index()
{
var videos = db.Videos.Include(v => v.Artist).Include(v => v.Category);
return View(videos.ToList());
}
Here is the video class
public class Video
{
public int Id { get; set; }
public virtual Category Category { get; set; }
public int CategoryId { get; set; }
public virtual Artist Artist { get; set; }
public int ArtistId { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public string Length { get; set; }
public string Keywords { get; set; }
public string format { get; set; }
public string Language { get; set; }
public string URL { get; set; }
public string Image { get; set; }
public string UserId { get; set; }
public bool isblock { get; set; }
public virtual ApplicationUser ApplicationUser { get; set; }
public virtual ICollection<Comment> comments { get; set; }
public virtual ICollection<Like> likes { get; set; }
public virtual ICollection<HitCounter> Views { get; set; }
}

asp.net using mvc 3 linq and join

i have a problem with my join in my asp.net .. i have two database tables.. named APPLICANT and Profile...they have the same fields.. meaning if the Last name in applicant is null the last name is already in profile table. I already connect my program to the applicant table.. but it have so many null fields that have to fetch from the profile data table.... sorry i'm new in asp.net ...
Here's my code in controller:
public View Result Index()
{
var applicants = (from a in db.APPLICANTs
select a).ToList();
return View(applicants);
}
heres my context:
public partial class APPLICANT
{
public int APPLICANT_ID { get; set; }
public Nullable<int> Profile_id { get; set; }
public string APPLICANT_LastName { get; set; }
public string APPLICANT_FirstName { get; set; }
public string APPLICANT_MiddleName { get; set; }
public string APPLICANT_Address { get; set; }
public string APPLICANT_City { get; set; }
public string APPLICANT_ZipCode { get; set; }
public string APPLICANT_Phone { get; set; }
public string APPLICANT_Email { get; set; }
}
public partial class Profile
{
public int PROFILE_ID { get; set; }
public string Applicant_LASTNAME { get; set; }
public string Applicant_FIRSTNAME { get; set; }
public string Applicant_MIDDLENAME { get; set; }
public string Applicant_EMAIL { get; set; }
public string Applicant_PHONE { get; set; }
public string Applicant_ADDRESS { get; set; }
public string Applicant_ZIPCODE { get; set; }
public string Applicant_CITY { get; set; }
}
thanks for those who can help my problem..

MVC3 - Sorting db query when using Include

I'm doing a site where i've been using the MvcMusicStore as a base.
I want to get all the albums from a specific genre, and order these by Artist name. I can't really figure out how to order by Artist name.
My models:
public partial class Genre
{
public int GenreId { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public ICollection Albums { get; set; }
public ICollection Artists { get; set; }
}
public class Artist
{
public int ArtistId { get; set; }
public string Name { get; set; }
}
public class Album
{
public int AlbumId { get; set; }
public int GenreId { get; set; }
public int ArtistId { get; set; }
public string Title { get; set; }
public string Price { get; set; }
public string AlbumArtUrl { get; set; }
public string Description { get; set; }
public virtual Genre Genre { get; set; }
public virtual Artist Artist { get; set; }
}
My Controller:
public ActionResult Index(string genre = "CD/LP")
{
var genreModel = storeDb.Genres.Include("Albums").Include("Artists")
.Where(g => g.Name == genre).FirstOrDefault();
return View(genreModel);
}
How can I order the results by Artist name?
storeDb.Albums.Where(a => a.Genre.Name == genre).OrerBy(a => a.Artist.Name)

Resources