gpt4 book ai didi

c# - 提交不执行 HTTP Post,而是执行 http get/request

转载 作者:太空宇宙 更新时间:2023-11-03 15:50:32 24 4
gpt4 key购买 nike

我正在尝试向我的数据库(一个表)中的团队提交更新。但是,在单击“提交”或“保存”以更新团队后,它会一直调用负责 HTTP 请求的第一个 Edit() 而不是负责 HTTP Post 的 Controller 中的 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; }
}

Controller :

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

结果包括我提交后的 URL。

enter image description here

bundle :

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());
}

路由配置:

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

最佳答案

好的,我找到了我的问题:

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

关于c# - 提交不执行 HTTP Post,而是执行 http get/request,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26066307/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com