Core 2.0 MVC taghelper生成没有href的链接

时间:2018-01-15 09:21:12

标签: asp.net-mvc .net-core asp.net-core-tag-helpers

我是Core 2.0框架的新手(虽然不是MVC),我正在扩展ContosoUniversity演示项目以包含一些想要的功能,看看这是否是我想要构建的即将推出的应用程序的方法。其中一个是Area。

我没有在默认文件夹中使用“学生”控制器,模型和视图的演示,而是创建了一个“教育”区域,并将StudentsController放在其Controllers文件夹中,模型在Models文件夹中,然后使用Scaffolding在那里创建视图: Areas folder structure

Startup.cs中的Configure方法已修改为设置区域路由:

// Map static routes
        app.UseStaticFiles();

        // Map MVC routing
        app.UseMvc(routes =>
        {
            // Area routes
            routes.MapRoute(
              name: "areas",
              template: "{area:exists}/{controller=Home}/{action=Index}/{id?}"
            );

            // Default MVC routing
            routes.MapRoute(
                name: "default",
                template: "{controller=Home}/{action=Index}/{id?}");
        });

在测试应用程序时,它加载正常,我可以按预期查阅/ Education / Students页面。但是,在验证框架生成的CRUD链接时,它们似乎没有HREF属性,因此不起作用。当我将其更改为ActionLink时,它确实有效:

@Html.ActionLink("Edit 1", "Edit", "Students", new { area = "Education", id = item.ID }, null) // works
                <a asp-action="Edit" asp-area="Education" asp-route-id="@item.ID">Edit 1</a> // does not work

actionlink生成的链接为http://localhost:61541/Education/Students/Edit?id=1

然而,为了使这个工作,我必须修改控制器以包括路由装饰器,如果我正确理解教程,因为当前的区域应该粘贴到链接中,从示例中不应该是必需的。可能存在与此相关的潜在问题,但我无法以某种方式弄清楚。控制器,索引操作和编辑操作:

    namespace ContosoUniversity.Areas.Education
{
    [Area("Education")]
    [Route("Education/[controller]")]
    public class StudentsController : Controller
    {
        private readonly EducationContext _context;

        public StudentsController(EducationContext context)
        {
            _context = context;
        }

        // GET: Education/Students        
        public async Task<IActionResult> Index()
        {
            return View(await _context.Students.ToListAsync());
        }

        // GET: Education/Students/Edit/5
        [Route("[action]")]
        public async Task<IActionResult> Edit(int? id)
        {
            if (id == null)
            {
                return NotFound();
            }

            var student = await _context.Students.SingleOrDefaultAsync(m => m.ID == id);
            if (student == null)
            {
                return NotFound();
            }
            return View(student);
        }

所以我的问题:

  1. 如何让taghelpers生成正确的链接,
  2. 我是否因为需要各种各样的ActionLinks而在某个地方捏造了一些东西 路由工作到什么地方?
  3. 编辑:使用actionlink导航到编辑网址会生成一个空表单。模型在控制器中正确填充,它返回正确的视图,但不使用生成的代码填充字段:

    <input asp-for="LastName" class="form-control" />
    

    但是,如果我将@ Model.LastName添加到HTML中,我会看到呈现的正确名称。我错过了什么?某种程度上模型没有正确绑定? :(

0 个答案:

没有答案