首页 > 解决方案 > 如何将这两个参数传递给控制器​​动作方法?

问题描述

我有 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" })

意想不到的事情发生了。

标签: c#asp.net-mvcmodel-view-controller

解决方案


我建议您查看ASP.NET MVC 4 Helpers, Forms and Validation > 练习 3:创建编辑视图,了解如何使用ASP.Net MVC实现编辑机制。

一般来说,我建议您查看链接中的完整示例,因为它涵盖了构建ASP.Net MVC应用程序所需的许多主题。

希望能帮助到你!


推荐阅读