提交不会执行HTTP Post,但会改为http get / request

时间:2014-09-26 18:44:41

标签: c# asp.net-mvc

我试图向我的数据库中的团队提交更新(单个表)。但是,在单击“提交”或“保存”以更新团队时,它会一直调用负责HTTP请求的第一个Edit()而不是负责HTTP Post的控制器中的Edit()函数。

知道为什么会这样吗?可能是我使用了部分页面而我的母版页是_Layout.cshtml。

查看:

@using (Html.BeginForm("Edit","Team",FormMethod.Post))  {
@Html.AntiForgeryToken()
@Html.ValidationSummary()

<fieldset>
    <legend>Team</legend>

    @Html.HiddenFor(model => model.ID)

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

    <p>
        <input type="submit" name="Edit" value="Save" />
    </p>
</fieldset>
}

型号:

public class Team
{
    [Key]
    public int ID { get; set; }
    public string Name { get; set; }
}

控制器:

    public ActionResult Edit(int id = 0)
    {
        Team team = db.Teams.Find(id);
        if (team == null)
        {
            return HttpNotFound();
        }
        return View(team);
    }

    //
    // POST: /Team/Edit/5

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Edit(Team team)
    {
        if (ModelState.IsValid)
        {
            db.Entry(team).State = EntityState.Modified;
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        return View(team);
    }

结果,包括提交后的网址。

enter image description here

软件包:

public static void RegisterBundles(BundleCollection bundles)
    {
        bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
                    "~/Scripts/jquery-{version}.js"));

        bundles.Add(new ScriptBundle("~/bundles/jqueryui").Include(
                    "~/Scripts/jquery-ui-{version}.js"));

        bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
                    "~/Scripts/jquery.unobtrusive*",
                    "~/Scripts/jquery.validate*"));

        // Use the development version of Modernizr to develop with and learn from. Then, when you're
        // ready for production, use the build tool at http://modernizr.com to pick only the tests you need.
        bundles.Add(new ScriptBundle("~/bundles/modernizr").Include(
                    "~/Scripts/modernizr-*"));

        bundles.Add(new StyleBundle("~/Content/css").Include("~/Content/site.css"));
        bundles.Add(new StyleBundle("~/Content/css").Include("~/Styles/layout.css"));

        bundles.Add(new StyleBundle("~/Content/themes/base/css").Include(
                    "~/Content/themes/base/jquery.ui.core.css",
                    "~/Content/themes/base/jquery.ui.resizable.css",
                    "~/Content/themes/base/jquery.ui.selectable.css",
                    "~/Content/themes/base/jquery.ui.accordion.css",
                    "~/Content/themes/base/jquery.ui.autocomplete.css",
                    "~/Content/themes/base/jquery.ui.button.css",
                    "~/Content/themes/base/jquery.ui.dialog.css",
                    "~/Content/themes/base/jquery.ui.slider.css",
                    "~/Content/themes/base/jquery.ui.tabs.css",
                    "~/Content/themes/base/jquery.ui.datepicker.css",
                    "~/Content/themes/base/jquery.ui.progressbar.css",
                    "~/Content/themes/base/jquery.ui.theme.css"));
    }

过滤器:

    public static void RegisterGlobalFilters(GlobalFilterCollection filters)
    {
        filters.Add(new HandleErrorAttribute());
    }

RouteConfig:

    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );
    }

1 个答案:

答案 0 :(得分:0)

好的,我发现了我的问题:

我有一个布局页面_Layout.cshml,我在该页面中有一个表单标签,我将视图作为部分页面放在那里。删除表格后,mvc会正确发布。