In my example i try to keep it simple, and remove all unnecessary things.
For edit view it's look strangle that i get old values out of text box, but i will replace it with pictures in the future, so don't look at this :)
i have a model
public class Artist
{
public int ArtistId { get; set; }
public string ArtistName { get; set; }
public virtual ICollection<Album> ArtistAlbums { get; set; }
}
public class Album
{
public int AlbumId { get; set; }
public string AlbumName { get; set; }
}
public ActionResult Create(Artist newArtist, IEnumerable<string> ArtistAlbums)
{
foreach (var album in ArtistAlbums)
{
newArtist.ArtistAlbums.Add(new Album { AlbumName = album });
}
db.Entry(newArtist).State = EntityState.Added;
db.SaveChanges();
return RedirectToAction("Index");
}
here is my piece of Edit View
@foreach (var album in Model.ArtistAlbums)
{
<div>@album.AlbumName</div>
<input type="text" name="ArtistAlbums" />
}
here is my Edit Action
[HttpPost]
public ActionResult Edit(Artist artist, IEnumerable<string> ArtistAlbums)
{
foreach (var album in ArtistAlbums)
{
artist.ArtistAlbums.Add(new Album { AlbumName = album });
}
// An object with the same key already exists in the ObjectStateManager. The ObjectStateManager cannot track multiple objects with the same key.
//db.Entry(artist).State = EntityState.Modified;
// this one update my Artist entry, but not my Albums for this entry.
// var oldArtist = db.Artists.Find(artist.ArtistId);
// db.Entry(oldArtist).CurrentValues.SetValues(artist);
db.SaveChanges();
return RedirectToAction("Index");
}
and the question is - How can i properly save my updated model? And i am scared for some crazy stuff like custom model binding and e.t.c. i wanna keep it simple and beauty.
Thank you guys.
sumerokr
0 Points
3 Posts
how to update DB entry?
Aug 18, 2012 10:52 AM|LINK
Hello. First of all sory for my English.
I am new to ASP.NET and got some questions.
In my example i try to keep it simple, and remove all unnecessary things.
For edit view it's look strangle that i get old values out of text box, but i will replace it with pictures in the future, so don't look at this :)
i have a model
public class Artist { public int ArtistId { get; set; } public string ArtistName { get; set; } public virtual ICollection<Album> ArtistAlbums { get; set; } } public class Album { public int AlbumId { get; set; } public string AlbumName { get; set; } }here is my piece of Create View
here is my create Action
public ActionResult Create(Artist newArtist, IEnumerable<string> ArtistAlbums) { foreach (var album in ArtistAlbums) { newArtist.ArtistAlbums.Add(new Album { AlbumName = album }); } db.Entry(newArtist).State = EntityState.Added; db.SaveChanges(); return RedirectToAction("Index"); }here is my piece of Edit View
@foreach (var album in Model.ArtistAlbums) { <div>@album.AlbumName</div> <input type="text" name="ArtistAlbums" /> }here is my Edit Action
[HttpPost] public ActionResult Edit(Artist artist, IEnumerable<string> ArtistAlbums) { foreach (var album in ArtistAlbums) { artist.ArtistAlbums.Add(new Album { AlbumName = album }); } // An object with the same key already exists in the ObjectStateManager. The ObjectStateManager cannot track multiple objects with the same key. //db.Entry(artist).State = EntityState.Modified; // this one update my Artist entry, but not my Albums for this entry. // var oldArtist = db.Artists.Find(artist.ArtistId); // db.Entry(oldArtist).CurrentValues.SetValues(artist); db.SaveChanges(); return RedirectToAction("Index"); }and the question is - How can i properly save my updated model? And i am scared for some crazy stuff like custom model binding and e.t.c. i wanna keep it simple and beauty.
Thank you guys.
sumerokr
0 Points
3 Posts
Re: how to update DB entry?
Aug 18, 2012 01:56 PM|LINK
I may be poorly explained. If you need some clarification, i am ready to give it. Stacked with it for a few days already. Thank you.
sumerokr
0 Points
3 Posts
Re: how to update DB entry?
Aug 19, 2012 06:10 AM|LINK
And the big idea is
I wanna fill Artist and his Albums at the same time.
I don't ever need Albums controller.
For example, if Madonna will release new Album, i wont go to Album -> new Album for Madonna
i want go to Artist > Madonna -> Edit -> add new textbox (via jQuery) -> Save.