2016-07-12 67 views
-7

こんにちは、私はC#を初めて使用しています。問題の原因を発見する方法については、他の人の助けを借りて行うことができます。私は下の行「_parcelService」の下で到達不能なコードが検出されたというエラーが表示され続けます。この行で到達不能コードが検出されました。C#beginner

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.Mvc; 
using ABC.Data; 
using ABC.Services; 
using ABC.Services.Service; 
namespace ABC.Controllers 
{ 
    public class AdminController : Controller 
    { 
     private ParcelService _parcelService; 
     public AdminController() 
     { 
      _parcelService = new ParcelService(); 
     } 

     // GET: Admin 
     public ActionResult Index() 
     { 
      return View(); 
     } 

     [HttpGet] 
     public ActionResult AddParcel() 
     { 
      return View(); 
     } 
     [HttpPost] 
     public ActionResult AddParcel(ParcelDetail parcel) 
     { 
      return View(); 

      { 
       _parcelService.AddParcel(parcel); 

       return RedirectToAction("Parcels", 
        new { TrackingID = parcel.TrackingID, controller = "Parcel" }); 
      } 
     } 

    } 
} 
+9

:あなたはちょうどそうソリューションをreturn View()

を削除することができreturn RedirectToActionを持っているので、また、あなたはSO 2を返す...

を持っていますView(); '。その行の後のすべてのコードは決して実行できません。 – sstan

答えて

4

あなたはすでに、アクションから結果が返されました:

return View(); 

関数から返す文の後に何が存在することはできません。

[HttpPost] 
public ActionResult AddParcel(ParcelDetail parcel) 
{ 
    _parcelService.AddParcel(parcel); 

    return RedirectToAction("Parcels", 
     new { TrackingID = parcel.TrackingID, controller = "Parcel" }); 
} 

をまたは多分あなたは、あなたのバックエンドサービスに電話してリダイレクトしますそれ以外のモデルの検証が失敗した場合、同じビューを再レンダリングとなり、いくつかのロジックを適用したい:だから、この行を取り除く

[HttpPost] 
public ActionResult AddParcel(ParcelDetail parcel) 
{ 
    if (!ModelState.IsValid) 
    { 
     // The model that was passed to this action was not valid 
     // => Redisplay the same view so that the user can correct 
     // the errors 
     return View(); 
    } 

    // At this stage we know that the model is valid and we can submit it 
    // for processing 
    _parcelService.AddParcel(parcel); 

    // redirect to a different action by returning the corresponding result 
    return RedirectToAction(
     "Parcels", 
     new { TrackingID = parcel.TrackingID, controller = "Parcel" } 
    ); 
} 
+0

ありがとうございました! – user3116358

0

エラーは自己説明的です。以下

return View() 

あなたはビューを返します。したがって、これ以降のすべてのコードには到達できません。

[HttpPost] 
public ActionResult AddParcel(ParcelDetail parcel) 
{ 
    // Here you add the parcel 
    _parcelService.AddParcel(parcel); 

    // Then you make a redirect to a view that are visible all the parcels. 
    return RedirectToAction("Parcels", 
    new 
    { 
     TrackingID = parcel.TrackingID, 
     controller = "Parcel" 
    }); 
} 

あなたが有効であるオブジェクトの場合と同様に、いくつかの検証を強制するAddParcelを呼び出す前に、それが良いでしょう(Darin'nの答えを確認してください):私はあなたのコードの正しいバージョンは以下のだと思います。 POSTでparcel POSTがこれらの検証に合格した場合は、AddParcelに電話する必要があります。それ以外の場合は、投稿した間違ったオブジェクトをクライアントに返し、なぜ間違っているのかを説明する必要があります。

+0

@Downvoter上記の答えが間違っていることを指摘できれば、私はそれを訂正することを約束します。小さな誤りであれば、それは完全に間違っていれば削除します。 – Christos

+0

私はあなたのコードに何か間違いを見つけることができません..なぜdownvoteが適用されたidk ...だから私はそれを正常に戻すでしょう –

0
return View(); // problem here 

{ 
_parcelService.AddParcel(parcel); 

return RedirectToAction("Parcels", 
       new { TrackingID = parcel.TrackingID, controller = "Parcel" }); 
} 

ここに問題があります。ロジック(コード)がヒットする前に戻ってきます。この方法は、ライン `リターンで終了しますので...

[HttpPost] 
    public ActionResult AddParcel(ParcelDetail parcel) 
    { 
     // return View(); or just delete it.. since there is really no point to keep it commented out. 

     { 
      _parcelService.AddParcel(parcel); 

      return RedirectToAction("Parcels", 
       new { TrackingID = parcel.TrackingID, controller = "Parcel" }); 
     } 
    } 
関連する問題