2011-10-27 33 views
0

我正在學習ASP.NET MVC 2,並且專注於「Ajax和客戶端腳本」。理解錯誤「值不能爲空」錯誤

我剛纔所書下面寫的代碼如下:

public class MVCAJAXLearningsController : Controller 
{ 
    private Dictionary<string, double> offsets = new Dictionary<string, double> { { "utc", 0 }, { "bst", 1 }, { "mdt", -6 }, { "ist", 5.5 } }; 

    public ActionResult Index() 
    { 
     return View(); 
    } 

    public ActionResult GetTime(string zone) 
    { 
     DateTime time = DateTime.UtcNow.AddHours(offsets[zone]); 
     if (Request.IsAjaxRequest()) 
     { 
      string fragment = string.Format("<div>The time in {0} is {1:hh:MM:ss tt}</div>", zone.ToUpper(), time); 
      return Content(fragment); 
     } 
     else 
     { 
      return View(time); 
     } 
    } 
} 

我收到以下錯誤。

***Error***

這又如何解決呢?

回答

3

不是100%,但你似乎沒有將參數傳遞給GetTime。

+2

+1:具體來說,「zone」參數沒有被填充。 – StriplingWarrior

+0

嗨,謝謝你的回覆。是的,你說得對(我猜)。我怎樣才能傳遞參數?你能建議我嗎... –

0

問題不在於你的代碼。 MVC項目中包含的庫用於將您的母版頁中未引用的控件提交回給控制器。

如果您查看腳本文件夾,您將看到兩個名爲MicrosoftAjax.js和MicrosoftMvcAjax.js的javascrip庫。這兩個項目都需要在項目運行時加載。

這兩行只需添加到您的網站母版頁的部分:

<script src="<%: Url.Content("~/Scripts/MicrosoftAjax.js") %>" type="text/javascript"></script> 
<script src="<%: Url.Content("~/Scripts/MicrosoftMvcAjax.js") %>" type="text/javascript"></script> 

之後,你應該是好去。

試試看看是否有幫助。

+0

嗨,謝謝你的回覆。我已經這樣做了。但是,仍然沒有工作。 –

0

你發送的動作是什麼參數值?剃刀的一個例子: @ {Html.Renderaction(「GetTime」,「MVCAJAXLearnings」,new {zone =「ist」});}