当代码执行@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
);
但不幸的是。它不像以前那样工作。我想知道当我将“索引”操作更改为“注册”操作时,我需要说明吗?如果是,我可以在哪里做?谢谢