2012-02-19 63 views
-1

我得到一個System.NullReferenceException:對象引用未設置爲對象的實例,當我在嚮導上「回退」我正在使用的嚮導(另外,我是如果我只是回到瀏覽器上,就會得到一個「確認重新提交」頁面)。這是由於下面的代碼(通常情況下,不會對我的嚮導或瀏覽器上的後退按鈕使用該系統打「回」正常工作):上item.Selected = modelStateValue.Equals(item.Value, StringComparison.OrdinalIgnoreCase);使用EditorTemplate獲取對象引用錯誤的複選框/單選按鈕

嚮導發生

// Loop through the items and make sure they are Selected if the value has been posted 
    if(Model != null) 
    { 
     foreach (var item in selectorModel.Items) 
     { 
      if (supportsMany) 
      { 
       var modelStateValue = GetModelStateValue<string[]>(Html, fieldName) ?? ((IEnumerable)Model).OfType<object>().Select(m => m.ToString()); 
       item.Selected = modelStateValue.Contains(item.Value); 
      } 
      else 
      { 
       var modelStateValue = GetModelStateValue<string>(Html, fieldName); 
       item.Selected = modelStateValue.Equals(item.Value, StringComparison.OrdinalIgnoreCase); 
      } 
     } 
    } 

錯誤嚮導上的「後退」按鈕的代碼在控制器中看起來像這樣:

public ActionResult EMailQuoteConfirm(string backButton, string nextButton) 
     { 
      if (backButton != null) 
       return RedirectToAction("EMailQuoteBasicDetails"); 
      else if (nextButton != null) 
       return RedirectToAction("EMailQuoteSubmitted"); 
      else 
       return View("EMailQuote/Confirm", myData); 
     } 

任何建議表示讚賞。

回答

0

經過對這個問題的大量研究,我確定它是序列化導致這個奇怪的錯誤。儘管序列化運行良好,但我選擇了一個更適合此代碼的jQuery解決方案。

獲得的經驗教訓:jQuery並不那麼糟糕。 :)