如果我有一个模型,它看起来是这样的: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