Ajax Actionlink不会使用POST,只使用GET(在ASP.NET MVC4中)

时间:2013-09-14 09:20:39

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

我已经尝试了我能找到的所有解决方案,但没有任何效果。这是我的控制器中的相关代码:

public ActionResult Assign(int id)
{
    Person person = sc.GetPerson(id);
    if (person == null)
        return HttpNotFound();
    ViewBag.PersonID = person.PersonID;
    ViewBag.FirstName = person.FirstName;
    ViewBag.LastName = person.LastName;
    ViewBag.PersonTitle = person.Title;

    List<Project> projects = sc.GetPersonNonAssignments(id).ToList();
    return View(projects);
}

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Assign(int id, int id2)
{
    try
    {
        bool check = sc.CreateAssignment(id, id2);
        if (check)
            return RedirectToAction("Details", new { id = id });
        else
            return RedirectToAction("Index");
    }
    catch
    {
        return RedirectToAction("Index");
    }
}

在RouteConfig.cs中我有:

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

这是View(看起来我不需要HTML5中的type =“text / javascript”):

@model IEnumerable<TestService.Project>
@{ViewBag.Title = "Assign Person";}
<script src="@Url.Content("~/Scripts/jquery-1.8.2.min.js")"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")"></script>
<h3>Assign @ViewBag.FirstName @ViewBag.LastName (@ViewBag.PersonTitle) to:</h3>
<table>
@foreach (var item in Model) {
    <tr>
        <td>@Html.DisplayFor(modelItem => item.ProjectName)</td>
        <td>@Ajax.ActionLink("Assign", "Assign", new { id=ViewBag.PersonID, id2=item.ProjectID }, new AjaxOptions { HttpMethod = "POST" })</td>
    </tr>
}
</table>
@{string linkname = "Back to " + @ViewBag.FirstName + "'s projects";}
<p>@Html.ActionLink(linkname, "Details", new { id=ViewBag.PersonID })</p>
<p>@Html.ActionLink("Back to People", "Index")</p>

在根Web.config中我有:

<appSettings>
  <add key="webpages:Version" value="2.0.0.0" />
  <add key="webpages:Enabled" value="false" />
  <add key="PreserveLoginUrl" value="true" />
  <add key="ClientValidationEnabled" value="true" />
  <add key="UnobtrusiveJavaScriptEnabled" value="true" />
</appSettings>

在Global.asax中,我有:

public class MvcApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {
        ...
        HtmlHelper.UnobtrusiveJavaScriptEnabled = true;
    }
}

它仍然不会POST,只有GET。我通过剥离[HttpPost]并重命名第二个ActionResult来验证这一点。我在Chrome中尝试过“Inspect element”,它没有发现任何错误。发生了什么事?

0 个答案:

没有答案