我是MVC的新手,有人可以帮助我并解释如何从视图中调用控制器方法.
我有HomeController,里面有方法ShowFileContent().
[HttpPost] public ActionResult ShowFileContent() { string filepath = Server.MapPath("\\Files\\Columns.txt"); try { var lines = System.IO.File.ReadAllLines(filepath); foreach (var line in lines) { ViewBag.FileContent += line + "\n"; } } catch (Exception exc) { ViewBag.FileContent = "File not found"; } return View(); }
在内部视图中我试图用下面的代码调用此方法,但它不起作用.
@using (Html.BeginForm("ShowFileContent","Home")) { <input type="submit" value="Show File" style='width:300px'/> } <h2>@Html.Raw(ViewBag.FileContent.Replace("\n","</br>"))</h2>
我收到错误:找不到视图’ShowFileContent’或其主人,或者没有视图引擎支持搜索的位置.
我做错了什么,从剃刀视图调用方法的最佳方法是什么?
解决方法
这听起来像是打破你的
return View();
如果你不告诉它去哪里它认为视图被称为与你的动作相同.
所以请尝试
return View("Name of your view");