首页 > 解决方案 > 通过在视图上使用表发布值

问题描述

我的视图上有一个表格,我将模型传递给 tbody 上的这个视图,我从模型中获取值,并选择从视图包中获取值的位置。我想将每行上的值关联起来,例如 Metadisciplina.Nome 与 ViewBag.Docente.... 上的选定值。每行我都会得到一个我不知道的列表。现在我有一种方法,我可以得到两种不同类型的列表,其中一种是模型,另一种是选择选项中的值...但是使用这两个周期和很多如果我只能有一个我的想法...或者我可以为每个 Metadisciplina.Nome 或每个 Metadisciplina.Nome 获得相同的 Docente 值获得不同的 Docente 值...我希望有可能同时拥有具有不同 Docente 值的 Metadisciplina.Nome 和 Metadisciplina。 Nome 具有相同的 Docente 值..

看法

@model IList<ModelsLibrary.Turno>
@{
ViewData["Title"] = "Index";
}
<div class="row">
    <div class="col-md-4">
        <form asp-action="Index" method="post">
            <div asp-validation-summary="ModelOnly" class="text-danger"> 
</div>
            <div class="form-group">
                <label>Ano Letivo</label>
                <select name="anoLetivo" class="form-control" asp-items="ViewBag.Ano"></select>
            </div>
            <div class="form-group">
                <label>Ano Curricular</label>
                <select name="ano" class="form-control">
                    <option>-Select-</option>
                    <option>1</option>
                    <option>2</option>
                    <option>3</option>
                </select>
            </div>
            <div class="form-group">
                <label>Semestre</label>
                <select name="semestre" class="form-control">
                    <option>-Select-</option>
                    <option value="1">1</option>
                    <option value="2">2</option>
                </select>
            </div>
            <div class="form-group">
                <input type="submit" value="Escolhido" class="btn btn-default" />
            </div>
        </form>
    </div>
</div>

<form asp-action="Create" method="post">
    <table class="table">
        <thead>
            <tr>
                <th>
                    <label>Disciplina</label>
                </th>
                <th>
                    <label>Turno</label>
                </th>
                <th>
                    <label>Tipologia</label>
                </th>
                <th>
                    <label>Docente</label>
                </th>
            </tr>
        </thead>
        <tbody>
            @for (var i = 0; i < Model.Count; i++)
            {
                @Html.HiddenFor(x => x[i].TurnoId)
                @Html.HiddenFor(x => x[i].MetaDisciplina.Nome)
                <tr>
                    <td>
                        @Html.DisplayFor(modelItem => modelItem[i].MetaDisciplina.Nome)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => modelItem[i].LetraTurno)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => modelItem[i].Tipologia.Tipo)
                    </td>
                    <td>
                        <select name="DocenteId" asp-items="ViewBag.Docente"></select>
                    </td>
                </tr>

            }
        </tbody>
    </table>

    <div class="form-group">
         <input type="submit" value="Create" class="btn btn-default" />
    </div>
</form>
@section Scripts {
@{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}

控制器方法

  [HttpPost]
    public async Task<IActionResult> Create(IList<Turno> Turno, IList<int> DocenteId)
    {
        var turnodocente = new List<TurnoDocente>();

        foreach (var item in Turno)
        {    
            foreach (var ite in DocenteId)
            {
                if(!turnodocente.Any(x=>x.DocenteId == ite && x.TurnoId == item.TurnoId))
                {
                    turnodocente.Add(new TurnoDocente
                    {
                        TurnoId = item.TurnoId,
                        DocenteId = ite
                    });
                }
            }
        }

        HttpResponseMessage responsemeta = await HttpRequestBuilder.WebApiClient.PostAsJsonAsync("api/TurnoDocente", turnodocente);
        responsemeta.EnsureSuccessStatusCode();
        await _context.SaveChangesAsync();


        return RedirectToAction(nameof(Index));
    }

预期结果

预期结果

我希望得到没有红十字的值而不是全部....这是选项之一..在这里我只有一个例子,但我尝试另一种方法来执行 if 子句,但结果不是我想要的。 ..而且我真的不知道如何获得每个表格行相关的值...

标签: c#entity-frameworkpostmodel-view-controller

解决方案


推荐阅读