我是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();
}
Run Code Online (Sandbox Code Playgroud)
在内部视图中,我试图用下面的代码调用此方法,但它不起作用.
@using (Html.BeginForm("ShowFileContent", "Home"))
{
<input type="submit" value="Show File" style='width:300px'/>
}
<h2>@Html.Raw(ViewBag.FileContent.Replace("\n", "</br>"))</h2>
Run Code Online (Sandbox Code Playgroud)
我收到错误:找不到视图'ShowFileContent'或其主人,或者没有视图引擎支持搜索的位置.
我做错了什么,从剃刀视图调用方法的最佳方法是什么?
我的视图上有这段代码
<h2>@ViewBag.FileContent</h2>
@using (Html.BeginForm("ShowWebSiteLinks", "Home"))
{
<label>WebSite URL:</label>
<input type="text" id="ft" name="fturl" value="http://localhost/search/?sr=100" style="width:350px"><br>
<label>Save to file:</label>
<input type="text" id="filename" name="links_filename" value="links.txt" style="width:200px"><br>
<input id="btnSubmit" type="submit" value="Start" />
}
Run Code Online (Sandbox Code Playgroud)
单击提交按钮后,控制器调用功能正常,但提交按钮保持启用状态。我想防止多次点击按钮并想禁用它,但不知道如何禁用。我尝试过使用这个 javascript,但它不起作用。
$(document).ready(function () {
$('btnSubmit').click(function (e) {
$(this).attr('disabled', true);
});
});
Run Code Online (Sandbox Code Playgroud)
有人可以帮我弄这个吗?