2017-09-10 7 views
-1

私のアクションメソッド、モデルクラス、ビューは、コードエラーが発生したときに発生します: 'login.Models.Member1' login.Models.Member1 'には' GetEnumerator 'のパブリック定義が含まれていません。私を助けてください !!。テーブルデータをビュー(MVC)に表示したい

public ActionResult Details(Member1 mem) 
{ 
    var name = mem.Name; 
    var age = mem.Age; 
    var sex = mem.Sex; 
    var fees = mem.Fees; 
    OracleConnection connection = new OracleConnection(); 
    connection.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["DBConnectionDHC"].ToString(); 
    connection.Open(); 
    OracleCommand command = connection.CreateCommand(); 
    string sql = "SELECT * FROM MEMBER"; 
    command.CommandText = sql; 
    return View(); 
} 

マイモデル:

public class Member1 
{ 
    public String Name { get; set; } 
    public String Age { get; set; } 
    public String Sex { get; set; } 
    public String Fees { get; set; } 
} 

ビュー:

@model login.Models.Member1 

@{ 
    ViewBag.Title = "Details"; 
    Layout = null; 
} 
<table> 
    <thead> 
     <tr> 
      <th>Name</th> 
      <th>Age</th> 
      <th>Sex</th> 
      <th>Fees</th> 
     </tr> 
    </thead> 
    <tbody> 
     @foreach (var item in Model) 
     { 
      <tr> 
       <td>@item.Name</td> 
       <td>@item.Age</td> 
       <td>@item.Sex</td> 
       <td>@item.Fees</td> 
      </tr> 
     } 
    </tbody> 
</table> 
+0

結構ですあなたが複数のエラーを持っている

public ActionResult Details(Member1 mem) { var name = mem.Name; var age = mem.Age; var sex = mem.Sex; var fees = mem.Fees; OracleConnection connection = new OracleConnection(); connection.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["DBConnectionDHC"].ToString(); connection.Open(); OracleCommand command = connection.CreateCommand(); string sql = "SELECT * FROM MEMBER"; command.CommandText = sql; var result = command.executeNonQuery() //or reader just make sure you //make sure you get result in result variable // than send it to view return View(result); } 

をare' - あなたのビューは '' @modelのIEnumerable にする必要がある - コレクション、すなわち。しかし、あなたはモデルをビューに渡す必要があります - コントローラーの 'return View(model);' 'しかし' 'あなたはモデルを初期化したことがなく、あなたのコードは' 'コマンドを実行しません。また、メソッドの 'Member1 mem'パラメータは意味をなさない。 –

答えて

0

あなたがミスになっています。 最初にコマンドを実行して初期化する必要があります。

VAR結果= command.executeNonQuery()またはcommand.executeReader()//この

リターン・ビューを使用して、閲覧あなたにそれを返すために与えたよりも、私が与えたそのでし年齢は(この

を使用var);

ので、私の提案された変更はすべてのものが

関連する問題