我是新的MVC,有人可以帮助我,并解释如何从视图调用控制器方法。MVC4 - 如何从剃刀视图调用控制器方法
我有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'或其主人或未查看引擎支持搜索到的位置。
我做错了什么是最好的方式从剃刀视图中调用方法?
是你的控制器叫“HomeController”? –
yes,HomeController – msharank
尝试指定FormMethod,即'Html.BeginForm(“ShowFileContent”,“Home”,new {},FormMethod.Post,new {}))' –