为什么自定义类型变量没有保存价值MVC(why custom type variable is no

2019-09-28 15:56发布

我想建立使用MVC 5.我有3个变量控制器类网站。

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

在这里,视图模型数据类型的值被存储。 然后视图模型传递给视图。 视图返回另一种类型的可变的并调用另一方法。

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

在此方法中,视图模型为空,我不明白为什么。

被调用的观点:

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

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

Answer 1:

如果你需要保留请求之间的数据,你应该使用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);
}

当你的下一个请求(例如象下面这样一个POST请求),你可以找回:

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

就拿当你阅读你的数据注意TempData ,该值被标记为删除,并且不会在明年请求(第三请求,你的情况)。 如果你想读取数据,并保持它活着的下一个请求,你可以这样做,而不是:

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

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



文章来源: why custom type variable is not saving value MVC