nal
nal

Reputation: 119

@Ajax.ActionLink-Resource can not found error

when the code executes the @Ajax.ActionLink, it said "Resource can not found error". I use @Ajax.ActionLink in a view and tried to update a link information. The code is

<div id ="rsvpmsg">

    @if(Request.IsAuthenticated)
    {
        if(Model.IsUserRegistered(Context.User.Identity.Name)) 
        { 
            <p>You are registred for this event!</p>
        }
        else
        {
            @Ajax.ActionLink("RSVP for this event",
                "Register", "RSVP",
                new { id = Model.DinnerID },
                new AjaxOptions { UpdateTargetId = "rsvpmsg" })
        }
    }
    else
    {
        <a href ="/Account/Logon">Logon to RSVP for this event</a>
    }
    </div>

You may want to see the code of RVSP controller

namespace NerdDinner.Controllers
{

    public class RSVPController : Controller
    {
        sqlDinnerRepository _repository = new sqlDinnerRepository();

        //
        // AJAX: /Dinner/RSVPForEvent/1

        [Authorize]
        [HttpPost]
        public ActionResult Register(int id)
        {
            Dinner dinner = _repository.GetDinner(id);

            if (!dinner.IsUserRegistered(User.Identity.Name))
            {
                RSVP rsvp = new RSVP();
                rsvp.AttandeeName = User.Identity.Name;

                dinner.RSVPs.Add(rsvp);
                _repository.Save();
            }

            return Content("Thanks-we'll see you there!");
        }

    }
}

sqlDinnerRepository class is a refactory class which was working well. The RSVP controller is there, the Register action is there. Why it can not be found? Thanks.

I did reroute my code. But is it conflict with my problem which appears in my Detail controller. I will try to use default route and see what will happened.

 public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            "UpcomingDinners", // Route name
            "Dinner/Page/{page}", // URL with parameters
            new { controller = "Dinner", action = "Index" } // Parameter defaults
        );

        routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{id}", // URL with parameters
            new { controller = "Home", action = "Index", id = "" } // Parameter defaults
        );

    }

Hi, I tried to change the order of the content in RegisterRoutes. But it is still not working properly. Then, I checked the code again. In my Index view, I showed upcoming dinners. Three upcoming dinners each page. For this pagination reason, I used @Html.RouteLink to tell the code going to the route which is named "Upcoming dinner". That means I could not change the route even the order(I tested it). You may want to check the Index view. Belowing is the code.

@model NerdDinner.Helpers.PaginatedList<NerdDinner.Models.Dinner>

@{
    ViewBag.Title = "Upcoming Dinners";
}

<h2>Upcoming Dinners</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table>
    <tr>
        <th>
            Title
        </th>
        <th>
            Latitude
        </th>
        <th>
            Longitude
        </th>
        <th>
            EventDate
        </th>
        <th>
            ContactPhone
        </th>
        <th>
            Address
        </th>
        <th>
            Country
        </th>
        <th>
            HostedBy
        </th>
        <th>
            Description
        </th>
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Title)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Latitude)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Longitude)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.EventDate)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.ContactPhone)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Address)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Country)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.HostedBy)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Description)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { id=item.DinnerID }) |
            @Html.ActionLink("Details", "Details", new { id=item.DinnerID }) |
            @Html.ActionLink("Delete", "Delete", new { id=item.DinnerID })
        </td>
    </tr>
}

</table>

@if(Model.HasPreviousPage)
 {
     @Html.RouteLink("<<<", "UpcomingDinners", new { page = (Model.PageIndex - 1)})
 }

@if(Model.HasNextPage)
 {
     @Html.RouteLink(">>>", "UpcomingDinners", new { page = (Model.PageIndex + 1)})
 }

Then I carefully observed the error hint as well as break point my RSVP Controller code. I found the code broke out at

public ActionResult Register(int id)

which I personally think the code cannot find, for instance, RSVP/Register/13. Then, I tried to add a routemap like

routes.MapRoute(
            "RSVPs", // Route name
            "Dinner/RSVPForEvent/{rsvp}", // URL with parameters
            new { controller = "RSVP", action = "Register" } // Parameter defaults
        );

But unfortunately. It was not working as before. And I am wondering when I changed the "Index" action to "Register" action, do I need to state it?If yes, where can I do?Thanks

Upvotes: 1

Views: 1142

Answers (2)

Ufuk Hacıoğulları
Ufuk Hacıoğulları

Reputation: 38468

Ajax.ActionLink makes a GET request by default. Your action is expecting a POST request. You have to configure it in AjaxOptions parameter.

@Ajax.ActionLink("RSVP for this event",
                 "Register", "RSVP",
                 new { id = Model.DinnerID },
                 new AjaxOptions { UpdateTargetId = "rsvpmsg", HttpMethod = "POST" },
                 null)

Upvotes: 3

Altaf Sami
Altaf Sami

Reputation: 846

Check your global.asax file RegisterRoutes method:

EDIT:

Change the sequence of your MapRoute, It searches from top to bottom, first it gets the top one and not go to next. e.g

 public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
     routes.MapRoute(
        "Default", // Route name
        "{controller}/{action}/{id}", // URL with parameters
        new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
    );

    routes.MapRoute(
        "UpcomingDinners", // Route name
        "Dinner/Page/{page}", // URL with parameters
        new { controller = "Dinner", action = "Index" } // Parameter defaults
    );



}

Upvotes: 0

Related Questions