提交按钮无法按预期工作

时间:2013-08-11 16:40:12

标签: c# asp.net-mvc-4

我在注册页面上有一个带有提交按钮的表单,按下后无法正常工作,现在已经有一段时间了吗?我在creat方法上设置了一个断点,它不会被称为

这是控制器中的Creat Metod

   public ActionResult Create()
    {
        return View();
    }

    //
    // POST: /RegsterDetails/Create

    [HttpPost]
    public ActionResult Create(User user )
    {
        if(ModelState.IsValid)
        try
        {
            db.Users.Add(user);
            db.SaveChanges();
            return RedirectToAction("RegisterStats", "RegisterStats");
        }
        catch(Exception)
        {
            return View();
        }
        return View(user);

这是视图

    @model Fitness_Friend.Web.Models.User

@{
    ViewBag.Title = "RegisterDetails";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>RegisterDetails</h2>

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

    <fieldset>
        <legend>User</legend>

        <div class="editor-label">
            @Html.LabelFor(model => model.FirstName)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.FirstName)
            @Html.ValidationMessageFor(model => model.FirstName)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.LastName)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.LastName)
            @Html.ValidationMessageFor(model => model.LastName)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.DOB)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.DOB)
            @Html.ValidationMessageFor(model => model.DOB)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Address)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Address)
            @Html.ValidationMessageFor(model => model.Address)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Email)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Email)
            @Html.ValidationMessageFor(model => model.Email)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Gender)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Gender)
            @Html.ValidationMessageFor(model => model.Gender)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.UserName)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.UserName)
            @Html.ValidationMessageFor(model => model.UserName)
        </div>

        <p>
            <input type="Submit" name="Create" value="Register Details" />
        </p>
    </fieldset>
}

<div>
    @Html.ActionLink("Back to List", "Index","Home")
</div>

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

4 个答案:

答案 0 :(得分:2)

出于某种原因,您的表单未正确处理您的操作。为了消除正在发生的事情的某些可能性,我建议您实际指定表单将用于发布的操作和控制器。

@Html.BeginForm("CreateUser", "ControllerWithCreateUserMethod")
{
    //insert your HTML here.
    //submit button here
    <input type="submit" value="Submit"/>
}

答案 1 :(得分:1)

在HTML表单中,您需要一个提交按钮来发布数据,例如

<input type="submit" value="Submit"/>

<button type="submit">Submit</button>

如果你不喜欢这些,你可以选择这样的东西

<a onclick="$('#formId').submit();">Submit</a>

当然,您可以选择在表单标签中指定要发布数据的位置

@Html.BeginForm("Action", "Controller")
{
   ...
}

答案 2 :(得分:1)

我已经重复了以下代码:

控制器+模型(糟糕的设计不这样做):

public class HomeController : Controller
{
    public ActionResult Create()
    {
        return View();
    }

    [HttpPost]
    public ActionResult Create(MyUser user)
    {
        // break point next line works
        if (ModelState.IsValid)
            try
            {
                //db.Users.Add(user);
                //db.SaveChanges();
                return RedirectToAction("RegisterStats", "RegisterStats");
            }
            catch (Exception)
            {
                return View();
            }
        return View(user);
    }

    public class MyUser
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
    }
}

有了这个观点:

@model MvcApplication3.Controllers.HomeController.MyUser

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

    <fieldset>
        <legend>User</legend>

        <div class="editor-label">
            @Html.LabelFor(model => model.FirstName)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.FirstName)
            @Html.ValidationMessageFor(model => model.FirstName)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.LastName)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.LastName)
            @Html.ValidationMessageFor(model => model.LastName)
        </div>

        <p>
            <input type="Submit" name="Create" value="Register Details" />
        </p>
    </fieldset>
}

它没有问题。

答案 3 :(得分:0)

我迟到了,但我想我知道为什么这个控制器方法不会被调用。

如果表单将Model的数据传递回控制器,则Model类需要有一个不带参数的构造函数。我愿意在上面的例子中打赌 用户类没有这样的构造函数。