ASP .Net MVC表单在提交时不调用控制器

时间:2015-03-10 15:05:35

标签: c# asp.net-mvc

我无法在MVC Controller中调用post方法。当我单击提交按钮时,它不会调用Create方法。我添加一个断点,但它永远不会到达代码。我假设有一些错误,但不确定如何查看错误消息。

以下是视图:

@model PersonViewModel

@{
    ViewBag.Title = "Register";
}

@using (Html.BeginForm(PeopleControllerAction.Create, ControllerName.People, FormMethod.Post))
{
    @Html.AntiForgeryToken()

<div class="form-horizontal row"> 
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
</div>
<div class="row panel radius">
    <div class="medium-2 columns">
        <h3>Contact Information</h3>
    </div>
    <div class="medium-10 columns">
        <div class="row">
            <div class="medium-6 columns">
                <div class="form-group">
                    @Html.LabelFor(model => model.FirstName, htmlAttributes: new { @class = "control-label" })
                    <div>
                        @Html.EditorFor(model => model.FirstName, new { htmlAttributes = new { @class = "form-control" } })
                        @Html.ValidationMessageFor(model => model.FirstName, "", new { @class = "text-danger" })
                    </div>
                </div>
            </div>
         </div>
      </div>
   </div>

// more fields removed for brevity

<div class="row">
    <div class="form-group">
        <div>
            <input type="submit" value="Submit" class="button" />
        </div>
    </div>
</div>
}

这是控制器:

public class PeopleController : Controller
{
    private IPersonService context { get; set; }

    public PeopleController(IPersonService context)
    {
        this.context = context;
    }

// POST: People/Create
    // To protect from overposting attacks, please enable the specific properties you want to bind to, for
    // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> Create([Bind(Include = "FirstName,LastName,Age,Email,Phone,City,State,HopeToReach,Story,Goal,Image")] PersonViewModel person)
    {
        if (ModelState.IsValid)
        {
            try
            {
                person.ImagePath = ImageUploader.UploadImage(person.Image);
            }
            catch (ArgumentException e)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest, e.Message);
            }

            var id = await context.AddAsync(person);
            return RedirectToAction(PeopleControllerAction.Confirmation);
        }

        return View(person);
    }
}

1 个答案:

答案 0 :(得分:-1)

这已经解决了。有问题的行动没有路线。我正在使用属性路由。