【问题标题】:How can we redirect to same view without data loss我们如何在不丢失数据的情况下重定向到相同的视图
【发布时间】:2019-01-11 05:15:18
【问题描述】:

当我第一次加载时,我在项目的同一个视图上有两个文本框值被删除我想要文本框中的两个值。

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);
    }
  }
}

【问题讨论】:

    标签: c# jquery angularjs asp.net-mvc-4 asp.net-mvc-5


    【解决方案1】:
    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);
    
        }
    
    
        }
    }
    

    【讨论】:

      【解决方案2】:

      只需将变量/对象设为静态即可保留值。

      static Class1 cs = new Class1();
      

      【讨论】:

      • 将 Class1 设为静态将为所有访问者提供相同的值。
      猜你喜欢
      • 2016-01-19
      • 2021-05-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多