【问题标题】:My view page is not displaying anything in mvc asp.net我的视图页面未在 mvc asp.net 中显示任何内容
【发布时间】: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.cshtmlCreateCustomerAccount 是您的控制器的名称,而不是方法的名称)。或者,您可以使用return View("CreateCustomerAccount");,但这没有任何意义
  • 非常感谢!我将视图名称更改为索引并添加“使用 Microsoft.AspNetCore.Authorization;使用 Microsoft.AspNetCore.Mvc;”它有效

标签: c# asp.net .net asp.net-mvc razor


【解决方案1】:

控制器名称和动作结果名称应该不同更改动作结果名称并使用此动作名称重新创建视图这将起作用

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 ActionResult Index()
 {
    return View();
 }

 public ActionResult CreateCustomerAccountt()
 {
    return View();
 }


 }
}

【讨论】:

  • 非常感谢!我将视图名称更改为索引并添加了“使用 Microsoft.AspNetCore.Authorization;使用 Microsoft.AspNetCore.Mvc;”它有效
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-02
  • 2015-07-31
  • 2021-02-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多