当前位置: 首页 > 知识库问答 >
问题:

PartialView-如何在视图接收IEnumerable时传递单个对象

贾沛
2023-03-14

我有一个带控制器的模型(机器)。在索引操作“IActionResult”中收集机器列表并将其发送到索引视图的控制器。

public async Task<IActionResult> Index(string sortOrder, string searchString)
{
    ViewData["NameSortParm"] = string.IsNullOrEmpty(sortOrder) ? "nombre_desc" : "";
    ViewData["DateSortParm"] = sortOrder == "Date" ? "date_desc" : "Date";
    ViewData["CurrentFilter"] = searchString;

    var maquinas = from s in _context.Machines
                   select s;
    if (!String.IsNullOrEmpty(searchString))
    {
        maquinas = maquinas.Where(s => s.MchName.Contains(searchString));
    }
    switch (sortOrder)
    {
        case "nombre_desc":
            maquinas = maquinas.OrderByDescending(s => s.MchName);
            break;
        case "Date":
            maquinas = maquinas.OrderBy(s => s.FechaCompra);
            break;
        case "date_desc":
            maquinas = maquinas.OrderByDescending(s => s.FechaCompra);
            break;
        default:
            maquinas = maquinas.OrderBy(s => s.MchName);
            break;
    }
    return View(await _context.Machines.Include(t => t.MachineTypes).AsNoTracking().ToListAsync());
}

在索引中,我在一张表上列出了以下列表:

@model IEnumerable<Application.Models.Machine>

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

<h2>Index</h2>

<p>
    <a asp-action="Create">Create New</a>
</p>
<form asp-action="Index" method="get">
    <div class="form-actions no-color">
        <p>
            Search by name: <input type="text" name="SearchString" value="@ViewData["currentFilter"]" />
            <input type="submit" value="Search" class="btn btn-default" /> |
            <a asp-action="Index">Volver a lista completa</a>
        </p>
    </div>
</form>
<table class="table">
    <thead>
        <tr> 
                <th>
                    <a asp-action="Index" asp-route-sortOrder="@ViewData["NameSortParm"]">@Html.DisplayNameFor(model => model.MchName)</a>
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.MachineTypes.TypeDescription)
                </th>
                <th>
                    <a asp-action="Index" asp-route-sortOrder="@ViewData["DateSortParm"]">@Html.DisplayNameFor(model => model.FechaCompra)</a>
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.CostoMaq)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.PUnit)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.FechaPUnit)
                </th>
        </tr>
    </thead>
    <tbody>
@foreach (var item in Model) {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.MchName)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.MachineTypes.TypeDescription)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.FechaCompra)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.CostoMaq)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.PUnit)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.FechaPUnit)
            </td>
            <td>
                <a asp-action="Edit" asp-route-id="@item.Id">Edit</a> |
                <a asp-action="Details" asp-route-id="@item.Id">Details</a> |
                <a asp-action="Delete" asp-route-id="@item.Id">Delete</a>
            </td>
        </tr>
}
    </tbody>
</table>

如您所见,我有一个名为“创建”的asp操作,它打开创建视图,用户可以在其中注册一台新机器。

以下是来自机器控制器的Create()代码:注意:Create是一个视图,还不是PartialView

public IActionResult Create()
{
    PopulateMachineTypeDropDownList();
    PopulateMachineTypeDropDownListSupplier();
    PopulateMachineTypeDropDownListStore();

    return View();
}

目标:

从ndex.cshtml内部调用创建窗体作为模式(作为部分视图?)

为此,我编辑了Index.cshtml并将其转换为:

<a asp-action="Create">Create New</a>

为此:

<a data-toggle="modal" data-target="CreateModal">Create New</a>

此外,我在表的末尾插入了模式代码,列出了我们现有的机器,并尝试将Create视图作为部分视图调用。(当然失败了)。模态代码:

<div class="modal fade" id="CreateModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title" id="myModalLabel">Modal title</h4>
            </div>
            <div class="modal-body">
                @Html.RenderPartial("Create", Model)
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                <button type="button" class="btn btn-primary">Save changes</button>
            </div>
        </div>
    </div>
</div>

我相信这失败的原因有很多,在没有定义为partialview的东西上使用RenderPartial就是其中之一,我希望这是主要原因。

问题:

我应该遵循哪些步骤将Create视图转换为Index.cshtml中的partialview,并将其显示为模式表单?

顺便说一下,这是目前创建视图的代码:

@model Application.Models.Machine

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

<h2>Create</h2>

<form asp-action="Create">

        <h4>Machine</h4>
        <hr />
        <div asp-validation-summary="ModelOnly" class="text-danger"></div>
        <div class="form-group">
            <label asp-for="TypeID" class="col-md-2 control-label"></label>
            <div class="col-md-10">
                <select asp-for="TypeID" class="form-control" asp-items="ViewBag.TypeID">
                    <option value="">-- Seleccione Tipo --</option>
                </select>
                <span asp-validation-for="TypeID" class="text-danger"></span>
            </div>
        </div>
        <div class="form-group">
            <label asp-for="SupplierID" class="col-md-2 control-label">    </label>
            <div class="col-md-10">
                <select asp-for="SupplierID" class="form-control" asp-items="ViewBag.SupplierID">
                    <option value="">-- Seleccione Proveedor --</option>
                </select>
                <span asp-validation-for="SupplierID" class="text-danger">    </span>
            </div>
        </div>
        <div class="form-group">
            <label asp-for="StoreID" class="col-md-2 control-label"></label>
            <div class="col-md-10">
                <select asp-for="StoreID" class="form-control" asp-items="ViewBag.StoreID">
                    <option value="">-- Seleccione Tienda --</option>
                </select>
                <span asp-validation-for="StoreID" class="text-danger"></span>
            </div>
        </div>
        <div class="form-group">
            <label asp-for="MchName" class="col-md-2 control-label"></label>
            <div class="col-md-10">
                <input asp-for="MchName" class="form-control" />
                <span asp-validation-for="MchName" class="text-danger"></span>
            </div>
        </div>
        <div class="form-group">
            <label asp-for="FechaCompra" class="col-md-2 control-label"></label>
            <div class="col-md-10">
                <input asp-for="FechaCompra" class="form-control" />
                <span asp-validation-for="FechaCompra" class="text-danger"></span>
            </div>
        </div>
        <div class="form-group">
            <label asp-for="CostoMaq" class="col-md-2 control-label"></label>
            <div class="col-md-10">
                <input asp-for="CostoMaq" class="form-control" />
                <span asp-validation-for="CostoMaq" class="text-danger"></span>
            </div>
        </div>
        <div class="form-group">
            <label asp-for="PUnit" class="col-md-2 control-label"></label>
            <div class="col-md-10">
                <input asp-for="PUnit" class="form-control" />
                <span asp-validation-for="PUnit" class="text-danger"></span>
            </div>
        </div>
        <div class="form-group">
            <label asp-for="FechaPUnit" class="col-md-2 control-label"></label>
            <div class="col-md-10">
                <input asp-for="FechaPUnit" class="form-control" />
                <span asp-validation-for="FechaPUnit" class="text-danger"></span>
            </div>
        </div>
        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Create" class="btn btn-default" />
            </div>
        </div>

</form>

<div>
    <a asp-action="Index">Back to List</a>
</div>

@section Scripts {
    @{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}

共有2个答案

周翼
2023-03-14

这是我的旧代码,我想它会帮助你的。它与您的不同,但您将获得
局部视图:

@model App.Domain.PostOffice
        <div class="modal fade" id="Addmodal" tabindex="-1" role="dialog" aria-labelledby="edit" aria-hidden="true" data-keyboard="false" data-backdrop="static">
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                        <h4 class="modal-title">Post Office</h4>
                    </div>
                    <div class="modal-body form-horizontal ">
                        <div class="row">
                            <div class="col-md-12">
                                <div class="row" id="rptshow">
                                    <div class="col-sm-12">
                                        <div class="card-box table-responsive">
                                            <div class="form-group">
                                                @Html.HiddenFor(x => x.Id)
                                                @Html.LabelFor(x => x.PoId, new { @class = "col-md-2 control-label" })
                                                <div class="col-sm-6">
                                                    @Html.TextBoxFor(x => x.PoId, new { @class = "form-control col-md-4", disabled = "disabled" })
                                                </div>
                                            </div>
                                            <div class="form-group">
                                                @Html.LabelFor(x => x.Name, new { @class = "col-md-2 control-label" })
                                                <div class="col-sm-6">
                                                    @Html.TextBoxFor(x => x.Name, new { @class = "form-control col-md-4", required = "required" })
                                                </div>
                                            </div>
                                            <div class="form-group">
                                                @Html.LabelFor(x => x.Address, new { @class = "col-md-2 control-label" })
                                                <div class="col-sm-6">
                                                    @Html.TextBoxFor(x => x.Address, new { @class = "form-control col-md-4", required = "required", })
                                                </div>
                                            </div>
                                            <div class="form-group">
                                                @Html.LabelFor(x => x.PostCode, new { @class = "col-md-2 control-label" })
                                                <div class="col-sm-6">
                                                    @Html.TextBoxFor(x => x.PostCode, new { @class = "form-control col-md-4", required = "required", data_parsley_minlength = "4", type = "number", palceholder = "" })
                                                </div>
                                            </div>
                                            <div class="form-group">
                                                @Html.LabelFor(x => x.Hpo, new { @class = "col-md-2 control-label" })
                                                <div class="col-sm-6">
                                                    @Html.TextBoxFor(x => x.Hpo, new { @class = "form-control col-md-4", required = "required", data_parsley_Maxlength = "2", data_parsley_minlength = "2", type = "number", palceholder = "" })
                                                </div>
                                            </div>
                                            <div class="form-group">
                                                @Html.LabelFor(x => x.Gpo, new { @class = "col-md-2 control-label" })
                                                <div class="col-sm-6">
                                                    @Html.TextBoxFor(x => x.Gpo, new { @class = "form-control col-md-4", required = "required", data_parsley_Maxlength = "2", data_parsley_minlength = "2", type = "number", palceholder = "" })
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="col-md-1"></div>
                        </div>

                    </div>

                    <div class="modal-footer">
                        <button type="button" class="btn btn-default waves-effect" data-dismiss="modal">Close</button>
                        <button type="submit" class="btn btn-info waves-effect waves-light" id="add">Save changes</button>
                    </div>

                </div>
            </div>

        </div>  

@模型App.Domain.PostOffice

<div class="modal fade" id="Addmodal" tabindex="-1" role="dialog" aria-labelledby="edit" aria-hidden="true" data-keyboard="false" data-backdrop="static">
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                        <h4 class="modal-title">Post Office</h4>
                    </div>
                    <div class="modal-body form-horizontal ">
                        <div class="row">
                            <div class="col-md-12">
                                <div class="row" id="rptshow">
                                    <div class="col-sm-12">
                                        <div class="card-box table-responsive">
                                            <div class="form-group">
                                                @Html.HiddenFor(x => x.Id)
                                                @Html.LabelFor(x => x.PoId, new { @class = "col-md-2 control-label" })
                                                <div class="col-sm-6">
                                                    @Html.TextBoxFor(x => x.PoId, new { @class = "form-control col-md-4", disabled = "disabled" })
                                                </div>
                                            </div>
                                            <div class="form-group">
                                                @Html.LabelFor(x => x.Name, new { @class = "col-md-2 control-label" })
                                                <div class="col-sm-6">
                                                    @Html.TextBoxFor(x => x.Name, new { @class = "form-control col-md-4", required = "required" })
                                                </div>
                                            </div>
                                            <div class="form-group">
                                                @Html.LabelFor(x => x.Address, new { @class = "col-md-2 control-label" })
                                                <div class="col-sm-6">
                                                    @Html.TextBoxFor(x => x.Address, new { @class = "form-control col-md-4", required = "required", })
                                                </div>
                                            </div>
                                            <div class="form-group">
                                                @Html.LabelFor(x => x.PostCode, new { @class = "col-md-2 control-label" })
                                                <div class="col-sm-6">
                                                    @Html.TextBoxFor(x => x.PostCode, new { @class = "form-control col-md-4", required = "required", data_parsley_minlength = "4", type = "number", palceholder = "" })
                                                </div>
                                            </div>
                                            <div class="form-group">
                                                @Html.LabelFor(x => x.Hpo, new { @class = "col-md-2 control-label" })
                                                <div class="col-sm-6">
                                                    @Html.TextBoxFor(x => x.Hpo, new { @class = "form-control col-md-4", required = "required", data_parsley_Maxlength = "2", data_parsley_minlength = "2", type = "number", palceholder = "" })
                                                </div>
                                            </div>
                                            <div class="form-group">
                                                @Html.LabelFor(x => x.Gpo, new { @class = "col-md-2 control-label" })
                                                <div class="col-sm-6">
                                                    @Html.TextBoxFor(x => x.Gpo, new { @class = "form-control col-md-4", required = "required", data_parsley_Maxlength = "2", data_parsley_minlength = "2", type = "number", palceholder = "" })
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="col-md-1"></div>
                        </div>

                    </div>

                    <div class="modal-footer">
                        <button type="button" class="btn btn-default waves-effect" data-dismiss="modal">Close</button>
                        <button type="submit" class="btn btn-info waves-effect waves-light" id="add">Save changes</button>
                    </div>

                </div>
            </div>

        </div>  

主要观点:

@{
        ViewBag.Title = "Create";
        Layout = "~/Views/Shared/_Layout.cshtml";
        List<App.Domain.PostOffice> items = ViewBag.Items;
    }

    <link href="~/App_Themes/Theme1/plugins/datatables/jquery.dataTables.min.css" rel="stylesheet" type="text/css" />
    <link href="~/App_Themes/Theme1/assets/css/pages.css" rel="stylesheet" type="text/css">
    <link href="../App_Themes/Theme1/assets/css/components.css" rel="stylesheet" type="text/css">
    <style type="text/css">
        input[type="text"] {
            border: 1px solid #00ffff;
            background-color: white;
        }

        input[type="number"] {
            border: 1px solid #00ffff;
            background-color: white;
        }

        th {
            color: white;
        }
    </style>
    <div>
        <button id="addModalButton" value="Add" class="btn btn-primary">Add</button>
    </div>
    @using (Html.BeginForm())
    {
        <div id="modalDiv">

        </div>

        <div class="row">
            <div class="col-md-10">
                <br /><h4 class="m-t-0 header-title"><b>Existing Post Office List </b></h4><br />
                <table id="datatable-buttons" class="table table-striped table-bordered">
                    <thead style="background:#4c5667;">
                        <tr>
                            <th>SL.</th>
                            <th>PO ID No</th>
                            <th>PO Name</th>
                            <th>PO Address</th>
                            <th>Post Code</th>
                            <th>HPO</th>
                            <th>GPO</th>
                            <th>Create By</th>
                            <th>Action</th>
                        </tr>
                    </thead>
                    <tbody id="postList">
                        @{
        int i = 0;
        foreach (var item in items)
        {
            i = i + 1;
            <tr>
                <td>@i</td>
                <td>@item.PoId</td>
                <td>@item.Name</td>
                <td>@item.Address</td>
                <td>@item.PostCode</td>
                <td>@item.Hpo</td>
                <td>@item.Gpo</td>
                <td>@item.EntryUser</td>
                <td>
                    <a href="#" class="on-default edit-row editSup" data-id="@item.Id" id="edit" value="@item.Id"><i class="fa fa-pencil"></i></a>&nbsp;&nbsp;&nbsp;&nbsp;
                </td>

            </tr>
        }
                        }

                    </tbody>
                </table>
                <br />
            </div>
            <div class="col-md-1"></div>
        </div>
    }

控制器:

public ActionResult CreatePostOffice()
        {
            ViewBag.Items = _postOfficeService.All().ToList();
            return View();
        }

        public ActionResult CreatePosto(PostOffice aPostOffice)
        {
            if(ModelState.IsValid)
            {
                var lastPo = _postOfficeService.All().ToList().FirstOrDefault(x => x.PoId == aPostOffice.PoId.Trim());
                if(lastPo!=null)
                {
                    aPostOffice.PoId = (Convert.ToInt32(lastPo.PoId) + 1).ToString().PadLeft(4,'0');
                }

                aPostOffice.EntryUser = User.Identity.Name;
                aPostOffice.EntryDate = DateTime.Now;
                _postOfficeService.Add(aPostOffice);
                _postOfficeService.Save();
            }

            return RedirectToAction("CreatePostOffice");
        }
申黎明
2023-03-14

进展:

好的,因为我无法将单个模型渲染到索引视图中,以供PartialView(“创建”)使用,所以我执行了以下操作:

            @foreach (var defectsVM in Model)
            {
                Html.RenderPartial("Create", defectsVM);

            }

好的,没有错误,但是当我点击“创建”链接时,它会弹出创建表单(yey),当然,它会重复多次,并将每个寄存器的数据放入机器表中。

我怎么能只带一个干净的呢?

 类似资料:
  • 请帮助如何使其工作

  • 我开始从clic项目recyclerview为我的所有人员列表发送数据,但我需要使用activity 2中的一些信息,我只是传递人员的姓名 Nb:person对象(id,name,phone,email,post) 这是click item Recolyer视图适配器的代码 @override public void onClick(View v){ */Context.StartActivity

  • 我对RxJava很陌生,每当我有一个情况,我需要从链上的一个可观察的返回数据传递到调用“订阅” - 我很难理解如何在没有任何补丁的情况下以“反应式”方式做到这一点...... 例如: 我想发出obs1和obs2,得到它们的结果,然后发出obs3然后obs4,然后以订阅结束链,同时可以访问obs1、obs2、obs3和obs4的结果。 调用的顺序很重要,我需要在执行obs3之前完成obs1和obs2

  • 环境: ASP. NET Core MVC 3.1和库。NET标准 目标: > 从DLL创建对象(DLL可以随时更改),没有常量结构,例如类Obj{int,string,string},但另一个DLL将具有类Obj{int,int,int}。 将此对象传递到视图。 生成单个“表单框”,而不知道哪些属性有对象。例如。字符串= 将此数据传递给方法/控制器/api。 我的问题是: 在那一刻,我可以看到前

  • 我想知道如何将绑定变量向下传递到滑块视图 如果我将@State更改为@绑定,它会显示很多错误消息,如下所示 无法将“Double”类型的值转换为指定的“Binding”类型 调用中的无关参数标签“wrapedValue:” 无法推断泛型参数“V” 但是,我想让它对该变量具有@Binding,因为我将在另一个视图中使用此视图。 有什么解决办法吗?

  • 我正在使用RxJava链接异步操作,我想向下游传递一些变量: 这似乎是一种常见的模式,但我找不到有关它的信息。