2012-05-24 109 views
4

我是mvc C#的新手,並且卡住了。請告知如何解決此問題。我在添加時遇到錯誤。當我將鼠標懸停在紅色波浪線它說:「並非所有的代碼路徑返回一個值」獲取錯誤:並非所有的代碼路徑都返回一個值

public ActionResult Add(ShapeInputModel dto, FormCollection collection) 
    { 

     var model = new GeoRegions(); 

     if (TryUpdateModel(model)) 
     { 


      var destinationFolder = Server.MapPath("/App_Data/KML"); 
      var postedFile = dto.Shape; 

      if (postedFile != null) 
      { 
       var fileName = Path.GetFileName(postedFile.FileName); 
       var path = Path.Combine(destinationFolder, fileName); 
       postedFile.SaveAs(path); 

       //Save to Database 
       Db.AddGeoRegions(model); 
       return RedirectToAction("Index"); 

      } 

      return View(); 

     } 
    } 

回答

7

使用此:

public ActionResult Add(ShapeInputModel dto, FormCollection collection) 
{ 
    var model = new GeoRegions(); 

    if (TryUpdateModel(model)) 
    { 
     var destinationFolder = Server.MapPath("/App_Data/KML"); 
     var postedFile = dto.Shape; 

     if (postedFile != null) 
     { 
      var fileName = Path.GetFileName(postedFile.FileName); 
      var path = Path.Combine(destinationFolder, fileName); 
      postedFile.SaveAs(path); 

      //Save to Database 
      Db.AddGeoRegions(model); 
      return RedirectToAction("Index"); 
     } 
     return View(); 

    } 
    return null; // you can change the null to anything else also. 
} 

發生此錯誤是因爲如果您的函數不返回任何內容,如果TryUpdateModel(model) = false。因此,添加行return nullreturn 'any other thing'將解決問題!

+0

謝謝大家的回答。我從你們所有人那裏學到了很多東西,我很欣賞它,儘管我還有很多東西需要學習。我使用了返回null,並且一切都很完美! – user1382770

+0

請記住,如果您使用'null',則應始終檢查該值,如果方法返回時爲null或不是! – Writwick

3

沒有return如果「如果」是從來沒有進入。

我想始終保持的if-else的使用[與回報]平衡,我可以看到返回值(和所有路徑有返回值)一目瞭然:

if (TryUpdateModel(model)) 
{ 
    ... 
    if (postedFile != null) 
    { 
     ... 
     return RedirectToAction("Index"); 
    } else { 
     return View(); 
    } 
} else { 
    return View(); // or null/whatever is appropriate 
} 

當然ReSharper的常告訴我我有「無用的」else語句;-)

快樂編碼。

0

如果(TryUpdateModel(model))返回false,則不返回任何內容。也許你的意思是讓你的return View();if之外?

0

這個錯誤是在錫上說的;有一個代碼路徑,函數將完成但不會返回值。具有返回類型的函數必須始終返回一個值或拋出異常。

在你的情況下,如果TryUpdateModel(model)返回false,你沒有返回值。

0

看完這個錯誤!在你的方法執行的某個時刻,你必須返回一個值,或者拋出一個異常。 (我認爲在這種情況下返回null是有用的)

0

當然,你的初始if (TryUpdateModel(model))只會使你的例程在條件爲true時才返回一個值;如果不是,則不會返回任何違反方法簽名的內容。

0

if (TryUpdateModel(model)) 
{ 
    // lots of stuff 

    return View(); 
} 

那麼將被告知是否TryUpdateModel是不是真的回來了?

即使if語句爲false,您的方法也必須返回ActionResult

0

只要快速查看代碼,我可以看到你有返回命令(在if語句塊中返回View()。現在如果「If」條件失敗,那麼外面沒有返回語句它的範圍最簡單的方法是將

} 

      return View(); 

     } 
return null; // Depends upon your code though. you might want to return something else 
} 
1

的最後一行之前添加

return null; 

}

0

您正在返回「if()」條件中的值。假設條件失敗時會怎樣?該程序將不會返回值。因此,如果條件不變,則返回任何默認值,它可能處於其他條件。

public ActionResult Add(ShapeInputModel dto, FormCollection collection) 
{ 

    var model = new GeoRegions(); 

    if (TryUpdateModel(model)) 
    { 
    .... 
    .... 
    } 
    return default_value;//it may be in else condition also. 
} 

試試吧。如果你的問題解決了,那麼標記爲已回答。所以這對其他人有用。

相關問題