从上一个问题主题:mvc database first issues with edmx/relationships and view showing id field给出的答案中,我意识到viewmodel是要走的路,所以我会在这里发布我的viewmodel相关问题。
首先,edmx是以下
我的数据库限制是1本书可能是多位作者和多位发布者
这些是我目前的课程:
BOOK
public partial class BOOK
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public BOOK()
{
this.AUTHORS = new HashSet<AUTHOR>();
this.PUBLISHERS = new HashSet<PUBLISHER>();
}
public int ID { get; set; }
public string TITLE { get; set; }
public int PAGES { get; set; }
public string SYNOPSIS { get; set; }
public System.DateTime PUBLISHDATE { get; set; }
public string ISBN { get; set; }
public string SUBJECT { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<AUTHOR> AUTHORS { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<PUBLISHER> PUBLISHERS { get; set; }
}
出版商
public partial class PUBLISHER
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public PUBLISHER()
{
this.BOOKS = new HashSet<BOOK>();
}
public int ID { get; set; }
public string COUNTRY { get; set; }
public string NAME { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<BOOK> BOOKS { get; set; }
}
AUTHOR
public partial class AUTHOR
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public AUTHOR()
{
this.BOOKS = new HashSet<BOOK>();
}
public int ID { get; set; }
public string FIRSTNAME { get; set; }
public string LASTNAME { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<BOOK> BOOKS { get; set; }
}
现在我可以成功保存图书记录,但是他们只有1位作者和1位发布商,我如何才能完成多位作者和发布商?
我的观点模型:
public class BooksViewModel
{
[Key]
public int ID { get; set; }
[Display(Name = "Title")]
public string TITLE { get; set; }
[Display(Name = "Pages")]
public int PAGES { get; set; }
[Display(Name = "Synopsis")]
public string SYNOPSIS { get; set; }
[Display(Name = "Publish Date")]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
public DateTime PUBLISHDATE { get; set; }
[Display(Name = "ISBN")]
public string ISBN { get; set; }
[Display(Name = "Subject")]
public string SUBJECT { get; set; }
[Display(Name = "Publisher")]
public string NAME { get; set; }
[Display(Name = "Publisher Location")]
public string COUNTRY { get; set; }
[Display(Name = "Auhor First Name")]
public string FIRSTNAME { get; set; }
[Display(Name = "Author Last Name")]
public string LASTNAME { get; set; }
}
我应该将有关作者和发布者的字符串属性更改为这些对象的列表吗?即
public class BooksViewModel2
{
[Key]
public int ID { get; set; }
[Display(Name = "Title")]
public string TITLE { get; set; }
[Display(Name = "Pages")]
public int PAGES { get; set; }
[Display(Name = "Synopsis")]
public string SYNOPSIS { get; set; }
[Display(Name = "Publish Date")]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
public DateTime PUBLISHDATE { get; set; }
[Display(Name = "ISBN")]
public string ISBN { get; set; }
[Display(Name = "Subject")]
public string SUBJECT { get; set; }
public virtual ICollection<AUTHOR> AUTHORS { get; set; }
public virtual ICollection<PUBLISHER> PUBLISHERS { get; set; }
}
并在视图上使用此属性执行for循环,然后只需在页面中向表中添加列?
我发布我的索引并创建动作&#39;视图模型供您欣赏
索引操作:
private ApplicationDbContext db = new ApplicationDbContext();
// GET: BooksViewModels
public ActionResult Index()
{
return View(db.BooksViewModels.ToList());
}
创建行动:
public ActionResult Create([Bind(Include = "ID,TITLE,PAGES,SYNOPSIS,PUBLISHDATE,ISBN,SUBJECT,NAME,COUNTRY,FIRSTNAME,LASTNAME")] BooksViewModel booksViewModel)
{
if (ModelState.IsValid)
{
BOOK new_book = null;
var new_author = new List<AUTHOR>(){};
new_author.Add(new AUTHOR() { FIRSTNAME = booksViewModel.FIRSTNAME, LASTNAME = booksViewModel.LASTNAME });
var new_publisher = new List<PUBLISHER>() { };
new_publisher.Add(new PUBLISHER() { COUNTRY = booksViewModel.COUNTRY, NAME = booksViewModel.NAME });
using (ApplicationDbContext db = new ApplicationDbContext())
{
new_book = new BOOK()
{
ID = booksViewModel.ID,
TITLE = booksViewModel.TITLE,
PAGES = booksViewModel.PAGES,
SYNOPSIS = booksViewModel.SYNOPSIS,
PUBLISHDATE = booksViewModel.PUBLISHDATE,
ISBN = booksViewModel.ISBN,
SUBJECT = booksViewModel.SUBJECT,
AUTHORS = new_author,
PUBLISHERS = new_publisher
};
}
//database context
using (LibrarySQLEntities db2 = new LibrarySQLEntities())
{
db2.BOOKS.Add(new_book);
db2.SaveChanges();
}
//viewmodel context
using (ApplicationDbContext db = new ApplicationDbContext())
{
db.BooksViewModels.Add(booksViewModel);
db.SaveChanges();
}
}
return RedirectToAction("Index");
}
我对上面的代码有以下问题: