【问题标题】:The current request for action ... on controller type ... is ambiguous between the following action methods当前对控制器类型的操作请求.​​..在以下操作方法之间不明确
【发布时间】:2015-09-26 22:14:11
【问题描述】:

当我使用 ~/Person/New 在浏览器上运行应用程序时,结果是

当前对控制器类型“PersonController”的操作“New”请求在 PersonMVC.Controllers.PersonController System.Web.Mvc.ActionResult New( PersonMVC.Models.Person) 类型为 PersonMVC.Controllers.PersonController

型号

namespace PersonMVC.Models
{
    public class Person
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string LastName { get; set; }

        [Required]
        [DisplayName("Social Sequrity Number")]
        [MinLength(11, ErrorMessage ="Social Security Number Must be 11 digit.")]
        [MaxLength(11, ErrorMessage ="")]
        public string SocialSecurityNumber { get; set; }

        [DisplayFormat(DataFormatString ="{0:dd.MM.yyyy}")]
        public DateTime BirthDay { get; set; }
    }
}

-查看

@model PersonMVC.Models.Person

@{
    ViewBag.Title = "New";
}

<h2>New</h2>


@using (Html.BeginForm()) 
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <h4>Person</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
            @Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.LastName, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.LastName, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.LastName, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.SocialSecurityNumber, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.SocialSecurityNumber, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.SocialSecurityNumber, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.BirthDay, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.BirthDay, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.BirthDay, "", 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>

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

控制器

namespace PersonMVC.Controllers
{
    public class PersonController : Controller
    {
       public ActionResult New()
        {
            return View();
        }
        [HttpPost]
        public ActionResult New(Person newPerson)
        {
            bool isTrue = ModelState.IsValid;
            return View();
        }
    }
}

【问题讨论】:

  • 你得到的错误是第二种方法没有用[HttpPost]装饰。你确定你显示的代码正确吗?
  • 我会使用 firebug 并确保您的 JS 不会将 POST 请求返回给~/Person/New.。由于第一个方法没有修饰,它将匹配任何 HTTP 动词。一个空白的表单帖子可以被认为是在没有参数的情况下点击新操作或使用空人员视图模型的新 (POST) 操作。

标签: asp.net-mvc asp.net-mvc-5


【解决方案1】:

你需要用适当的 http 动词来装饰方法。否则它通常会被认为是 HttpPost。

【讨论】:

    【解决方案2】:

    我认为在控制器方法中添加 [HttpPost] 可以解决问题

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-10-25
      • 1970-01-01
      • 1970-01-01
      • 2012-05-26
      • 2013-07-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多