我正在尝试执行本教程:https://www.tutorialspoint.com/asp.net_core/asp.net_core_razor_edit_form.htm
我有一个NET Core Webapp和一个包含员工的小列表(id,name)。我创建了一个Index.cshtml,Details.cshtml和Edit.cshtml。 数据库连接正常。 当我尝试单击员工姓名后面的编辑或详细信息链接时,路由似乎不起作用。我只是得到空站点。
Index.cshtml
@using MyApp.Controllers
@model HomePageViewModel
@{
ViewBag.Title = "Home";
}
<h1>Welcome!</h1>
<table>
@foreach (var employee in Model.Employees)
{
<tr>
<td>@employee.name</td>
<td>
<a asp-controller="Employee" asp-action="Details"
asp-routeid="@employee.id">Details</a>
<a asp-controller="Employee" asp-action="Edit"
asp-routeid="@employee.id">Edit</a>
</td>
</tr>
}
</table>
Edit.cshtml
@model MyApp.Models.Employees
@{
ViewBag.Title = $"Edit {Model.name}";
}
<h1>Edit @Model.name</h1>
<form asp-action="Edit" method="post">
<div>
<label asp-for="name"></label>
<input asp-for="name" />
<span asp-validation-for="name"></span>
</div>
<div>
<input type="submit" value="Save" />
</div>
</form>
完整的控制器
using Microsoft.AspNetCore.Mvc;
using MyApp.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;
namespace MyApp.Controllers
{
public class EmployeeController : Controller
{
public ViewResult Index()
{
var model = new HomePageViewModel();
using (var context = new FirstAppDemoDbContext())
{
SQLEmployeeData sqlData = new SQLEmployeeData(context);
model.Employees = sqlData.GetAll();
}
return View(model);
}
public IActionResult Details(int id)
{
var context = new FirstAppDemoDbContext();
SQLEmployeeData sqlData = new SQLEmployeeData(context);
var model = sqlData.Get(id);
if (model == null)
{
return RedirectToAction("Index");
}
return View(model);
}
[HttpGet]
public IActionResult Edit(int id)
{
var context = new FirstAppDemoDbContext();
SQLEmployeeData sqlData = new SQLEmployeeData(context);
var model = sqlData.Get(id);
if (model == null)
{
return RedirectToAction("Index");
}
return View(model);
}
[HttpPost]
public IActionResult Edit(int id, EmployeeEditViewModel input)
{
var context = new FirstAppDemoDbContext();
SQLEmployeeData sqlData = new SQLEmployeeData(context);
var employee = sqlData.Get(id);
if (employee != null && ModelState.IsValid)
{
employee.name = input.Name;
context.SaveChanges();
return RedirectToAction("Details", new { id = employee.id });
}
return View(employee);
}
}
public class SQLEmployeeData
{
private FirstAppDemoDbContext _context { get; set; }
public SQLEmployeeData(FirstAppDemoDbContext context)
{
_context = context;
}
public void Add(Employees emp)
{
_context.Add(emp);
_context.SaveChanges();
}
public Employees Get(int ID)
{
return _context.Employees.FirstOrDefault(e => e.id == ID);
}
public IEnumerable<Employees> GetAll()
{
return _context.Employees.ToList<Employees>();
}
}
public class HomePageViewModel
{
public IEnumerable<Employees> Employees { get; set; }
}
public class EmployeeEditViewModel
{
[Required, MaxLength(80)]
public string Name { get; set; }
}
}
应用程序请求localhost:xxxx / Employee / Edit with argument&lt; 0&gt; (而不是id 1或2 ...)并重定向到localhost:xxxx / Employee的索引列表。我期待localhost:xxxx / Employee / Edit / 1但是id参数似乎丢失了。
我在_ViewImports.cshtml
中使用 @addTagHelper *,Microsoft.AspNetCore.Mvc.TagHelpers我可以尝试什么?如果您需要更多信息,请告诉我们!
答案 0 :(得分:1)
有很多场景我们需要经历,所以我宁愿写在这里,而不是多次评论:
设置一个断点,运行debug 每一行。看它是否出错。
对于这种情况,我怀疑它在这里出错:
if (model == null)
{
return RedirectToAction("Index");
}
您的模型可能为null,因此会重定向回主索引。
由于使用控制器名称作为&#34;员工&#34;而不是&#34; Home&#34;,与教程点相比,您的路由可能会在某处发生故障。检查您的路由文件,默认情况下,它应该是这样的:
routes.MapRoute(
name: "default_route",
template: "{controller}/{action}/{id?}",
defaults: new { controller = "Home", action = "Index" });
你在这里做了什么改变吗?
答案 1 :(得分:1)
我希望您的问题出现在0.003
文件中,您指定在路由中使用Index.cshtml
:
@employee.id
将这两者改为:
asp-routeid="@employee.id"
区别在于asp-route-id="@employee.id"
和-
之间的route
。有关Form Tag Helper如何使用路由参数的详细信息,请参阅文档:
提供
id
属性,其中asp-route-<Parameter Name>
添加到路线值。