why custom type variable is not saving value MVC

2019-07-23 17:24发布

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

First method that gets called is

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

        return View(viewModel);
    }

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

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

The view that gets called :

@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>

The full class :

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 !

1条回答
淡お忘
2楼-- · 2019-07-23 18:15

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

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

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

查看更多
登录 后发表回答