我是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>
解决方法
这听起来像是打破你的
- return View();
如果你不告诉它去哪里它认为视图被称为与你的动作相同.
所以请尝试
- return View("Name of your view");