控制器类型的当前操作请求.​​..在以下操作方法之间是不明确的

时间:2015-09-26 22:14:11

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

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

  

控制器类型'PersonController'上当前的操作请求'New'在以下操作方法System.Web.Mvc.ActionResult类型PersonMVC.Controllers.PersonController上的New()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();
        }
    }
}

1 个答案:

答案 0 :(得分:0)

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