单击按钮后转到另一个视图 - 找不到视图

时间:2018-02-14 22:01:35

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

" 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>

SecondController

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"视图?

谢谢

1 个答案:

答案 0 :(得分:3)

Test(string id)方法中,您使用的return View("Test", id);使用this overload,因为type的{​​{1}}是id。< / p>

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

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

更改代码以将object转换为string

object
相关问题