找不到资源。无法访问URL

时间:2019-04-29 19:00:12

标签: c# asp.net asp.net-mvc visual-studio

错误消息:

 The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable.  Please review the following URL and make sure that it is spelled correctly.

请求的网址:/Receivers/Create

我在“收件人”文件夹中创建了视图。我已经检查了拼写。是正确的,但不会显示创建页面。

代码在下面。

@model IEnumerable<BBProjectnew.Models.Receivers.Receiver>

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>

我的控制器代码如下。

    [HttpPost]
            [ValidateAntiForgeryToken]
            public async Task<ActionResult> Create(ReceiverCreationModel model)
            {
                if (ModelState.IsValid)
                {
                    await receiverService.CreateReceiver(model);
                }

                return RedirectToAction("Index");
            }

我的ReceiverService代码如下

public async Task CreateReceiver(ReceiverCreationModel model)
            {
                DbContext.Receivers.Add(Mapper.Map<ReceiverCreationModel, Receiver>(model));
                await DbContext.SaveChangesAsync();
            }

            public Receiver ReceiverDetails(int id)
            {
                return DbContext.Receivers.SingleOrDefault(d => d.ReceiverId == id);
            }

1 个答案:

答案 0 :(得分:0)

您不能使用Html.ActionLink来调用post操作方法(请参见下面的链接)。您可以使用@Ajax.ActionLink(... new AjaxOptions { HttpMethod = "POST" }) instaed。

Sending Html.ActionLink to post action

**编辑 另一个选择是使用Html.BeginForm

Html.BeginForm("ActionName", "ControllerName", null, FormMethod.Post, new { your arguments })