ASP.NET MVC - 国家与建筑 [英] ASP.NET MVC - State and Architecture
问题描述
一对编程会话后,一个有趣的问题上来,我想我知道答案。
问:是否有在ASP.NET MVC中任何其它所需的方式比写入数据库或文本文件,保留国家等。
?我要在这里定义状态,以表示我们的人对象的集合,我们创建了一个新的,并转到另一页,并期望看到新创建人。 (所以没有阿贾克斯)
我的想法是,我们不希望任何功夫的ViewState或其他机制,这个框架是要回去无国籍网页。
的我的想法是,我们不希望任何功夫的ViewState或其他机制,这个框架是要回去无国籍网页。的
块引用>您所提供的例子是pretty容易没有任何形式的视图状态功夫使用已经在MVC能力做。 用户增加了一个人,并认为,未来屏幕上。让我code一个简单的
PersonController
,你想要做什么:公众的ActionResult添加()
{
返回查看(新的Person());
}[HttpPost]
公众的ActionResult添加(PersonViewModel myNewPersonViewModel)
{
//验证,用户正确输入一切
如果(!ModelState.IsValid)
返回查看(); //地图模型到我的数据库/实体/域对象
VAR myNewPerson =新的Person()
{
名字= myNewPersonViewModel.FirstName,
姓氏= myNewPersonViewModel.LastName
} // 1.保持人的状态,会将用户带到下一个视图链
用同样的动作//
如果(MyDataLayer.Save(myNewPerson))
{
变种人= MyDataLayer.GetPersons();
persons.Add(myNewPersion); 返回视图(PersonGrid人员);
} // 2。沿着人的唯一ID传递给不同的操作或控制器
//是的,另一个数据库调用,但可能不是一个大问题
如果(MyDataLayer.Save(myNewPerson))
返回RedirecToAction(PersonGrid...等通过INT为路径值); 返回视图(PersonSaveError,myNewPersonViewModel);
}现在,就是我感应是要在
PersonSaveSuccess
人又在另一页上或别的东西。在这种情况下,你可能想使用TempData的()
这是一个单一的服务会话,并只保存的状态的从一个请求到另一个或管理传统会话[]
自己弄好了。什么是混淆对我来说,你可能要去的分贝让所有的人反正。如果你救一个人就应该在您的收藏者在你的
GetPersons下一次调用()
。如果你不使用Ajax,比什么状态,你想坚持?After a pair programming session, an interesting question came up which I think I know the answer for.
Question: Is there any other desired way in ASP.NET MVC to retain 'state' other than writing to database or a text file?
I'm going to define state here to mean that we have a collection of person objects, we create a new one, and go to another page, and expect to see the newly created person. (so no Ajax)
My thoughts are we don't want any kung-fu ViewState or other mechanisms, this framework is about going back to a stateless web.
解决方案My thoughts are we don't want any kung-fu ViewState or other mechanisms, this framework is about going back to a stateless web.
The example you provided is pretty easy to do without any sort of "view state kung fu" using capabilities that are already in MVC. "User adds a person and sees that on the next screen." Let me code up a simple
PersonController
that does exactly what you want:public ActionResult Add() { return View(new Person()); } [HttpPost] public ActionResult Add(PersonViewModel myNewPersonViewModel) { //validate, user entered everything correctly if(!ModelState.IsValid) return View(); //map model to my database/entity/domain object var myNewPerson = new Person() { FirstName = myNewPersonViewModel.FirstName, LastName = myNewPersonViewModel.LastName } // 1. maintains person state, sends the user to the next view in the chain // using same action if(MyDataLayer.Save(myNewPerson)) { var persons = MyDataLayer.GetPersons(); persons.Add(myNewPersion); return View("PersonGrid", persons); } //2. pass along the unique id of person to a different action or controller //yes, another database call, but probably not a big deal if(MyDataLayer.Save(myNewPerson)) return RedirecToAction("PersonGrid", ...etc pass the int as route value); return View("PersonSaveError", myNewPersonViewModel); }
Now, what I'm sensing is that you want person on yet another page after
PersonSaveSuccess
or something else. In that case, you probably want to useTempData[""]
which is a single serving session and only saves state from one request to another or manage the traditionalSession[""]
yourself somehow.What is confusing to me is you're probably going to the db to get all your persons anyway. If you save a person it should be in your persons collection in the next call to your
GetPersons()
. If you're not using Ajax, than what state are you trying to persist?这篇关于ASP.NET MVC - 国家与建筑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!