0
我是新來的MVC,我有我不明白的問題。下面的代碼是什麼,我要做到:ASP.NET MVC公共靜態列表後POST不同
型號:
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 protected]";
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 protected]";
list2.Add(firstUser);
}
public static string list3 = "test";
}
}
控制器:
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();
}
}
}
查看:
<h2>Index</h2>
@using (Html.BeginForm("Index", "Home", FormMethod.Post)) {
<input type="submit" value="Submit" />
}
之前POST方法列表1,列表2和項目list3都OK但在Post list1爲空之後,list2爲空(創建實例)並且list3爲正確。這是爲什麼?我想在POST之前和之後擁有相同的值。
請停止指的是 「ASP.NET MVC」 簡稱爲 「MVC」。一個是框架,而另一個是獨立於語言的設計模式。這就像打電話給IE - 「互聯網」 –
你是對的。對不起 – Jakub
這不是直接清楚發生什麼,看起來應該填充。但是MVC中的一個通用規則(通常是ASP.NET)是爲了避免「靜態」。那麼你真的想要追求這個嗎? –