[英]How can we redirect to same view without data loss
當我第一次加載項目時,我在項目的同一視圖上有兩個文本框,我們使用model.name在控制器上為文本框提供值,而不是在按鈕上單擊另一個文本框以顯示其他值model.age,但將model.name值刪除我想要兩個文本框中的值。
namespace WebApplication2.Controllers {
public class HomeController: Controller {
// GET: Home
Class1 cs = new Class1();
public ActionResult Index() {
if (TempData["A"] == null) {
cs.name = "hi";
TempData["A"] = "B";
}
return View(cs);
}
public ActionResult A() {
cs.age = "hello";
//return RedirectToAction("Index",cs);
return View("Index", cs);
}
}
}
namespace WebApplication2.Controllers
{
public class HomeController : Controller
{
// GET: Home
Class1 cs = new Class1();
public ActionResult Index()
{
if(TempData["A"]==null)
{
cs.name = "hi";
TempData["A"] = "B";
}
return View(cs);
}
public ActionResult A(string name)
{
cs.age ="hello";
cs.name = name;
return View("Index", cs);
}
}
}
只需將變量/對象設為靜態即可保留值。
static Class1 cs = new Class1();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.