@ Ajax.ActionLink-Resource无法找到错误

时间:2013-06-09 18:41:17

标签: asp.net ajax asp.net-mvc razor

当代码执行@ Ajax.ActionLink时,它说“资源找不到错误”。我在视图中使用@ Ajax.ActionLink并尝试更新链接信息。代码是

<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>

您可能希望查看RVSP控制器的代码

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类是一个运行良好的重构类。 RSVP控制器在那里,Register操作就在那里。为什么找不到它?感谢。

我重新路由了我的代码。但是它与我的细节控制器中出现的问题相冲突。我将尝试使用默认路由,看看会发生什么。

 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
        );

    }

嗨,我试图更改RegisterRoutes中内容的顺序。但它仍然无法正常工作。然后,我再次检查了代码。在我的索引视图中,我展示了即将到来的晚餐。每页三个即将到来的晚餐。由于这个分页原因,我使用@ Html.RouteLink告诉代码进入名为“即将到来的晚餐”的路线。这意味着我无法改变路线甚至订单(我测试了它)。您可能想要检查索引视图。下面是代码。

@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)})
 }

然后我仔细观察了错误提示以及我的RSVP控制器代码的断点。我发现代码在

发生了
public ActionResult Register(int id)

我个人认为代码找不到,例如RSVP / Register / 13。然后,我尝试添加像

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

但不幸的是。它没有像以前那样工作。我想知道何时将“索引”操作更改为“注册”操作,是否需要说明?如果是,我该怎么办?谢谢

2 个答案:

答案 0 :(得分:3)

Ajax.ActionLink默认发出GET请求。您的操作正在等待POST请求。您必须在AjaxOptions参数中配置它。

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

答案 1 :(得分:0)

检查global.asax文件RegisterRoutes方法:

编辑:

更改MapRoute的顺序,它从上到下进行搜索,首先搜索到顶部,然后不进入下一个。 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
    );



}