我正在构建一个Asp.Net Core 2.1 EF MVC解决方案。
我没有在每个索引视图中为每个CRUD按钮(编辑,创建,删除)分别编写一行,而是尝试从局部视图动态生成按钮,如下所示。
模型/ AdmSchool
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using Microsoft.AspNetCore.Mvc.Rendering;
namespace FACEZ.Models
{
public partial class AdmSchool
{
[Display(Name = "Id")]
public int AdmSchoolId { get; set; }
[Required]
[MaxLength(10)]
[Display(Name = "Code")]
public string AdmSchoolCode { get; set; }
[Required]
[MaxLength(100)]
[Display(Name = "School Name")]
public string AdmSchoolDescr { get; set; }
[MaxLength(100)]
[Display(Name = "Generic Email Principal")]
public string EmailPrincipal { get; set; }
[MaxLength(100)]
[Display(Name = "Generic Email Accounts")]
public string EmailAccounts { get; set; }
[MaxLength(100)]
[Display(Name = "Generic Email Registrar")]
public string EmailRegistrar { get; set; }
[Display(Name = "Obsolete")]
public bool Obsolete { get; set; }
}
}
视图/共享/ _IndividualButtonPartial
@model FACEZ.Models.IndividualButtonPartial
<a type="button" class="btn btn-sm @Model.ButtonType"
href="@Url.Action(Model.Action) @Model.ActionParameters">
<span class="glyphicon glyphicon-@Model.Glyph"></span>
<span class="sr-only">@Model.Text</span>
</a>
视图/共享/ _TableButtonPartial
@model FACEZ.Models.IndividualButtonPartial
<td style="width: 150px;">
<div class="btn-group" role="group">
@await Html.PartialAsync("_IndividualButtonPartial",
new IndividualButtonPartial {
Action="Edit",
ButtonType="btn-primary",
Glyph = "pencil",
Text = "Edit",
Id=Model.Id
})
@await Html.PartialAsync("_IndividualButtonPartial",
new IndividualButtonPartial {
Action="Details",
ButtonType="btn-success",
Glyph = "list",
Text = "Details",
Id=Model.Id
})
@await Html.PartialAsync("_IndividualButtonPartial",
new IndividualButtonPartial {
Action="Delete",
ButtonType="btn-danger",
Glyph = "trash",
Text = "Delete",
Id=Model.Id
})
</div>
</td>
区域/ Adm /视图/AdmSchools/Index.cshtml
@using Microsoft.EntityFrameworkCore.SqlServer.Query.ExpressionTranslators.Internal
@model IEnumerable<FACEZ.Models.AdmSchool>
@{
ViewData["Title"] = "Index";
}
<h2>Index</h2>
<p>
<a asp-action="Create">Create New</a>
</p>
<div class="form-border">
<table class="table">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.AdmSchoolCode)
</th>
<th>
@Html.DisplayNameFor(model => model.AdmSchoolDescr)
</th>
<th>
@Html.DisplayNameFor(model => model.EmailPrincipal)
</th>
<th>
@Html.DisplayNameFor(model => model.EmailAccounts)
</th>
<th>
@Html.DisplayNameFor(model => model.EmailRegistrar)
</th>
<th>
@Html.DisplayNameFor(model => model.Obsolete)
</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.AdmSchoolCode)
</td>
<td>
@Html.DisplayFor(modelItem => item.AdmSchoolDescr)
</td>
<td>
@Html.DisplayFor(modelItem => item.EmailPrincipal)
</td>
<td>
@Html.DisplayFor(modelItem => item.EmailAccounts)
</td>
<td>
@Html.DisplayFor(modelItem => item.EmailRegistrar)
</td>
<td>
@Html.DisplayFor(modelItem => item.Obsolete)
</td>
<td>
@await Html.PartialAsync("_TableButtonPartial", new IndividualButtonPartial {Id=item.AdmSchoolId})
@* I am trying to replace the following code with the line above
<a asp-action="Edit" asp-route-id="@item.AdmSchoolId">Edit</a> |
<a asp-action="Details" asp-route-id="@item.AdmSchoolId">Details</a> |
<a asp-action="Delete" asp-route-id="@item.AdmSchoolId">Delete</a>
*@
</td>
</tr>
}
</tbody>
</table>
运行该应用程序时,我可以在“索引”视图中查看学校信息。 但是,正在创建的动作链接是在某处获得空间吗? 像https://localhost:44399/Adm/AdmSchools/Edit / 1而不是 像https://localhost:44399/Adm/AdmSchools/Edit/1
如何删除动作名称后面的空格。
答案 0 :(得分:1)
在_IndividualButtonPartial
中,@Url.Action(Model.Action)
和@Model.ActionParameters
之间确实有一个空格。这就是正在体现出来的。