Asp.net @找不到Ajax.ActionLink-Resource错误
当代码执行@Ajax.ActionLink时,它说“资源找不到错误”。我在视图中使用@Ajax.ActionLink并尝试更新链接信息。代码是Asp.net @找不到Ajax.ActionLink-Resource错误,asp.net,ajax,asp.net-mvc,razor,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 registre
<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>
sqlDinnerRepository类是一个运行良好的重构类。
RSVP控制器在那里,寄存器操作在那里。为什么找不到呢?谢谢
我确实改变了我的代码。但它是否与我的细节控制器中出现的问题相冲突。我将尝试使用默认路线,看看会发生什么
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/Register/13。然后,我尝试添加一个routemap,如
routes.MapRoute(
"RSVPs", // Route name
"Dinner/RSVPForEvent/{rsvp}", // URL with parameters
new { controller = "RSVP", action = "Register" } // Parameter defaults
);
但不幸的是。它不像以前那样工作了。我想知道当我将“Index”操作更改为“Register”操作时,是否需要声明它?如果需要,我可以在哪里执行?谢谢Ajax。默认情况下,ActionLink会发出GET请求。您的操作正在等待POST请求。您必须在AjaxOptions参数中配置它
@Ajax.ActionLink("RSVP for this event",
"Register", "RSVP",
new { id = Model.DinnerID },
new AjaxOptions { UpdateTargetId = "rsvpmsg", HttpMethod = "POST" },
null)
检查您的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
);
}
谢谢,但我已经编辑了我的代码的post,不幸的是仍然是相同的错误。@Lucky您的浏览器在调试时是否发送post请求?@Lucky使用Firefox中的Firebug或Chrome&IE中的开发者工具。您好,我使用了断点,代码刚刚在ajax中中断。控制器中的post代码未执行。@幸运的是,您在Firebug/Developer Tools的“网络”选项卡上看到任何发送的请求吗?您已检查控制台?您的Global.asax文件routes.MapRoute方法是什么?@Altaf Sami,请查看我的routes.MapRoute。我确实改变了我的代码。我确实改变了我的代码。但它是否与我的细节控制器中出现的问题相冲突。我会尝试使用默认路线,看看会发生什么。检查我的答案,我已经在你的评论后编辑了。希望它能起作用。也许你会看到这个答案:我只是尝试使用默认路由,和以前一样的错误。
@Ajax.ActionLink("RSVP for this event",
"Register", "RSVP",
new { id = Model.DinnerID },
new AjaxOptions { UpdateTargetId = "rsvpmsg", HttpMethod = "POST" },
null)
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
);
}