ASP.net核心Crud有一个以上的密钥

时间:2017-03-14 14:21:27

标签: asp.net-mvc model-view-controller

我在CRUD中遇到问题,表中有一个以上的键。我在创建,更新和详细信息中解决了问题,但在删除中我无法解决。我可以打开删除页面,但在Controller.DeleteConfirmed()我无法通过2个密钥。

以下是视图代码(delete.cshtml):

@model Teste_db.Models.Vdcped

@{
    ViewData["Title"] = "Delete";
}

<h2>Delete</h2>

<h3>Are you sure you want to delete this?</h3>
<div>
    <h4>Vdcped</h4>
    <hr />
    <dl class="dl-horizontal">
        <input type="hidden" asp-for="PedEmpCod" />
        <input type="hidden" asp-for="PedNum" />
        <dt>
            @Html.DisplayNameFor(model => model.PedSituacao)
        </dt>
        <dd>
            @Html.DisplayFor(model => model.PedSituacao)
        </dd>
        <dt>
            @Html.DisplayNameFor(model => model.PedCliCgc)
        </dt>
        <dd>
            @Html.DisplayFor(model => model.PedCliCgc)
        </dd>
        <dt>
            @Html.DisplayNameFor(model => model.PedArcCod)
        </dt>
        <dd>
            @Html.DisplayFor(model => model.PedArcCod)
        </dd>
        <dt>
            @Html.DisplayNameFor(model => model.PedVenCod)
        </dt>
        <dd>
            @Html.DisplayFor(model => model.PedVenCod)
        </dd>
        <dt>
            @Html.DisplayNameFor(model => model.PedComiss2)
        </dt>
        <dd>
            @Html.DisplayFor(model => model.PedComiss2)
        </dd>
            @Html.DisplayNameFor(model => model.PedDtemi)
        </dt>
        <dd>
            @Html.DisplayFor(model => model.PedDtemi)
        </dd>
        <dt>
            @Html.DisplayNameFor(model => model.PedDtultentrega)
        </dt>
        <dd>
            @Html.DisplayFor(model => model.PedDtultentrega)
        </dd>
        <dt>
            @Html.DisplayNameFor(model => model.PedDtpgto)
        </dt>
        <dd>
            @Html.DisplayFor(model => model.PedDtpgto)
        </dd>
        <dt>
            @Html.DisplayNameFor(model => model.PedCpgCod)
        </dt>
        <dd>
            @Html.DisplayFor(model => model.PedCpgCod)
        </dd>
        <dt>
            @Html.DisplayNameFor(model => model.PedTipo)
        </dt>
        <dd>
            @Html.DisplayFor(model => model.PedTipo)
        </dd>
        <dt>
            @Html.DisplayNameFor(model => model.PedVlmerc)
        </dt>
        <dd>
            @Html.DisplayFor(model => model.PedVlmerc)
        </dd>
        <dt>
            @Html.DisplayNameFor(model => model.PedVlacresc)
        </dt>
        <dd>
            @Html.DisplayFor(model => model.PedVlacresc)
        </dd>
        <dt>
            @Html.DisplayNameFor(model => model.PedVldesc)
        </dt>
        <dd>
            @Html.DisplayFor(model => model.PedVldesc)
        </dd>
        <dt>
            @Html.DisplayNameFor(model => model.PedVlavista)
        </dt>
        <dd>
            @Html.DisplayFor(model => model.PedVlavista)
        </dd>
        <dt>
            @Html.DisplayNameFor(model => model.PedVltot)
        </dt>
        <dd>
            @Html.DisplayFor(model => model.PedVltot)
        </dd>
        <dt>
            @Html.DisplayNameFor(model => model.PedVlicmretido)
        </dt>
        <dd>
            @Html.DisplayFor(model => model.PedVlicmretido)
        </dd>
        <dt>
            @Html.DisplayNameFor(model => model.PedDesconto)
        </dt>
        <dd>
            @Html.DisplayFor(model => model.PedDesconto)
        </dd>
        <dt>
            @Html.DisplayNameFor(model => model.PedVlfrete)
        </dt>
        <dd>
            @Html.DisplayFor(model => model.PedVlfrete)
        </dd>
        <dt>
            @Html.DisplayNameFor(model => model.PedNrpedcli)
        </dt>
        <dd>
            @Html.DisplayFor(model => model.PedNrpedcli)
        </dd>
        <dt>
            @Html.DisplayNameFor(model => model.PedAcresc)
        </dt>
        <dd>
            @Html.DisplayFor(model => model.PedAcresc)
        </dd>
        <dt>
            @Html.DisplayNameFor(model => model.PedNfsimpfat)
        </dt>
        <dd>
            @Html.DisplayFor(model => model.PedNfsimpfat)
        </dd>
        <dt>
            @Html.DisplayNameFor(model => model.PedSimplesfat)
        </dt>
        <dd>
            @Html.DisplayFor(model => model.PedSimplesfat)
        </dd>
        <dt>
            @Html.DisplayNameFor(model => model.PedNfrem)
        </dt>
        <dd>
            @Html.DisplayFor(model => model.PedNfrem)
        </dd>
        <dt>
            @Html.DisplayNameFor(model => model.PedVlicmsubst)
        </dt>
        <dd>
            @Html.DisplayFor(model => model.PedVlicmsubst)
        </dd>
        <dt>
            @Html.DisplayNameFor(model => model.PedQtdparc)
        </dt>
        <dd>
            @Html.DisplayFor(model => model.PedQtdparc)
        </dd>
        <dt>
            @Html.DisplayNameFor(model => model.PedVlparcini)
        </dt>
        <dd>
            @Html.DisplayFor(model => model.PedVlparcini)
        </dd>
        <dt>
            @Html.DisplayNameFor(model => model.PedVlparcela)
        </dt>
        <dd>
            @Html.DisplayFor(model => model.PedVlparcela)
        </dd>
        <dt>
            @Html.DisplayNameFor(model => model.PedVldesc2)
        </dt>
        <dd>
            @Html.DisplayFor(model => model.PedVldesc2)
        </dd>
        <dt>
            @Html.DisplayNameFor(model => model.PedIndfrete)
        </dt>
        <dd>
            @Html.DisplayFor(model => model.PedIndfrete)
        </dd>
        <dt>
            @Html.DisplayNameFor(model => model.PedPrazo)
        </dt>
        <dd>
            @Html.DisplayFor(model => model.PedPrazo)
        </dd>
        <dt>
            @Html.DisplayNameFor(model => model.PedTpgCod)
        </dt>
        <dd>
            @Html.DisplayFor(model => model.PedTpgCod)
        </dd>
        <dt>
            @Html.DisplayNameFor(model => model.PedLocal)
        </dt>
        <dd>
            @Html.DisplayFor(model => model.PedLocal)
        </dd>
        <dt>
            @Html.DisplayNameFor(model => model.PedPedtroca)
        </dt>
        <dd>
            @Html.DisplayFor(model => model.PedPedtroca)
        </dd>
        <dt>
            @Html.DisplayNameFor(model => model.PedVlbaseicms)
        </dt>
        <dd>
            @Html.DisplayFor(model => model.PedVlbaseicms)
        </dd>
        <dt>
            @Html.DisplayNameFor(model => model.PedVlicms)
        </dt>
        <dd>
            @Html.DisplayFor(model => model.PedVlicms)
        </dd>
        <dt>
            @Html.DisplayNameFor(model => model.PedPorCod)
        </dt>
        <dd>
            @Html.DisplayFor(model => model.PedPorCod)
        </dd>
        <dt>
            @Html.DisplayNameFor(model => model.PedStpCod)
        </dt>
        <dd>
            @Html.DisplayFor(model => model.PedStpCod)
        </dd>
        <dt>
            @Html.DisplayNameFor(model => model.PedRotCod)
        </dt>
        <dd>
            @Html.DisplayFor(model => model.PedRotCod)
        </dd>
        <dt>
            @Html.DisplayNameFor(model => model.PedOcgNum)
        </dt>
        <dd>
            @Html.DisplayFor(model => model.PedOcgNum)
        </dd>
        <dt>
            @Html.DisplayNameFor(model => model.PedVlbasesubst)
        </dt>
        <dd>
            @Html.DisplayFor(model => model.PedVlbasesubst)
        </dd>
        <dt>
            @Html.DisplayNameFor(model => model.PedPesoliq1)
        </dt>
        <dd>
            @Html.DisplayFor(model => model.PedPesoliq1)
        </dd>
        <dt>
            @Html.DisplayNameFor(model => model.PedPesoliq2)
        </dt>
        <dd>
            @Html.DisplayFor(model => model.PedPesoliq2)
        </dd>
        <dt>
            @Html.DisplayNameFor(model => model.PedMotCod)
        </dt>
        <dd>
            @Html.DisplayFor(model => model.PedMotCod)
        </dd>
        <dt>
            @Html.DisplayNameFor(model => model.PedDtpreentrega)
        </dt>
        <dd>
            @Html.DisplayFor(model => model.PedDtpreentrega)
        </dd>
        <dt>
            @Html.DisplayNameFor(model => model.PedCliCod)
        </dt>
        <dd>
            @Html.DisplayFor(model => model.PedCliCod)
        </dd>
        <dt>
            @Html.DisplayNameFor(model => model.PedVlpago)
        </dt>
        <dd>
            @Html.DisplayFor(model => model.PedVlpago)
        </dd>
        <dt>
            @Html.DisplayNameFor(model => model.PedDtimport)
        </dt>
        <dd>
            @Html.DisplayFor(model => model.PedDtimport)
        </dd>
        <dt>
            @Html.DisplayNameFor(model => model.PedStimport)
        </dt>
        <dd>
            @Html.DisplayFor(model => model.PedStimport)
        </dd>
        <dt>
            @Html.DisplayNameFor(model => model.PedHoraini)
        </dt>
        <dd>
            @Html.DisplayFor(model => model.PedHoraini)
        </dd>
        <dt>
            @Html.DisplayNameFor(model => model.PedHorafin)
        </dt>
        <dd>
            @Html.DisplayFor(model => model.PedHorafin)
        </dd>
        <dt>
            @Html.DisplayNameFor(model => model.PedDtvis)
        </dt>
        <dd>
            @Html.DisplayFor(model => model.PedDtvis)
        </dd>
        <dt>
            @Html.DisplayNameFor(model => model.PedObsvis)
        </dt>
        <dd>
            @Html.DisplayFor(model => model.PedObsvis)
        </dd>
        <dt>
            @Html.DisplayNameFor(model => model.PedNfiSerie)
        </dt>
        <dd>
            @Html.DisplayFor(model => model.PedNfiSerie)
        </dd>
        <dt>
            @Html.DisplayNameFor(model => model.PedTnfCod)
        </dt>
        <dd>
            @Html.DisplayFor(model => model.PedTnfCod)
        </dd>
        <dt>
            @Html.DisplayNameFor(model => model.PedVlipi)
        </dt>
        <dd>
            @Html.DisplayFor(model => model.PedVlipi)
        </dd>
        </dd>
    </dl>

    <form asp-action="Delete">
        <div class="form-actions no-color">
            <input type="submit" value="Delete" class="btn btn-default"  /> |
            <a asp-action="Index">Back to List</a>
        </div>
    </form>
</div>

VdcpedController的代码。 在删除中可以使用id和ped,但在DeleteConfirmed中,id为OK,ped为空。

public async Task<IActionResult> Delete(string id, Int32 ped)
{   
    if (id == null)
    {
        return NotFound();
    }

    var vdcped = await _context.Vdcped
        .SingleOrDefaultAsync(m => m.PedEmpCod == id && m.PedNum == ped);
    if (vdcped == null)
    {
        return NotFound();
    }

    return View(vdcped);
}

// POST: Vdcped/Delete/5        
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public async Task<IActionResult> DeleteConfirmed(string id, Int32 ped)
{

    var vdcped = await _context.Vdcped.SingleOrDefaultAsync(m => m.PedEmpCod == m.PedEmpCod && m.PedNum == ped);
    _context.Remove(vdcped);
    await _context.SaveChangesAsync();
    return RedirectToAction("Index");
}

1 个答案:

答案 0 :(得分:0)

考虑将隐藏的输入移动到表单标记的范围内:

<form asp-action="Delete">
    <input type="hidden" asp-for="PedEmpCod" />
    <input type="hidden" asp-for="PedNum" />

    <div class="form-actions no-color">
        <input type="submit" value="Delete" class="btn btn-default"  /> |
        <a asp-action="Index">Back to List</a>
    </div>
</form>

这样,在提交表单时,字段实际上会提交给控制器。

此外,您可能需要将DeleteConfirmed的签名更改为以下内容:

public async Task<IActionResult> DeleteConfirmed(string id, Int32 PedNum)