MVC 4 - HttpPost没有被解雇(MVC 4 - HttpPost is not fired)
我的CreateController.cs :
using System.Web.Mvc;
using ClientDAL;
using Services;
using ShardingMvcDemo.Raven;
using ShardingMvcDemo.ViewModels;
namespace ShardingMvcDemo.Controllers
{
public class CreateController : Controller
{
//
// GET: /Create/
public ActionResult Index()
{
return View();
}
//
// POST: /Create/
[HttpPost]
public ActionResult Create(ClientViewModel client)
{
try
{
var ravenDbConnection = new RavenDbConnection(new RavenDbConnectionManager());
var service = new ClientService(ravenDbConnection);
service.AddClient(client.FirstName, client.LastName, client.Country);
return RedirectToAction("Index");
}
catch
{
return View();
}
}
}
}
我的Create/Index.cshtml视图:
@model ShardingMvcDemo.ViewModels.ClientViewModel
@{
ViewBag.Title = "Create a client";
}
Create
@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
Client
@Html.LabelFor(model => model.FirstName)
@Html.EditorFor(model => model.FirstName)
@Html.ValidationMessageFor(model => model.FirstName)
@Html.LabelFor(model => model.LastName)
@Html.EditorFor(model => model.LastName)
@Html.ValidationMessageFor(model => model.LastName)
@Html.LabelFor(model => model.Country)
@Html.EditorFor(model => model.Country)
@Html.ValidationMessageFor(model => model.Country)
}
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
当我单击表单中的提交按钮时,甚至不会触发[HttpPost]方法(在调试模式下检查)。 我究竟做错了什么?
My CreateController.cs:
using System.Web.Mvc;
using ClientDAL;
using Services;
using ShardingMvcDemo.Raven;
using ShardingMvcDemo.ViewModels;
namespace ShardingMvcDemo.Controllers
{
public class CreateController : Controller
{
//
// GET: /Create/
public ActionResult Index()
{
return View();
}
//
// POST: /Create/
[HttpPost]
public ActionResult Create(ClientViewModel client)
{
try
{
var ravenDbConnection = new RavenDbConnection(new RavenDbConnectionManager());
var service = new ClientService(ravenDbConnection);
service.AddClient(client.FirstName, client.LastName, client.Country);
return RedirectToAction("Index");
}
catch
{
return View();
}
}
}
}
My Create/Index.cshtml view:
@model ShardingMvcDemo.ViewModels.ClientViewModel
@{
ViewBag.Title = "Create a client";
}
Create
@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
Client
@Html.LabelFor(model => model.FirstName)
@Html.EditorFor(model => model.FirstName)
@Html.ValidationMessageFor(model => model.FirstName)
@Html.LabelFor(model => model.LastName)
@Html.EditorFor(model => model.LastName)
@Html.ValidationMessageFor(model => model.LastName)
@Html.LabelFor(model => model.Country)
@Html.EditorFor(model => model.Country)
@Html.ValidationMessageFor(model => model.Country)
}
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
When I click the submitting button in the form, the [HttpPost] method is not even triggered (checked in debug mode). What am I doing wrong?
原文:https://stackoverflow.com/questions/17469045
更新时间:2019-12-31 18:17
最满意答案
它与您的视图名称不匹配。 将其更改为:
public ActionResult Index(ClientViewModel client)
It doesn't match your view name. Change it to:
public ActionResult Index(ClientViewModel client)
相关问答
想象一下: [HttpGet]
public ActionResult Edit(int id) { ... }
[HttpPost]
public ActionResult Edit(MyEditViewModel myEditViewModel) { ... }
除非使用ActionMethodSelectorAttributes HttpGet和HttpPost ,否则这是不可能的。 这使得创建编辑视图非常简单。 所有的操作链接都直接指向控制器。 如果视图模型验证为false,则只需再次
...
假设您有一个Login操作,为用户提供一个登录屏幕,然后在用户提交表单后再次收到用户名和密码: public ActionResult Login() {
return View();
}
public ActionResult Login(string userName, string password) {
// do login stuff
return View();
}
即使我们可以通过查看,MVC也没有给出明确的指示,哪个动作是哪个。 如果您将[HttpGe
...
它与您的视图名称不匹配。 将其更改为: public ActionResult Index(ClientViewModel client)
It doesn't match your view name. Change it to: public ActionResult Index(ClientViewModel client)
尝试这个 [HttpPost]
public ActionResult SearchPost(string txt_name)
{
return JavaScript(string.Format("window.location = '{0}'", Url.Action("Search", "Controller"));
}
您必须确保Search操作方法的URL正确,我不知道控制器名称是什么。 理论上说,既然你已经提出了客户端ajax请求,那么返回View("S
...
我现在明白了这个问题。 它必须是HttpGet(并且将通过测试),因为它是发送给用户的一次性链接。 用户必须单击将参数传递给应用程序的链接。 这必须使用HttpGet而不是HttpPost来完成。 I now understand the problem. It has to be a HttpGet (and will pass testing) as is a one time link sent to the user. The user has to click on the link p
...
上传实际上要求您在表单上指定enctype属性。 这是一个如何做到这一点的例子: @using (Html.BeginForm("AddFiles", "AdministerFiles", FormMethod.Post,
new { enctype = "multipart/form-data" }))
{
...
}
Uploads actually require you to specify the enctype attribute on the form. Here
...
解: @using (Ajax.BeginForm("AddNewsletterEmail", "Newsletter", new AjaxOptions() { UpdateTargetId = "email", HttpMethod = "Post"}))
我所要做的就是将NewsletterController更改为Newsletter以便修复它(我认为Ajax.Beginform已经将Controller附加到指定的重载?)纠正我,我错了。 特别感谢来自asp.net论坛的Nataraj
...
问题是action的id参数是一个int(值类型),它不能作为空引用传递。 因此,您需要在BeginForm调用中(在视图中)将其显式设置为0或使其可为空。 基本上,路由引擎无法根据您提供的数据(操作名称和控制器名称)+路由映射来解析您的操作和控制器。 示例(如果您决定将参数保留为int ):
...
您可能需要在视图中传入Html.BeginForm()中的控制器和操作名称。 由于正在为HTTP get请求调用[HttpPost] Settings()操作,这意味着get请求没有另一个Settings()操作,因此我猜测您的视图是通过其他操作提供的。 在这种情况下,您需要在Html.BeginForm()中显式设置控制器和操作。 尝试这个:
You probabl
...
but since you simply return the link, I advise you to substitute it somehow that way: