【发布时间】:2017-12-19 03:14:22
【问题描述】:
我是 asp.net core 的初学者。我使用 code first 方法来创建一个名为 CreateCustomerAccount.cshtml 的视图页面,并添加了控制器以返回名为 CreateCustomerAccountController 的视图。但是,控制器返回的是空白页而不是 html/view 内容。我做错了什么吗?
这是我的 CreateAccount.cshtml 代码
@{
ViewData["Title"] = "CreateCustomerAccount";
}
<h2>CreateCustomerAccount</h2>
<p>User can create account using a form later</p>
这是我的 CreateCustomerAccountController.cs 类的代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
namespace TimeSheetManagementSystem.Controllers
{
public class CreateCustomerAccountController : Controller
{
public IActionResult Index()
{
return View();
}
public IActionResult CreateCustomerAccount()
{
return View();
}
}
}
【问题讨论】:
-
您的视图应位于名为“CreateCustomerAccountController”的文件夹下,您的视图/页面应命名为“Index.cshtml”或“CreateCustomerAccount.cshtml”,您的结构是否相同?
-
您的方法名为
Index(),因此您的视图需要命名为Index.cshtml(CreateCustomerAccount是您的控制器的名称,而不是方法的名称)。或者,您可以使用return View("CreateCustomerAccount");,但这没有任何意义 -
非常感谢!我将视图名称更改为索引并添加“使用 Microsoft.AspNetCore.Authorization;使用 Microsoft.AspNetCore.Mvc;”它有效
标签: c# asp.net .net asp.net-mvc razor