c# - 如何将这两个参数传递给控制器动作方法?
问题描述
我有 2 种操作方法,其中一种用作“获取”方法,但显示一个列表并仅从用户那里获取 1 个值。另一个使用收到的值更新数据库。但是,似乎我没有将此值传递给第二个控制器,因为我null
在调试代码时看到了它。谁能帮忙找出我的错误?
动作方法:
[AllowAnonymous]
public ActionResult AvaliaAluno()
{
var user = User.Identity.GetUserId();
if (User.IsInRole("Docentes") || User.IsInRole("Comissao"))
{
Docente d = db.Docentes.SingleOrDefault(x => x.UserId == user);
var vm = new ViewModels
{
Propostas = db.Propostas.Where(x => x.DocenteId == d.DocenteId).ToList(),
Alunos = db.Alunos.ToList(),
Candidaturas = db.Candidaturas.ToList()
};
return View(vm);
}
if (User.IsInRole("Empresas"))
{
Empresa d = db.Empresas.SingleOrDefault(x => x.UserId == user);
var vm = new ViewModels
{
Propostas = db.Propostas.Where(x => x.EmpresaId == d.EmpresaId).ToList(),
Alunos = db.Alunos.ToList(),
Candidaturas = db.Candidaturas.ToList()
};
return View(vm);
}
return View();
}
[AllowAnonymous]
public ActionResult ConfirmaAvaliacao(int id, decimal? avaliacao)
{
Candidatura c = db.Candidaturas.SingleOrDefault(x => x.CandidaturaId == id);
Proposta p = db.Propostas.SingleOrDefault(x => x.PropostaId == c.PropostaId);
if (User.IsInRole("Docentes") || User.IsInRole("Comissao"))
{
p.AvaliacaoDocenteAluno = avaliacao;
return RedirectToAction("AvaliaAluno");
}
if (User.IsInRole("Empresas"))
{
p.AvaliacaoEmpresaALuno = avaliacao;
return RedirectToAction("AvaliaAluno");
}
return RedirectToAction("Index", "Home");
}
和观点:
@model DEIS_ISEC.Models.ViewModels
<h3>Alunos Orientados por si</h3>
<table class="table">
<tr>
<th>
Nome do Aluno
</th>
<th>
Número do Aluno
</th>
<th>
Ramo Inscrito
</th>
<th>
Titulo da Proposta
</th>
<th>
Ramo da Proposta
</th>
<th>
Data de Início
</th>
<th>
Data de Fim
</th>
<th>
Avaliar
</th>
<th></th>
</tr>
@foreach (var item in Model.Propostas)
{
<tr>
@foreach (var c in Model.Candidaturas)
{
if (c.PropostaId == item.PropostaId)
{
foreach (var a in Model.Alunos)
{
if (a.AlunoId == c.AlunoId)
{
<td>
@Html.DisplayFor(modelItem => a.Nome) @Html.DisplayFor(modelItem => a.Apelido)
</td>
<td>
@Html.DisplayFor(modelItem => a.NumeroAluno)
</td>
<td>
@Html.DisplayFor(modelItem => a.Ramo)
</td>
}
}
}
}
<td>
@Html.DisplayFor(modelItem => item.Titulo)
</td>
<td>
@Html.DisplayFor(modelItem => item.Ramo)
</td>
<td>
@Html.DisplayFor(modelItem => item.DataInicio)
</td>
<td>
@Html.DisplayFor(modelItem => item.DataFim)
</td>
<td>
@Html.EditorFor(modelItem => item.AvaliacaoDocenteAluno, new { htmlAttributes = new { @class = "form-control", @style = "width:50% !important; min-width:50px;" } })
@Html.ValidationMessageFor(modelItem => item.AvaliacaoDocenteAluno, "", new { @class = "text-danger" })
</td>
<td>
@Html.ActionLink("Guardar", "ConfirmaAvaliacao", new { id = item.CandidaturaId, avaliacao = item.AvaliacaoDocenteAluno }, new { @class = "btn btn-info btn-md" })
</td>
</tr>
}
我相信当我在这里得到用户输入的那一刻:
@Html.EditorFor(modelItem => item.AvaliacaoDocenteAluno, new { htmlAttributes = new { @class = "form-control", @style = "width:50% !important; min-width:50px;" } })
@Html.ValidationMessageFor(modelItem => item.AvaliacaoDocenteAluno, "", new { @class = "text-danger" })
意想不到的事情发生了。
解决方案
我建议您查看ASP.NET MVC 4 Helpers, Forms and Validation > 练习 3:创建编辑视图,了解如何使用ASP.Net MVC实现编辑机制。
一般来说,我建议您查看链接中的完整示例,因为它涵盖了构建ASP.Net MVC应用程序所需的许多主题。
希望能帮助到你!
推荐阅读
- sql - 我需要一些帮助来解决 SQL 查询,我需要列出要求至少一种产品但不要求牛奶的消费者的姓名
- html - 在没有滑块的面板上显示长文本
- python - 为什么我的 Flask 服务器在部署到 Docker 容器后无法连接?
- c# - 尝试更改 MainPage 但新页面上的 WebView 无法加载站点。但是当我在应用程序启动时将它设置为主页时它是成功的
- sql - 多连接选择
- android - Jetpack 的 BiometricPrompt (androidx.biometric.BiometricPrompt) 抛出 NullPointerException
- java - 如何回滚捕获异常的事务?
- html - 响应式轮播库
- javascript - 访问其他HTML页面时如何避免与当前socket.io连接断开
- shell - UNIX - 如何将来自不同子目录的文件的名称和路径写入单个 .txt 文件?