0
我有以下我的申請表:問題與ViewData的在ASP.NET MVC 3
BookCatalogue
--id(PK)
--BookName
--AuthorID( FK)
作者
--id(PK)
--FirstName
--LastName
在編輯視圖bookCatalogue我想顯示ID,但作者的名單不是文本框。
這是我的代碼:
控制器
public ActionResult Edit(int id)
{
ViewData["Authors"] = _authorRepository.GetAllAuthors();
var book = _bookCtalogueRepository.GetBookById(id);
if (book == null)
throw new Exception("Book not found");
return View(book);
}
視圖(此只顯示名字)
...
<div>
@Html.LabelFor(model => model.Author)
</div>
<div>
@Html.DropDownList("ID", new SelectList(ViewData["Authors"] as System.Collections.IEnumerable,
"ID", "LastName" , Model.AuthorID))
</div>
...
我有兩個問題:
1)如何結合姓和姓氏在一個DropDownList?
2)當我更改LastName和BookName並嘗試保存更改時,只有新的BookName發生更改。
這是我的代碼用來保存更改
[HttpPost]
public ActionResult Edit(int id, FormCollection collection)
{
var book = _bookCtalogueRepository.GetBookById(id);
if (TryUpdateModel(book))
{
_bookCtalogueRepository.SaveChanges();
return RedirectToAction("Index");
}
else
{
ViewData["Genres"] = _genreRepository.GetAllGenres();
ViewData["Authors"] = _authorRepository.GetAllAuthors();
return View(book);
}
}
謝謝。
你在哪裏改變你的姓? – 2011-04-21 03:49:00