我有兩個Create
方法,一個用HttpGet
裝飾,另一個用HttpPost
裝飾。將數據從html表格保存到列表
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
<form action="/" method="post">
<input type="text" name="txt" value="" />
<input type="submit" />
</form>
的方法:我有一個爲第一個看起來像這樣創建視圖
List<string> myList = new List<string> { "element1", "element2", "element3" };
public ActionResult Create()
{
return View();
}
[HttpPost]
public ActionResult Create(string txt)
{
//myList.Add(Request.Form["txt"]);
myList.Add(txt);
return View();
}
我simly試圖從我的形式傳遞數據上的按鈕,我的第二個Create()
並保存到myList
。
我需要一些關於如何使這項工作的建議。
..什麼是不工作?這似乎工作得很好(假設你的'
@RudiVisser它只是沒有添加到列表 –
@RobertRoss什麼你的意思是「不添加」?在第二個請求'myList'只包含默認值? – feeeper