Asp.net core PartialView-如何在视图接收IEnumerable时传递单个对象
我有一个带控制器的模型(机器)。在索引操作“IActionResult”中收集机器列表并将其发送到索引视图的控制器Asp.net core PartialView-如何在视图接收IEnumerable时传递单个对象,asp.net-core,bootstrap-modal,partial-views,Asp.net Core,Bootstrap Modal,Partial Views,我有一个带控制器的模型(机器)。在索引操作“IActionResult”中收集机器列表并将其发送到索引视图的控制器 public async Task<IActionResult> Index(string sortOrder, string searchString) { ViewData["NameSortParm"] = string.IsNullOrEmpty(sortOrder) ? "nombre_desc" : ""; ViewData["DateSor
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());
}
目标:
从Index.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">×</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>
&时代;
情态标题
@Html.RenderPartial(“创建”,模型)
接近
保存更改
我相信这失败的原因有很多,在没有定义为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");}
}
@model Application.Models.Machine
@{
ViewData[“标题”]=“创建”;
}
创造
机器
--提波岛酒店--
--Seleccione Proveedor--
--蒂恩达酒店--
返回列表
@节脚本{
@{wait Html.RenderPartialAsync(“_validationScript”);}
}
这是我的旧代码,我想它会对你有所帮助。它与您的不同,但您将获得局部视图:
@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>
进度: 好的,因为我无法将单个模型渲染到索引视图中,以供PartialView(“创建”)使用,所以我执行了以下操作:
@foreach (var defectsVM in Model)
{
Html.RenderPartial("Create", defectsVM);
}
好的,没有错误,但是当我点击“创建”链接时,它会弹出创建表单(yey),当然,它会重复多次,并将每个寄存器的数据放入机器表中
我怎么能只带一个干净的呢?你好。谢谢你的回答。我将数据从控制器传递到索引视图,如下所示:返回视图(wait_context.Machines.Include(t=>t.MachineTypes).AsNoTracking().ToListAsync());所以,我想我应该像这样使用RenderPartial:@Html.RenderPartial(“Create”,Models.Machines),但我得到的结果是:“Models”在实际上下文中不存在。你到底想传递什么机器列表。。。。如果是这样,您已经在@model IEnumerable中声明了。。。。所以你只需要写。。。。仅@Html.RenderPartial(“创建”,模型)。。。Not@Html.RenderPartial(“Create”,Models.Machines)收到此消息参数1:无法从'void'转换为'object'+@Html.RenderPartial(“Create”,Model):(还尝试了以下操作:@{Html.RenderPartial(“Create”,Model);}并收到相同的初始错误:“InvalidOperationException:传递到ViewDataDictionary的模型项的类型为'System.Collections.Generic.List'1[Application.Models.Machine]”,但此ViewDataDictionary实例需要'Application.Models.Machine'类型的模型项。“我知道了…如果您知道需要传递哪个模型..然后编写@Html.RenderPartial(“创建”,model)”[0])假设我要通过0索引模型
@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>
<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>
</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");
}
@foreach (var defectsVM in Model)
{
Html.RenderPartial("Create", defectsVM);
}