ActionLink没有调用Controller

时间:2016-07-15 10:40:16

标签: c# asp.net-mvc razor

我在视图中定义了一个操作链接

@Html.ActionLink("Mark as Completed", "MarkComplete", new { id = item.ID })

但是这不会调用我的控制器中的方法,而是调用它产生以下链接的链接

http://localhost:52069/Users/MarkComplete/3

    [HttpPost]
    public ActionResult MarkComplete(int? id) {
        if (id == null) {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
        Task task = db.Tasks.Find(id);
        if (task == null) {
            return HttpNotFound();
            }
        if (ModelState.IsValid) {
            task.Completed = 1;
            db.Entry(task).State = EntityState.Modified;
            db.SaveChanges();
            return RedirectToAction("Index");
            }
        return View(task);
        }

我在这里缺少的任何东西

完整视图代码

@model IEnumerable<Base.Models.Task> @{
    ViewBag.Title = "UserTasks"; }

<h2>User</h2>


@using (Html.BeginForm()) {
    @Html.AntiForgeryToken()

        <h4>Task for User</h4>
        <table class="table">
            <tr>  
                <th>
                    Name
                </th>
                <th>
                    Details
                </th>
                <th>
                    Start Date
                </th>
                <th>
                    Due Date
                </th>
             </tr>
                    @foreach (var item in Model) {
                <tr>
                    <td>@item.Name </td>
                    <td>@item.Details</td>
                    <td>@item.StartDate.ToShortDateString()</td>
                    <td>@item.EndDate.ToShortDateString()</td>
                    <td>
                        @Html.ActionLink("Mark as Completed", "MarkComplete", new { id = item.ID })
                    </td>
                </tr>
                        }
                </table>
    }
            <div>
                @Html.ActionLink("Back to List", "Index")
            </div>

            @section Scripts {
                @Scripts.Render("~/bundles/jqueryval")
            }

2 个答案:

答案 0 :(得分:2)

HttpPost生成ActionLink请求时,您的操作会标有GET属性。更准确地说,ActionLink呈现一个锚标记,单击该标记时会向服务器发送GET请求。

操作是否标有HttpPost属性有什么特别的原因吗?您可能希望删除该属性或将其替换为HttpGet属性。

如果是,由于某些原因,要求操作仅收听POST请求,那么您可以使用单个按钮设置为链接,而不是使用ActionLink。 / p>

@using(Html.BeginForm("MarkComplete", "Users")) {
    <input type="hidden" name="id" value="@item.ID">
    <input type="submit" value="Mark as Completed" />
}

但是你已经将整个表格包装在一个表单中,是的,表单不能嵌套,所以你需要更加戏剧性地更改视图。

因此整个视图可能如下所示:

@model IEnumerable<Base.Models.Task>
@{ ViewBag.Title = "UserTasks"; }

<h2>User</h2>

<h4>Task for User</h4>
<table class="table">
    <tr>  
        <th>
            Name
        </th>
        <th>
             Details
        </th>
        <th>
            Start Date
        </th>
        <th>
            Due Date
        </th>
    </tr>
    @foreach (var item in Model) {
        <tr>
            <td>@item.Name </td>
            <td>@item.Details</td>
            <td>@item.StartDate.ToShortDateString()</td>
            <td>@item.EndDate.ToShortDateString()</td>
            <td>
                @using(Html.BeginForm("MarkComplete", "Users")) {
                    @Html.AntiForgeryToken()
                    <input type="hidden" name="id" value="@item.ID">
                    <input type="submit" value="Mark as Completed" />
                }
            </td>
        </tr>
    }
</table>

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

答案 1 :(得分:0)

替换ActionLink
<input type="submit" value="Mark as Completed"> 

所以它会进行POST。

将Form的Action设置为ActionLink指向的位置。

https://weblogs.asp.net/scottgu/asp-net-mvc-framework-part-4-handling-form-edit-and-post-scenarios

相关问题