2016-08-18 15 views
1

これは私が持っている問題の簡略化されたバージョンです。基本的に、Visual Studioでは、コントローラの中にオブジェクト(リストのようなもの)を作成することはできません。ここでリストを作成するときにコントローラからのビューを返すことができません

using System.Collections.Generic; 
using System.Web.Mvc; 

namespace HDDTest0818.Controllers 
{ 
    public class HomeController : Controller 
    { 
     public ViewResult Index() 
     { 
      public List<string> someList = new List<string>(); 

      return View(); 
     } 
    } 
} 

私は取得していますエラーです:

Index - HomeController.Index();: not all code paths return a value

は、第三のオープンブレース - } expected

returnからInvalid token 'return' in class, struct, or interface member declaration

Viewから'HomeController.View' must declare a body because it is not marked abstract, extern, or partial

Viewから'HomeController.View' hides inherited member 'Controller.View'. Use the new keyword if hiding was intended

ViewからMethod must have a return type

最後の閉じ中括弧 - Type or namespace definition, or end-of-file expected

+0

これらはすべてコンパイル時エラーです。無効なC#コードがあります。 – Louis

+1

'publicリスト someList =新しいリスト();' - > 'リスト someList =新しいリスト();' –

+0

ああ、ありがとうございました!それはうまくいった!どうしてそれは一般公開できないのですか? –

答えて

2

あなたはあなたのコードを修正する必要があります。

using System.Collections.Generic; 
using System.Web.Mvc; 

namespace HDDTest0818.Controllers 
{ 
    public class HomeController : Controller 
    { 
     //here is where you would declare your List variable public so that scope of this variable can be within the entire class... 
     // public List<string> someList = new List<string>(); 
     public ViewResult Index() 
     { 
      /*public*/ List<string> someList = new List<string>(); 
      //you need to get rid of public before you create your List variable 
      // if you want to declare this list variable as public you need to do it outside of the method (Index()).. 

      return View(); 
     } 
    } 
} 

は、このことができますなら、私を知ってみましょう!

+0

これは非常に便利です!ありがとうございました!!! –

+1

@TaylorLissようこそ。お役に立てて嬉しいです! –

関連する問題