c# - 提交按钮没有按预期工作

标签 c# asp.net-mvc-4

我在注册页面上有一个带有提交按钮的表单,按下该按钮后将不起作用,我已经用了一段时间了吗?我在 creat 方法上设置了一个断点,但它没有被调用

这是 Controller 中的创建方法

   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")
}

最佳答案

出于某种原因,您的表单没有正确连接到您的操作。为了消除正在发生的事情的某些可能性,我建议您实际指定表单将用于发布到的 Action 和 Controller。

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

关于c# - 提交按钮没有按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18174235/

相关文章:

c# - 缓存局部 View Asp.net MVC 5

C#程序员想开发第一个网站

c# - 将 X-Frame-Options header 添加到 MVC 4 应用程序中的所有页面

asp.net - DropDownList 值到 DataType.Date 模型 (ASP.NET MVC 4)

c# - 带有子检查器的 Unity 自定义检查器

asp.net-mvc-4 - 如何在 MVC4 ViewModel、Controller、View 中使用字典?

c# - 无法从 .cshtml 选项卡运行应用程序

c# - 如何附加动态创建的 Windows 窗体控件?

c# - 抛出 InvalidOperationException 是因为它无法将值从 appsettings.json 转换为枚举

c# - 从 2 个派生类中选择正确的枚举列表