提交按钮在 asp.net MVC 视图中不起作用
submit button is not working in asp.net MVC view
我正在尝试 运行 使用 MVC 中的提交按钮编写 C# 代码,但出现错误。
当我单击 'Connect' 按钮时,出现此错误:
错误信息:
型号:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;
namespace NEW_Tools.Models
{
public class LocalEnvGuide
{
public int id { get; set; }
[Required(ErrorMessage = "The path field is required")]
public string mvc_cf_path { get; set; }
[Required(ErrorMessage = "The path field is required")]
public string es_cf_path { get; set; }
[Required(ErrorMessage = "The environment name field is required")]
public string env_name { get; set; }
[Required]
public bool enable_tasker { get; set; }
}
}
控制器:(LocalEnvGuides.cs)
...
// POST: LocalEnvGuides/Run
[HttpPost, ActionName("Run")]
[ValidateAntiForgeryToken]
public async Task<IActionResult> RunConfirmed(int id)
{
var localEnvGuide = await _context.LocalEnvGuide.FindAsync(id);
string machine_name = Environment.MachineName.ToString();
string env_name = localEnvGuide.env_name;
string mvc_cf_path = localEnvGuide.mvc_cf_path;
string es_cf_path = localEnvGuide.es_cf_path;
Run.Main(machine_name, env_name, mvc_cf_path, es_cf_path, true);
ViewBag.Message = string.Format("Hello {0}.", machine_name);
return View();
}
视图:(Index.cshtml):
@model IEnumerable<NEW_Tools.Models.LocalEnvGuide>
@{
ViewData["Title"] = "Index";
}
<link rel="stylesheet" href="~/css/StyleTable.css">
<h1 class="text-center">Choose the Environment</h1>
<p>
<a asp-action="Create">
<input type="submit" value="Enter a new connection" class="btn btn-primary" />
</a>
</p>
<div class="pricing-box-container">
@foreach (var item in Model)
{
<div class="pricing-box text-center">
<p class="price">@Html.DisplayFor(modelItem => item.env_name)</p>
<ul class="features-list">
<li>@Html.DisplayFor(modelItem => item.mvc_cf_path)</li>
<li>@Html.DisplayFor(modelItem => item.es_cf_path)</li>
<li>Tasker Status: @Html.DisplayFor(modelItem => item.enable_tasker)</li>
</ul>
<a asp-action="Edit" asp-route-id="@item.id">
<button class="btn-primary">Edit</button>
</a>
<a asp-action="Details" asp-route-id="@item.id">
<button class="btn-primary">Details</button>
</a>
<a asp-action="Delete" asp-route-id="@item.id">
<button class="btn-primary">Delete</button>
</a>
<br />
<a asp-action="Run" asp-route-id="@item.id">
<input type="submit" value="Connect" class="btn btn-primary" style="background-color:limegreen" />
</a>
<a asp-action="Disconnect" asp-route-id="@item.id">
<input type="submit" value="Disconnect" class="btn btn-primary" style="background-color:red" />
</a>
</div>
}
视图:(Run.cshtml):
@model IEnumerable<NEW_Tools.Models.LocalEnvGuide>
@{
ViewData["Title"] = "Index";
}
<!DOCTYPE html>
<div class="text-center">
<h1 class="display-4">You are connected to the environment!</h1>
</div>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Run</title>
</head>
<body>
@if (ViewBag.Message != null)
{
<script type="text/javascript">
window.onload = function () {
alert("Congratulations, you have a new connection!");
};
</script>
}
</body>
</html>
<br />
<div class="text-center">
<a asp-action="Index">
<input type="submit" value="Back to connections history" class="btn btn-primary" />
</a>
</div>
我想得到你的帮助,谢谢!
(如果您需要更多详细信息,请评论,我会添加)
ValidateAntiForgeryToken 的目的是防止 cross-site 请求伪造攻击。
在您的视图中添加下行
@Html.AntiForgeryToken()
<a>
link只能发送httpget请求,可以尝试用form代替:
<form method="post" asp-action="Run" asp-route-id="1">
<input type="submit" value="Connect" class="btn btn-primary" style="background-color:limegreen" />
</form>
我正在尝试 运行 使用 MVC 中的提交按钮编写 C# 代码,但出现错误。 当我单击 'Connect' 按钮时,出现此错误:
错误信息:
型号:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;
namespace NEW_Tools.Models
{
public class LocalEnvGuide
{
public int id { get; set; }
[Required(ErrorMessage = "The path field is required")]
public string mvc_cf_path { get; set; }
[Required(ErrorMessage = "The path field is required")]
public string es_cf_path { get; set; }
[Required(ErrorMessage = "The environment name field is required")]
public string env_name { get; set; }
[Required]
public bool enable_tasker { get; set; }
}
}
控制器:(LocalEnvGuides.cs)
...
// POST: LocalEnvGuides/Run
[HttpPost, ActionName("Run")]
[ValidateAntiForgeryToken]
public async Task<IActionResult> RunConfirmed(int id)
{
var localEnvGuide = await _context.LocalEnvGuide.FindAsync(id);
string machine_name = Environment.MachineName.ToString();
string env_name = localEnvGuide.env_name;
string mvc_cf_path = localEnvGuide.mvc_cf_path;
string es_cf_path = localEnvGuide.es_cf_path;
Run.Main(machine_name, env_name, mvc_cf_path, es_cf_path, true);
ViewBag.Message = string.Format("Hello {0}.", machine_name);
return View();
}
视图:(Index.cshtml):
@model IEnumerable<NEW_Tools.Models.LocalEnvGuide>
@{
ViewData["Title"] = "Index";
}
<link rel="stylesheet" href="~/css/StyleTable.css">
<h1 class="text-center">Choose the Environment</h1>
<p>
<a asp-action="Create">
<input type="submit" value="Enter a new connection" class="btn btn-primary" />
</a>
</p>
<div class="pricing-box-container">
@foreach (var item in Model)
{
<div class="pricing-box text-center">
<p class="price">@Html.DisplayFor(modelItem => item.env_name)</p>
<ul class="features-list">
<li>@Html.DisplayFor(modelItem => item.mvc_cf_path)</li>
<li>@Html.DisplayFor(modelItem => item.es_cf_path)</li>
<li>Tasker Status: @Html.DisplayFor(modelItem => item.enable_tasker)</li>
</ul>
<a asp-action="Edit" asp-route-id="@item.id">
<button class="btn-primary">Edit</button>
</a>
<a asp-action="Details" asp-route-id="@item.id">
<button class="btn-primary">Details</button>
</a>
<a asp-action="Delete" asp-route-id="@item.id">
<button class="btn-primary">Delete</button>
</a>
<br />
<a asp-action="Run" asp-route-id="@item.id">
<input type="submit" value="Connect" class="btn btn-primary" style="background-color:limegreen" />
</a>
<a asp-action="Disconnect" asp-route-id="@item.id">
<input type="submit" value="Disconnect" class="btn btn-primary" style="background-color:red" />
</a>
</div>
}
视图:(Run.cshtml):
@model IEnumerable<NEW_Tools.Models.LocalEnvGuide>
@{
ViewData["Title"] = "Index";
}
<!DOCTYPE html>
<div class="text-center">
<h1 class="display-4">You are connected to the environment!</h1>
</div>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Run</title>
</head>
<body>
@if (ViewBag.Message != null)
{
<script type="text/javascript">
window.onload = function () {
alert("Congratulations, you have a new connection!");
};
</script>
}
</body>
</html>
<br />
<div class="text-center">
<a asp-action="Index">
<input type="submit" value="Back to connections history" class="btn btn-primary" />
</a>
</div>
我想得到你的帮助,谢谢! (如果您需要更多详细信息,请评论,我会添加)
ValidateAntiForgeryToken 的目的是防止 cross-site 请求伪造攻击。
在您的视图中添加下行
@Html.AntiForgeryToken()
<a>
link只能发送httpget请求,可以尝试用form代替:
<form method="post" asp-action="Run" asp-route-id="1">
<input type="submit" value="Connect" class="btn btn-primary" style="background-color:limegreen" />
</form>