Asp.net Core 我无法从表单发送多个数据

问题描述 投票:0回答:1

我必须吃饭 员工 个人电脑 我创建关系 M:M >> EmployeePc 并创建 ViewModel > EmployePCVWModel

从View发送数据到Controller时的问题 不仅仅是员工来,电脑不来

这是我查看的代码

//////////////////////

@模型 ITProject.ViewModels.EmployePCVWModel



<!-- Main content -->

<section class="content">
    <div class="row">
        <div class="col-xs-6 col-sm-8 col-md-6 col-lg-12">

            <!-- /.card -->

            <div class="card border-secondary mb-3">
                <div class="card-header">
                    <h5 class="page-title"> Assign PC \ Laptop</h5>
                </div>

                <form class="forms-sample" method="post" enctype="multipart/form-data"
                      asp-controller="EmployeePc" asp-action="Save" role="form">

                    <div class="row">

                        <div class="col-md-5 grid-margin stretch-card">

                            <div class="card-body">

                                @* <input asp-for="ItemId" type="hidden" />
                                <input asp-for="CreatedBy" type="hidden" />
                                <input asp-for="CreatedDate" type="hidden" />
                                <input asp-for="CurrentState" type="hidden" />
                                <input asp-for="ImageName" type="hidden" />*@

                                <div class="col-md-6 sm-3">

                                    <img src="@string.Format("/Uploads/Employee/{0}",Model.TBEmployee?.EmployeeImage)"
                                         class="w3-card-4" alt="Person"
                                         height="110" width="100"
                                         asp-append-version="true" />
                                    <br />

                                    <label>Number :</label>

                                    <input type="hidden" asp-for="TBEmployee!.EmployeeId" />    @* لحل مشكلة ان ال اي دي لا يساوى 0 عند التعديل*@

                                    <input type="text" readonly="readonly" asp-for="TBEmployee!.EmployeeNumber" class="form-control col-md-6 sm-3">
                                    <span class="label danger alert-danger" asp-validation-for="TBEmployee!.EmployeeNumber"></span>

                                </div>
                                <div class="row col-lg-8 md-7 sm-3">

                                    <label class="m-2">Name :</label>
                                    <input type="text" readonly="readonly" asp-for="TBEmployee!.EmployeeName" class="form-control">

                                    <span class="label danger alert-danger" asp-validation-for="TBEmployee!.EmployeeName"></span>

                                </div>


                            </div>

                        </div>

                        @* ////////////////LapTop////////////////*@

                        <div class="col-md-7">
                            <div class="card-body">
                                <h4 class="card-title">Device Specification</h4>
                                <br /><br />
                                <div class="row">
                                    <div>
                                        <table class="table table-bordered table-striped dataTable dtr-inline"
                                               style="overflow-y:scroll" role="grid">
                                            <thead>
                                                <tr role="row">
                                                    <th class="sorting_asc text-center">Pc ID</th>
                                                    <th class="sorting_asc text-center">Pc Model</th>
                                                    <th class="sorting text-center">Serial \ Mac address</th>
                                                    <th class="sorting text-center">Actions</th>
                                            </thead>

                                            <tbody>
                                                @foreach (var pc in Model.TBPc)
                                                {
                                                    <tr>
                                                        @*<input type="hidden" asp-for="@pc.Id" />*@
                                                        <td class="text-center">@pc.PcId</td>
                                                        <td class="text-center">@pc.PcName</td>
                                                        <td class="text-center">@pc.PcSerial</td>

                                                        <td class="text-center">
                                                            @*  <a  asp-action="Save" asp-route-id="@pc.PcId" class="btn btn-outline-success bi bi-plus"></a>
                                                        *@

                                                            <input class="form-check-input" id="@pc.PcId" type="checkbox" value="@pc.PcId">
                                                        </td>
                                                    </tr>
                                                }
                                            </tbody>

                                        </table>
                                    </div>
                                    <br />
                                    <button style='margin-right:86px; width:150px;' type="submit" class="btn btn-outline-success">@ViewBag.ButtonName</button>

                                    <a asp-controller="EmployeePc" asp-action="List" style="width:100px;" class="btn btn-outline-danger"> Back </a>

                                </div>

                            </div>

                        </div>
                    </div>
                </form>

            </div>
        </div>
    </div>
</section>

/////////////////////////

这款大众车型

enter image description here

这个控制器当数据到来时

enter image description here

TBPc 归零

请帮忙

我希望数据到达控制器

asp.net-mvc asp.net-core asp.net-mvc-4
1个回答
0
投票

由于您是从表单发送模型,请尝试使用 HtmlHelper

@using (Html.BeginForm("Save", "EmployeePc", FormMethod.Post)) { for (var i = 0; i < @Model.PC.Count; i++) { @Html.HiddenFor(m => m.PC[i].PCName) @Html.DisplayName(@Model.PC[i].PCName)
@Html.TextBoxFor(m => m.PC[i].PCId, new { htmlAttributes = new { @type = "number", @min = 0} })
}
}

在你看来

<section class="content">
    <div class="row">
        <div class="col-xs-6 col-sm-8 col-md-6 col-lg-12">

            <!-- /.card -->

            <div class="card border-secondary mb-3">
                <div class="card-header">
                    <h5 class="page-title"> Assign PC \ Laptop</h5>
                </div>

                <form class="forms-sample" method="post" enctype="multipart/form-data"
                      asp-controller="EmployeePc" asp-action="Save" role="form">

                    <div class="row">

                        <div class="col-md-5 grid-margin stretch-card">

                            <div class="card-body">

                                @* <input asp-for="ItemId" type="hidden" />
                                <input asp-for="CreatedBy" type="hidden" />
                                <input asp-for="CreatedDate" type="hidden" />
                                <input asp-for="CurrentState" type="hidden" />
                                <input asp-for="ImageName" type="hidden" />*@

                                <div class="col-md-6 sm-3">

                                    <img src="@string.Format("/Uploads/Employee/{0}",Model.TBEmployee?.EmployeeImage)"
                                         class="w3-card-4" alt="Person"
                                         height="110" width="100"
                                         asp-append-version="true" />
                                    <br />

                                    <label>Number :</label>

                                    <input type="hidden" asp-for="TBEmployee!.EmployeeId" />    @* لحل مشكلة ان ال اي دي لا يساوى 0 عند التعديل*@

                                    <input type="text" readonly="readonly" asp-for="TBEmployee!.EmployeeNumber" class="form-control col-md-6 sm-3">
                                    <span class="label danger alert-danger" asp-validation-for="TBEmployee!.EmployeeNumber"></span>

                                </div>
                                <div class="row col-lg-8 md-7 sm-3">

                                    <label class="m-2">Name :</label>
                                    <input type="text" readonly="readonly" asp-for="TBEmployee!.EmployeeName" class="form-control">

                                    <span class="label danger alert-danger" asp-validation-for="TBEmployee!.EmployeeName"></span>

                                </div>


                            </div>

                        </div>

                        @* ////////////////LapTop////////////////*@

                        <div class="col-md-7">
                            <div class="card-body">
                                <h4 class="card-title">Device Specification</h4>
                                <br /><br />
                                <div class="row">
                                    <div>
                                        <table class="table table-bordered table-striped dataTable dtr-inline"
                                               style="overflow-y:scroll" role="grid">
                                            <thead>
                                                <tr role="row">
                                                    <th class="sorting_asc text-center">Pc ID</th>
                                                    <th class="sorting_asc text-center">Pc Model</th>
                                                    <th class="sorting text-center">Serial \ Mac address</th>
                                                    <th class="sorting text-center">Actions</th>
                                            </thead>

                                               //Replace your foreach with using HtmlHelper
                                           <tbody>
        <tr>
            
            <td class="text-center">@Model.PCId</td>
            <td class="text-center">@Model.PCName</td>

            <td class="text-center">
                @using (Html.BeginForm("Save", "EmployeePc", FormMethod.Post))
                {
                    for (var i = 0; i < @Model. TBPc.Count; i++)
                    {
                        @Html.HiddenFor(m => m. TBPc [i].PCName)
                        @Html.DisplayName(@Model. TBPc [i].PCName) <br />
                        @Html.TextBoxFor(m => m. TBPc [i].PCId, new { htmlAttributes = new { @type = "number", @min = 0} })
                        <br />
                    }
                    <br />
                    <input class="btn btn-primary" type="submit" value="Submit" />
                }

                @*<a asp-action="Save" asp-route-id="@pc.PCId" class="btn btn-outline-success bi bi-plus"></a>*@

                <input class="form-check-input" id="@Model.PCId" type="checkbox" value="@ Model.PCId" name="@ Model.PCId">
            </td>
        </tr>
</tbody>
                                        </table>
                                    </div>
                                    <br />
                                    <button style='margin-right:86px; width:150px;' type="submit" class="btn btn-outline-success">@ViewBag.ButtonName</button>

                                    <a asp-controller="EmployeePc" asp-action="List" style="width:100px;" class="btn btn-outline-danger"> Back </a>

                                </div>

                            </div>

                        </div>
                    </div>
                </form>

            </div>
        </div>
    </div>
</section>

添加 [FromForm] 绑定模型。

您的控制器

//[ValidateAntiForgeryToken]
[HttpPost]
public IActionResult Save([FromForm]EmployeePCVWModel employeePC)
{
    if(ModelState.IsValid)
    {
        //
    }
    //
    return View(employeePC);
}

测试

© www.soinside.com 2019 - 2024. All rights reserved.