0

Error Message:

 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.

Requested URL: /Receivers/Create

I have create view in Receivers Folder. I have checked the spelling. it's correct but the create page doesn't show.

The code is below.

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

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

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

My controller code as follows.

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

                return RedirectToAction("Index");
            }

My ReceiverService code as follows

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);
            }
hmiedema9
  • 948
  • 4
  • 17
Ammad Hassan
  • 95
  • 1
  • 12
  • 1
    Have you create an action method to go with the view? URL routes go to actions, not views. – ADyson Apr 29 '19 at 19:02
  • 1
    That's your route config, which is useful, but where's your action method? You said you created a view, but didn't mention whether you created an action with the same name? P.s. if you have additional code, please use the "edit" button to add it to the question. Code does not belong in comments. Thanks. – ADyson Apr 29 '19 at 19:09
  • You can check the action method. – Ammad Hassan Apr 29 '19 at 19:12
  • you can't use a hyperlink to visit a POST method. A hyperlink always generates a GET request. Besides, you aren't even passing any data for the model, so it would make no sense. If you don't want to use AJAX (which, since the action returns a full View, also wouldn't really make sense), then you'll need a form for this. The form will need to contain all the fields required to generate a valid ReceiverCreationModel object. – ADyson Apr 29 '19 at 20:06

1 Answers1

0

you cannot use Html.ActionLink to call post action method (see link below). you can use @Ajax.ActionLink(... new AjaxOptions { HttpMethod = "POST" }) instaed.

Sending Html.ActionLink to post action

**EDIT another option would be to use Html.BeginForm

Html.BeginForm("ActionName", "ControllerName", null, FormMethod.Post, new { your arguments })
SAR
  • 180
  • 1
  • 9
  • But I m not using Ajax. – Ammad Hassan Apr 29 '19 at 19:55
  • I can see that, that's why I am suggesting that you use other options that support POST requests (for example `Ajax.ActionLink` or even `Html.BeginForm` with `FormMethod.Post`) – SAR Apr 29 '19 at 19:59