c# - 使用 RedirectToAction 传递 View 模型

标签 c# web model-view-controller

我想使用 RedirectToAction 将 View 模型从一个操作传递到另一个操作,但是在执行此操作时,当我已经填充了模型包含来自 Controller 的数据,但在 View 中为空。我想将数据从登录传递到登录。我不想使用查询字符串,因为这很容易被黑客操纵

[HttpPost]
public ActionResult Login(User account)
{
    using (TestDBEntities db = new TestDBEntities())
    {
        var user = db.Users.SingleOrDefault(u => u.Email == account.Email && u.Password == account.Password);
        if (user != null)
        {
            Session["USER"] = user.UserID;
            var model = new UserAccountViewModel{UserAccount = user};
            return RedirectToAction("LoggedIn", model);
        }

        ModelState.AddModelError("", "User credentials are invalid");
    }

    return View("Login");
}

public ActionResult LoggedIn(UserAccountViewModel model)
{
    return View(model);
}

最佳答案

RedirectToAction 将 30 倍踢回到浏览器。您的 View 模型将无法承受这种旅行。

您可以将 View 模型存储在 TempData 集合中, 或者只是调用您想要的函数:return mySuperCoolControllerInstance.ActionsAreJustMethods(viewModel)

在您的情况下,它将是:return this.LoggedIn(model);

关于c# - 使用 RedirectToAction 传递 View 模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60609613/

相关文章:

Java FX MVC 多个 View

android - 如何从钛工作室android项目中的另一个js文件访问一个js文件

ios - Swift 委托(delegate)和协议(protocol)方法不在 iOS 中调用

c# - 为什么 WebApi Controller 运行后没有人处理 DbContext?

html - "file"HTML 输入元素的预先存在的 MIME 类型接受的类型

web - 适合 aptana studio 初学者的所见即所得编辑器

google-app-engine - 在 Google App Engine 上托管/传输网站

c# - DllImport 未在 asp.net 代码隐藏文件中定义,即使包含 System.Runtime.InteropServices

c# - 需要什么 RedisCacheOptions.InstanceName ?

c# - Entity Framework 选择项目在有序列表中的位置