我有一个模型(机器)与它的控制器。控制器,它在Index action' 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>
正如你所看到的,我有一个名为&#34;创建&#34;的asp动作。这将打开Create视图,用户可以在其中注册新计算机。
以下是机器控制器中Create()的代码: 注意:“创建”是“视图”,而不是“部分视图”
public IActionResult Create()
{
PopulateMachineTypeDropDownList();
PopulateMachineTypeDropDownListSupplier();
PopulateMachineTypeDropDownListStore();
return View();
}
目的:
从Index.cshtml内部调用Create表单作为模态(作为局部视图?)
为此,我编辑了Index.cshtml并将其转换为:
<a asp-action="Create">Create New</a>
进入这个:
<a data-toggle="modal" data-target="CreateModal">Create New</a>
另外,我在表格末尾插入了模态代码,列出了我们库存的机器,并尝试将创建视图调用为部分视图。 (当然失败了)。模态代码:
<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>
我认为这个失败有很多原因,在未定义为部分视图的东西上使用RenderPartial是一个,我希望是主要原因。
问题:
我应该遵循哪些步骤将Create视图转换为Index.cshtml中的partialview并将其作为模式表单呈现?
顺便说一句,这是目前Create视图的代码:
@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");}
}
答案 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>
@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>
主要观点:
@{
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");
}
答案 1 :(得分:0)
<强>进度:强>
好的,因为我无法将单个模型渲染到PartialView(“Create”)使用的Index视图中,所以我这样做了:
@foreach (var defectsVM in Model)
{
Html.RenderPartial("Create", defectsVM);
}
好吧,没有错误,但是当我点击“创建”链接时,它会弹出创建表单(是的)但当然,它会多次执行并为Machine表中的每个寄存器提供数据。 / p>
我怎么才能带一个干净的呢?