这是在 MVC3 中保存表单值的正确方法吗?
这是我的代码:
[HttpGet]
public ActionResult Register()
{
RegisterViewModel model = new RegisterViewModel();
using (CityRepository city = new CityRepository())
{
model.SelectCityList = new SelectList(city.FindAllCities().ToList(), "CityID", "CityName");
}
using (CountryRepository country = new CountryRepository())
{
model.SelectCountryList = new SelectList(country.FindAllCountries().ToList(), "CountryID", "CountryName");
}
return View(model);
}
[HttpPost]
public ActionResult Register(RegisterViewModel model)
{
if (ModelState.IsValid)
{
//Actually register the user here.
RedirectToAction("Index", "Home");
}
//Something went wrong, redisplay the form for correction.
return View(model);
}
这是最好的方法还是还有其他更好的测试方法?请记住,我的数据库表/字段名称与我在模型中声明的完全不同。我必须从 ViewModel 中抓取值并将它们放入实体框架生成的类中以保留信息。
这里有什么事情让你大喊大叫是错的吗?
Here's my code:
[HttpGet]
public ActionResult Register()
{
RegisterViewModel model = new RegisterViewModel();
using (CityRepository city = new CityRepository())
{
model.SelectCityList = new SelectList(city.FindAllCities().ToList(), "CityID", "CityName");
}
using (CountryRepository country = new CountryRepository())
{
model.SelectCountryList = new SelectList(country.FindAllCountries().ToList(), "CountryID", "CountryName");
}
return View(model);
}
[HttpPost]
public ActionResult Register(RegisterViewModel model)
{
if (ModelState.IsValid)
{
//Actually register the user here.
RedirectToAction("Index", "Home");
}
//Something went wrong, redisplay the form for correction.
return View(model);
}
Is this the best approach or is there another better tested way? Keep in mind that my database tables/field names are nothing like what I declared in my models. I have to scrape the values from the ViewModel and put them into an entity framework generated class to persist the information.
Anything here that screams out at you as wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我使用该模式和另一个如下所示的模式(重要部分是 AutoMapper 部分):
I use that pattern and another pattern which looks like this (important part is the AutoMapper part):
这是我通常使用的模式。
That's the pattern I generally use.
我更喜欢这种模式:
控制器:
查看:
I prefer this pattern:
Controller:
View:
对
[HttpGet]
的建议更改:更改摘要:
建议对
[HttpPost]
进行更改:更改摘要:
Advised changes to
[HttpGet]
:Summary of changes:
Advised changes to your
[HttpPost]
:Summary of changes: