如果我有一個模型,它看起來是這樣的:MVC模型的設計和使用
public class LoginModel
{
pulbic List<string> UserNames {get; set; }
public string SelectedUserName {get; set; }
public string Password {get; set; }
}
而且我也有一對夫婦的動作方法的控制器,看起來是這樣的:
public ActionResult Login()
{
LoginModel model = null;
model = new LoginModel();
// Code to populate the UserNames property of the LoginModel instance (model)...
return View(model);
}
[HttpPost()]
public ActionResult Login(LoginModel model)
{
if (ModelState.IsValid == true)
{
return RedirectToAction("SomeOtherAction")
}
else
{
return View(model);
}
}
我將需要重新填充模型對象的UserNames屬性,然後將其傳遞給View方法。這是我當然可以做的事,但它確實感覺有點髒。這讓我想到了這個問題。有沒有更好的方法來處理這個問題?
你現在在MVC--想*無國界* – Nick 2013-02-18 22:32:12
感謝所有的答案。每個回答似乎基本上表明相同的事情。有趣的是,如果我使用傳統的ASP.NET(閱讀,我從來沒有真正喜歡會話和視圖狀態),我不會考慮*重建*模型,所以我不知道爲什麼我會被抓到在這種情況下。我想我只是想確保我正在做正確的事情(或者至少以一種可以接受的方式),因爲我剛剛開始。 – 2013-02-19 14:11:36