c# - MVC如何处理 Controller 中的按钮点击

标签 c# asp.net-mvc razor

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/

相关文章:

c# - 用C#在Word中用图片替换文本

c# - 从 List<T> 到数组 T[] 的转换

c# - 从 asp.net 中的代码隐藏函数调用 Javascript

asp.net - 如何在 IIS 7 中更新 FX_schema.xml 文件?

c# - 在 Razor 代码中放置 Javascript 变量

c# - 发起电话调用并随后返回应用程序 - Xamarin

jquery - 如何从 jQuery 数据表中的 ajax 数据源获取 mRender() 函数中的隐藏列值

javascript - 在部分 View 中包含 JavaScript 文件

c# - 没有具有键 'IEnumerable<SelectListItem>' 的 “Geslacht” 类型的 ViewData 项

javascript - 来自 MVC.Grid 的模态视图(部分)