ASP.NET "Resource not found" 尝试在控制器中执行 ActionResult 方法时

ASP.NET "Resource not found" when attempting to execute an ActionResult method in the controller

我正在尝试向我的 MVC5 应用程序添加编辑功能,但每当我尝试在 Search 视图中调用我的 Edit 方法时,我都会收到一条错误消息,指出资源 localhost:xxxx/Search/EditClient 不存在。

EditClient 只是我控制器中的一个方法,我必须 return 一个 RedirectToAction.

这是我的控制器:

public class SearchController : Controller 
{
    public ActionResult SearchClient() {
        //Just returning the default view for the search page
        return View();
    }

    public ActionResult SearchClient(string SearchId)
    {
        //I do have validations in case the ID doesn't exist, but I'm omitting them from this question to avoid inflating the code.
        Client clientFound = (Client)Session[SearchId];
        //SearchId is the ID attribute for my Client object. If the ID was in the Session variable, it will retrieve the corresponding object and I'll use it for the model in my view.
        return View("SearchClient", clientFound);
    }

    [HttpPost]
    public ActionResult EditClient(Client client)
    {
        Session[client.Id] = client;
        return RedirectToAction("SearchClient");
    }
}

我的观点

@model MVCExample.Models.Client

@{
    ViewBag.Title = "Search";
    ViewBag.Message = "Look for Clients";
}

<h1 class="store-title">@ViewBag.Title</h1>
<h2 class="page-title">@ViewBag.Message</h2>
@using (Html.BeginForm("SearchClient", "Search"))
{
   <p>Search for User ID: @Html.TextBox("SearchId")</p></br>
   <input type="submit" value="Search"/>

   @Html.LabelFor(m => m.FullName), new { @class = "control-label" }
   @Html.EditorFor(m => m.FullName, new { @class = "form-control" }

   <a class="btn btn-primary" href="@Url.Action("EditClient, "Search", FormMethod.Post)">Save Changes</a>
}

我可以成功检索用户并显示他们的数据,但每当我尝试更改它时(在本例中我只为 FullName 创建了一个字段,但我有每个 属性 的字段)并保存更改后,我被重定向到一个错误页面,指出 Search/EditClient 不存在。 我认为重定向到某个操作会解决这个问题,但为什么我的应用似乎在尝试显示错误的视图?

如果需要 SearchEdit 功能,您可以使用两个 Html.BeginForm():


@model MVCExample.Models.Client
@{
    ViewBag.Title = "Search";
    ViewBag.Message = "Look for Clients";
}

<h1 class="store-title">@ViewBag.Title</h1>
<h2 class="page-title">@ViewBag.Message</h2>

@using (Html.BeginForm("SearchClient", "Search"))
{
    <div>
        <p>Search for User ID: @Html.TextBox("SearchId")</p>
        <input type="submit" value="Search" />
    </div>

    <div>
        <br />
        @Html.LabelFor(m => m.FullName)
        @Html.EditorFor(m => Model.FullName)
    </div>
}

@using (Html.BeginForm("EditClient", "Search", FormMethod.Post))
{
    @Html.HiddenFor(m => Model.Id)
    @Html.HiddenFor(m => Model.FullName)
    <br /><input type="submit" value="Save Changes" />
}

[HttpPost] 属性应用于 EditClient 方法:

[HttpPost]
public ActionResult EditClient(Client client)
{
    Session[client.Id] = client;
    return RedirectToAction("SearchClient");
}

不幸的是,在此上下文中无法使用 href="@Url.Action() 发送 Post。如果你想创建 href 你需要开始处理 @Ajax.ActionLink().

Ancor 标记始终生成 Get 方法。它不提交表单,它只是使用提供的参数调用 GET 操作。如果你想 POST 一个表单,最好使用一个按钮。您需要调用 EditClient 操作。试试这个

 @using (Html.BeginForm("EditClient", "Search",  FormMethod.Post))
    {
..... your code
       <input type="submit" class="btn btn-primary" value="Save Changes"/>
    }

我看不到你的启动文件,也许你可以尝试使用路由

    [Route("~/Search/EditClient")]
    public ActionResult EditClient(Client client)
    {
 .....
     }