简体   繁体   English

MVC3中的模型持久性-模型设置为null且未保存,

[英]Model persistence in MVC3 - Models being set to null & not being saved,

Im persisting my data in MVC3 and have come across an annoying problem: 我将数据保留在MVC3中,并且遇到了一个令人讨厌的问题:

Lets say I have MyModel a = new MyModel() & MyModel b = new MySubmodel() defined in my constructor. 可以说我在构造函数中定义了MyModel a = new MyModel()MyModel b = new MySubmodel()

When I visit page 1: 当我访问第1页时:

 if (MyModel.MySubmodel== null)
  {
    //populate with defaults
  }else{
     //use existing model data
  }

On page 1, I can change the values using Html.textboxFor fields. 在第1页上,我可以使用Html.textboxFor字段更改值。 I have my model updating with: 我的模型更新为:

 [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Page1(Mysubmodel model)
        {

            MyModel.Mysubmodel = model;
            return RedirectToAction("Page2", "Tool");
        }

using a watch ,I can see that my model is updating with the new values! 使用手表,我可以看到我的模型正在使用新值进行更新! (which is great!), however, If i try to return to my page to check if the results are saved they are set to null again (太好了!),但是,如果我尝试返回到页面以检查结果是否已保存,它们会再次设置为null

Code: 码:

 public class MyController : Controller
{
  //set up initial models
       public MyModel mainmodel = new MyModel ();
       public MySubModel submodel = new MySubModel ();
 public ActionResult Page1()
        {
            tempList.Clear();
            service.XmlParseDefault(Request); //Acquire defaults
            //setup model 
            //could possibly throw this into .services if it becomes a "fat controller"
            if (mymodel.submobdel == null)
            {
                //Apply default values
            }
        return View(submodel);
}

 [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Page1(submodel model)
        {

            mymodel.submodel = model;
            return RedirectToAction("Page2", "homecontroller");
        }


}

HTTP : request/response protocol HTTP:请求/响应协议

Once a request is fulfilled by the Server by rendering the appropriate response (can be html, xml or json) to the Client, everything is flushed out of the memory. 服务器通过向客户端呈现适当的响应(可以是html,xml或json)来满足请求后,所有内容都会从内存中清除。 (Exceptions to this are session states). (会话状态除外)。

Therefore, in your case, when a request is received, the two model instances are created in the constructor and destroyed after the controller class goes out of scope ie it renders the view. 因此,在您的情况下,当收到请求时,两个模型实例将在构造函数中创建,并在控制器类超出范围(即,它呈现视图)之后销毁。 So, when your form is submitted, the instances for the models are again created and that's why they always show null values. 因此,在提交表单时,将再次创建模型的实例,这就是它们始终显示空值的原因。

What if i used ViewData in my postback to store the MyModel? 如果我在回发中使用ViewData来存储MyModel怎么办? Would I be right in thinking that this would store the entire model(with submodels), then read it in in the constructor of the page. 我认为这将存储整个模型(带有子模型),然后在页面的构造函数中读取它是否正确。 Also I assume that unlike sessions, ViewData is autodestroyed after one use? 我还假设与会话不同,ViewData在使用一次后会自动销毁?

Don't user ViewData, always use model and pass them to the views. 不要使用ViewData,请始终使用模型并将其传递给视图。 Retrieve them when you post back. 回发时检索它们。 Assign the postback model to new instances of models and save the changes somewhere persistent. 将回发模型分配给模型的新实例,并将更改保存在持久的位置。 Like in a database or xml file. 就像在数据库或xml文件中一样。

Using a Static class can help you out, but it's not recommended. 使用静态类可以为您提供帮助,但不建议这样做。 Because as i said, these classes live as long as the application recycles. 因为正如我所说,只要应用程序回收,这些类就一直存在。 Use a database or xml file. 使用数据库或xml文件。 Even for testing purposes 即使用于测试

After you perform the Redirect the model will be lost as the controller will be recreated on the GET request for Page2. 执行重定向后,该模型将丢失,因为将根据Page2的GET请求重新创建控制器。

So your question is ultimately, how do you store the submodel model during the redirect from page1 to page2. 因此,最终的问题是,在从page1到page2的重定向过程中,如何存储子模型模型。

I guess it depends what the model contains? 我猜这取决于模型包含什么?

If the information in the model does not contain sensitive information then I don't see anything wrong with storing the model values in hidden form fields to be recreated in the submodel by the default model binder in Page2 (keep in mind that a malicious user could change the values). 如果模型中的信息不包含敏感信息,那么将模型值存储在要由Page2中的默认模型绑定器在子模型中重新创建的隐藏表单字段中存储的情况下,我看不到任何错误(请注意,恶意用户可能更改值)。

If the information is more sensitive (or some of it is) then the sensitive information could be passed using TempData (note that this is will be removed from TempData once it is read in Page2) or in the Session so that it will be stored during the entire user session. 如果信息较敏感(或某些敏感),则可以使用TempData传递敏感信息(请注意,一旦在Page2中读取该信息,将从TempData中将其删除)或在会话中进行存储,以便在整个用户会话。

Or if the information is more permanent then it could be written to the database and the model recreated when necessary using this information. 或者,如果信息是永久性的,则可以将其写入数据库,并在必要时使用此信息重新创建模型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM