1

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

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

这是视图(看起来我在 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 中尝试过“检查元素”,它没有发现任何错误。到底是怎么回事?

4

0 回答 0