【问题标题】:Go to another view on button click - view is not found单击按钮转到另一个视图 - 未找到视图
【发布时间】:2018-02-14 22:01:34
【问题描述】:

从未找到“Test.cshtml”视图。

错误:未找到“测试”视图或其主视图,或者没有视图引擎支持搜索到的位置。
搜索了以下位置:

列出的地点有:

  • ~/Views/SecondGroup/Test.aspx
  • ~/Views/SecondGroup/Test.ascx
  • ~/Views/SecondGroup/123.cshtml

但位置不正确,应该是"~/Views/SecondGroup/Test/123" ??

ViewOne.cshtml(由 FirstController 生成)

<button onclick= "@("window.location.href ='" + @Url.Action("Test", 
"SecondController", new { id = "123" }) + "'");">
 GO TO ANOTHER VIEW
</button>

第二控制器

public ActionResult Test(string id)
{           
    return View("Test", id);  // here id = '123'          
}

Test.cshtml

@model String
@{    
   Layout = "~/Views/Shared/_Layout.cshtml";
}
<div> .... </div>

这是文件夹结构:

---Controllers
     --- HomeController
     --- FirstController
     --- SecondController

-- Views
     --- FirstGroup
         --- ViewOne

     --- SecondGroup
         --- Test

为什么永远找不到"Test.cshtml" 视图?

谢谢

【问题讨论】:

    标签: asp.net-mvc razor asp.net-mvc-routing


    【解决方案1】:

    在您的Test(string id) 方法中,您使用的是return View("Test", id);,它使用this overload,因为idtypestring

    在该重载中,第二个参数是在呈现视图时要使用的母版页或模板的名称。

    您需要使用this overload,其中第二个参数是object(您的型号)。

    更改代码以将string 转换为object

    public ActionResult Test(string id)
    {           
        return View("Test", (object)id);       
    }
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-03
    相关资源
    最近更新 更多