简体   繁体   English

POST之后的ASP.NET MVC公共静态列表不相同

[英]ASP.NET MVC public static list not same after POST

I'm new to MVC and i have problem i can't understand. 我是MVC的新手,但我有无法理解的问题。 Below code is what i want to attain: 下面的代码是我想要获得的:

Model: 模型:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace testapplication.Models {
    public class testModel {

        public class SystemUser {
            public string Name { get; set; }
            public string Email { get; set; }
        }

        public static List<SystemUser> list1;
        public static void CreateList1() {
            list1 = new List<SystemUser>();
        }

        public static void FillList1() {
            SystemUser firstUser = new SystemUser();
            firstUser.Name = "name";
            firstUser.Email = "email@address.com";
            list1.Add(firstUser);
        }

        public static List<SystemUser> list2 = new List<SystemUser>();    
        public static void FillList2() { 
            SystemUser firstUser = new SystemUser();
            firstUser.Name = "name";
            firstUser.Email = "email@address.com";
            list2.Add(firstUser);
        }

        public static string list3 = "test";
    }
}

Controller: 控制器:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using testapplication.Models;

namespace testapplication.Controllers {
    public class HomeController : Controller {

        public ActionResult Index() {
            testModel.CreateList1();
            testModel.FillList1();
            testModel.FillList2();
            var var1 = testModel.list1;
            var var2 = testModel.list2;
            var var3 = testModel.list3;
            return View();
        }

        [HttpPost]
        public ActionResult Index(string test) {
            var var1 = testModel.list1;
            var var2 = testModel.list2;
            var var3 = testModel.list3;
            return View();
        }
    }
}

View: 视图:

<h2>Index</h2>

@using (Html.BeginForm("Index", "Home", FormMethod.Post)) {
    <input type="submit" value="Submit" /> 
}

Before POST method list1,list2 and list3 are ok but right after Post list1 is null, list2 is empty (instance is created) and list3 is ok. 在POST方法list1,list2和list3之前是可以的,但在post list1为空之后,list2为空(创建实例)并且list3可以。 Why is that? 这是为什么? I want to have same value before and after POST. 我希望在POST前后具有相同的值。

It seems that you are not using a strongly-typed view neither static variables. 似乎您既没有使用强类型视图,也没有使用静态变量。 So, your lists are "valid" only in your [HttpGet] Action. 因此,仅在您的[HttpGet]操作中,列表才是“有效的”。

You could try this: 您可以尝试以下方法:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using testapplication.Models;

namespace testapplication.Controllers {
    public class HomeController : Controller {
        private static testapplication.Models.testModel testModel;

        public ActionResult Index() {
            testModel.CreateList1();
            testModel.FillList1();
            testModel.FillList2();
            var var1 = testModel.list1;
            var var2 = testModel.list2;
            var var3 = testModel.list3;
            return View();
        }

        [HttpPost]
        public ActionResult Index(string test) {                
            var var1 = testModel.list1;
            var var2 = testModel.list2;
            var var3 = testModel.list3;
            return View();
        }
    }
}

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

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