MVC 如何处理我代码中的提交按钮?我有一个名为 ArticleController 的 Controller 应该可以处理它,但我不知道如何处理。
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Article</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.Text, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Text, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Text, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default"/>
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
Controller 看起来像这样:
namespace Decision.Controllers
{
public class ArticleController : Controller
{
// GET: Article
public ActionResult Index()
{
return View();
}
[HttpGet]
public ActionResult Create()
{
var article = new ArticleController();
var home = new HomeController();
return View(home);
}
/*[HttpPost]
public ActionResult Create(Article article)
{
entities.Article.AddObject(article);
entities.SaveChanges();
return Redirect("/");
}*/
}
}
单击时哪个方法处理提交按钮?
最佳答案
请看https://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Request_methods以获得关于请求方法的清晰想法。
当用户查看页面时,这是一个 GET 请求,而当用户提交表单时,通常是一个 POST 请求。 HttpGet 和 HttpPost 只是将操作限制为适用的请求类型。
当你将 [HttpPost] 添加到 action 中时,MVC 清楚地知道使用哪个 action 来处理 POST 请求。
关于c# - MVC如何处理 Controller 中的按钮点击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42086266/