我想建立使用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);
}
}
如果有人可以告诉我在哪里可以找到答案。 谢谢 !