为什么自定义类型变量不能保存值MVC

为什么自定义类型变量不能保存值MVC

本文介绍了为什么自定义类型变量不能保存值MVC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用MVC 5建立网站.我有一个带有3个变量的控制器类.

I am trying to build a website using MVC 5.I have a controller class with 3 variables.

public class WorkerController : Controller
{
    public ViewModel viewModel = new ViewModel();

    private WorkerDB Wdb = new WorkerDB();
    private ProjectDB Pdb = new ProjectDB();

第一个被调用的方法是

    public ActionResult Index(User user)
    {
        viewModel.User = user;
        viewModel.ProjectsList = Pdb.DbSet.ToList();
        viewModel.WorkerList = Wdb.DbSet.ToList();

        return View(viewModel);
    }

在这里,将存储viewModel数据类型的值.然后将viewModel传递给View.该视图返回另一种类型的变量并调用另一种方法.

Here, the values of viewModel data type gets stored.Then the viewModel is passed to a View.The view returns another type of variable and call another method.

[HttpPost]
public ActionResult StartStopTime(Worker worker)
{
    return RedirectToAction("Index", viewModel);
}

在此方法中,viewModel为null,我不明白为什么.

Inside this method, the viewModel is null and I don't understand why.

被调用的视图:

@using System
@using WebApplication2.Models
@using WebApplication2.ViewModels

@model ViewModel

@{
    Worker worker = new Worker();

    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Worker</title>
</head>
<body>

<div>
    @foreach(var i in Model.WorkerList)
    {
        if ((i.Nume == Model.User.Username) && (i.Prenume == Model.User.Password))
        {
            worker = i;
            break;
        }
    }
    <br />
</div>

<div>
    Salut @Model.User.Username
    @foreach (var item in Model.ProjectsList)
    {
        <br>
        @item.Numar
    }
</div>

<br />
<br />


<form action="/Worker/StartStopTime" method="post">
    <input type="hidden" name="ID" value="@worker.ID" />
    <input type="hidden" name="Nume" value="@worker.Nume" />
    <input type="hidden" name="Prenume" value="@worker.Prenume" />
    <input type="text" name="Proiect" value="" required />
    <input type="hidden" name="Start" value="@DateTime.Now.ToString()" />
    <input type="hidden" name="Stop" value="@DateTime.Now.ToString()" />
    <input type="submit" name="submit" value="Start/Stop" />
</form>

完整班级:

public class WorkerController : Controller
{
    public ViewModel viewModel = new ViewModel();

    private WorkerDB Wdb = new WorkerDB();
    private ProjectDB Pdb = new ProjectDB();

    // GET: Worker
    public ActionResult Index(User user)
    {
        viewModel.User = user;
        viewModel.ProjectsList = Pdb.DbSet.ToList();
        viewModel.WorkerList = Wdb.DbSet.ToList();

        return View(viewModel);
    }

    [HttpPost]
    public ActionResult StartStopTime(Worker worker)
    {
        return RedirectToAction("Index", viewModel);
    }
}

如果有人可以告诉我在哪里可以找到答案.谢谢!

If someone can show me where to find the answer. Thank you !

推荐答案

如果需要在请求之间保留数据,则应使用TempData:

If you need to persist data between requests, you should use TempData:

public ActionResult Index(User user)
{
    var viewModel = new ViewModel();
    viewModel.User = user;
    viewModel.ProjectsList = Pdb.DbSet.ToList();
    viewModel.WorkerList = Wdb.DbSet.ToList();

    TempData["MyViewModel"] = viewModel;

    return View(viewModel);
}

当您下一个请求(例如如下所示的发帖请求)时,您可以检索它:

When you do the next request (e.g a post request like below), you can retrieve it:

[HttpPost]
public ActionResult StartStopTime(Worker worker)
{
    var viewModel = TempData["MyViewModel"] as ViewModel;
    return RedirectToAction("Index", viewModel);
}

请注意,当您从TempData读取数据时,该值被标记为删除,并且下一个请求(在您的情况下为第三个请求)将不可用.如果您想读取数据并在下一个请求中保持活动状态,则可以执行以下操作:

Take note when you read your data from TempData, the value is marked for deletion, and it won't be available next request (a third request, in your case). If you want to read data and keep it alive in the next request, you could do this instead:

[HttpPost]
public ActionResult StartStopTime(Worker worker)
{
    var viewModel = TempData.Peek("MyViewModel") as ViewModel;
    return RedirectToAction("Index", viewModel);
}

通过使用Peek(),不会将存储在TempData中的值标记为删除.

By using Peek(), the value stored in TempData won't be marked for deletion.

这篇关于为什么自定义类型变量不能保存值MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 10:01