我是MVC的新手,我在MS MVC2中實現了Nerd Dinner MVC示例應用程序。我在第10步,「Ajax使RSVPs接受」。我已經添加了新的RSVP控制器與增加了註冊操作方法如下所示:Ajax調用註冊表操作方法的書呆子晚餐錯誤
public class RSVPController : Controller
{
DinnerRepository dinnerRepository = new DinnerRepository();
//
// AJAX: /Dinners/RSVPForEvent/1
[Authorize, AcceptVerbs(HttpVerbs.Post)]
public ActionResult Register(int id) {
Dinner dinner = dinnerRepository.GetDinner(id);
if (!dinner.IsUserRegistered(User.Identity.Name)) {
RSVP rsvp = new RSVP();
rsvp.AttendeeName = User.Identity.Name;
dinner.RSVPs.Add(rsvp);
dinnerRepository.Save();
}
return Content("Thanks - we'll see you there!");
}
}
我添加到兩個阿賈克斯腳本庫的引用,並添加下面的詳細信息查看在文章中描述的代碼:
<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</a> to RSVP for this event.
<% } %>
</div>
當我點擊「RSVP此事件」鏈接,我收到了404 eror稱資源無法找到:
The resource cannot be found.
Description: HTTP 404. 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: /NerdDinner/RSVP/Register/24
Version Information: Microsoft .NET Framework Version:2.0.50727.4200; ASP.NET Version:2.0.50727.4205
當我踏進去的是找到註冊查詢代碼ster action方法正確。在玩弄它之後,我從Register方法的約束中移除了「AcceptVerbs(HttpVerbs.Post)」,然後它就起作用了。然而,它沒有重新加載它剛剛顯示的頁面「謝謝 - 我們將在那裏看到你」的消息在一個新的空白頁面上。查看詳細信息頁面中的html沒有表單提交發生,所以我想知道Ajax代碼是否需要更多的東西來使該調用成爲Post?這部分的書呆子晚餐應用有沒有已知的問題?我認爲應用程序是用MVC1編寫的,而我正在使用MVC2 - 這是否有所不同?
TIA,
夏蘭
嗨Tahbaza,返回的文本應該更新詳細信息頁面內的div - UpdateTargetId =「rsvpmsg」。根據Scott Gu教程下面的代碼和屏幕截圖,單擊「註冊」鏈接,並使用返回的文本更新div。不知道他的樣本如何工作,當他沒有做一個職位: –
2010-08-07 14:01:26