2016-06-15 9 views
0

これはストアドプロシージャを使って複雑な型を移入するエンティティプロジェクトで、通常のエンティティオブジェクトのように扱うことができます。私の変数はどのように割り当てられておらず、それを満たしていないパスはありません

私に与えているエラーはReturn("Index", model);です。'Use of unassigned local variable 'model'です。ここで

はコードです:

public ActionResult FilterCallReview(DateTime StartDate, DateTime EndDate, string searchField, string searchValue) 
{ 
    ObjectResult<MasterMxieCallReview_Result> model; 

    if (String.IsNullOrWhiteSpace(searchValue)) 
    { 
     model = spdb.MasterMxieCallReview(StartDate, EndDate); 
    } 
    else 
    { 
     if (searchField.ToLower() == "contract_number") 
     { 
      // Check if we need to ignore the date 
      if (1 == 1) 
      { 
       // Run the SP with dates 
       model = spdb.MasterMxieCallReview_ContractNum(StartDate, EndDate, searchValue); 
      } 
      else 
      { 
       // Run the SP without dates 
       model = spdb.MasterMxieCallReview_ContractNum(null, null, searchValue); 
      } 
     } 
     else if (searchField.ToLower() == "phone_number") 
     { 
      // Check if we need to ignore the date 
      if (1 == 1) 
      { 
       // Run the SP with dates 
       model = spdb.MasterMxieCallReview_ContractNum(StartDate, EndDate, searchValue); 
      } 
      else 
      { 
       // Run the SP without dates 
       model = spdb.MasterMxieCallReview_ContractNum(StartDate, EndDate, searchValue); 
      } 
     } 
    } 

    ViewBag.StartDate = StartDate.ToShortDateString(); 
    ViewBag.EndDate = EndDate.ToShortDateString(); 

    // Model should never be allowed to be null at this point 
    return View("Index", model); 
} 

modelが私の知識に取り込まれていない任意のパス、私はここで間違ってやっているものを任意のアイデアはありませんか?

答えて

2

phone_numberまたはcontract_number以外の場合、モデルは初期化されません。

+0

ブラストしました。これは長すぎます。このセクションのデフォルトのelse文を追加しました。ありがとうございました。 –

1

「if-elseステートメント」のパスに「model」という変数の値が割り当てられないため、モデルがnullになる可能性があります。すべての可能な方法を考慮して、すべてのケースで「モデル」変数が価値を持つことを確認する必要があります。

すべての場合にモデル変数を割り当てるには、 'else if'の前に 'else'ステートメントが必要です。

+0

私は何かを見落としていたことを知っていました。あなたが答えを見つけるかもしれない場所を少しわかりやすくするために答えを編集できるなら、私はこれをアップヴォートにするのが大好きです! –

+0

ここにあります:) – AndrewGolota

関連する問題