2016-09-13 20 views
-1

enter image description hereデータを挿入し、同じビューから挿入したデータを表示します。部分ビューを使用することができます。助けてください、ここでは挿入時にエラーを出しています。mvcの同じビューにデータを挿入して表示する

error screenshot

Entityクラス

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.ComponentModel.DataAnnotations; 
namespace WebApplication7.Models 
{ 
    public class Class1 
    { 
     [Key] 
     public int id { get; set; } 
     [Required(ErrorMessage="Name plz")] 
     public string Name { get; set; } 
     public string Address { get; set; } 
     public string email { get; set; } 
     public string phone { get; set; } 
    } 
} 

データ層クラス

public class dlayer 
{ 
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings\["connection"\].ConnectionString); 

    public DataSet getalldata() 
    { 
     string query = "select * from employee"; 
     SqlCommand cmd = new SqlCommand(query,con); 
     //cmd.CommandText = "select * from employee"; 
     //cmd.CommandType = CommandType.Text; 
     //cmd.Connection = con; 
     SqlDataAdapter da = new SqlDataAdapter(cmd); 
     DataSet ds = new DataSet();  
     da.Fill(ds); 
     return ds; 
    } 

    public string insert(Class1 cl1) 
    { 
     string STR = ""; 
     SqlCommand cmd = new SqlCommand(); 
     cmd.CommandText = "insert into employee(Name,Address,email,phone) values(@Name,@Address,@email,@phone)"; 
     cmd.Parameters.AddWithValue("@Name", cl1.Name); 
     cmd.Parameters.AddWithValue("@Address", cl1.Address); 
     cmd.Parameters.AddWithValue("@email", cl1.email); 
     cmd.Parameters.AddWithValue("@phone", cl1.phone); 
     cmd.CommandType = CommandType.Text; 
     cmd.Connection = con; 
     con.Open(); 
     int result = cmd.ExecuteNonQuery(); 
     con.Close(); 
     return STR = Convert.ToString(result); 
    } 
} 

デフォルトcontroller.cs

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.Mvc; 
using WebApplication7.Models; 
using WebApplication7.DATALAYER; 
using System.Data; 
namespace WebApplication7.Controllers 
{ 
    public class DefaultController : Controller 
    { 
     public ActionResult Index() 
    { 
     dlayer dl = new dlayer(); 
     DataSet ds = new DataSet(); 
     DataTable dt = new DataTable(); 
     dt = dl.getalldata(); 
     List<Class1> products = new List<Class1>(); 

     foreach (DataRow dr in dt.Rows) 
     { 

      products.Add(new Class1() { id = int.Parse(dr[0].ToString()), Name = dr[1].ToString(), phone = dr[2].ToString(), email = dr[3].ToString() });  
     }  
     return View(products); 
    } 

     public ActionResult insert() 
     { 
      return View(); 
     } 

     [HttpPost] 
     public ActionResult insert(Class1 cls) 
     { 
      dlayer dl = new dlayer(); 
      string A = dl.insert(cls); 
     } 
    } 
} 
+1

GETメソッドはありません(表示されているものは 'HttpPost 'と表示されています) –

+0

ですが、同じビューでデータを挿入したりフェッチしたりするにはどうすればいいですか? –

+1

まずは、しかし、あなたのコードはとにかく正しく動作しません(モデルのコレクションを 'DataSet'ではなくビューに渡す必要があります) –

答えて

0

return View("Index")インデックスビューを返して[HttpPost]を削除することができます。 他のものは無視してください。

0

のようにコードを変更:代わりに表示するDataSetを渡す

public class DefaultController : Controller 
{ 
    // [HttpPost] /*Remove this to initial load of Index page with GET method*/ 
    public ActionResult Index() 
    { 
     dlayer dl = new dlayer(); 
     DataSet ds = new DataSet(); 
     ds=dl.getalldata();   
     return View(ds); 
    } 

    public ActionResult insert() 
    { 
     return View(); 
    } 

    [HttpPost] 
    public ActionResult insert(Class1 cls) 
    { 
     dlayer dl = new dlayer(); 
     string A = dl.insert(cls); 

    return RedirectToAction("Index"); //This line for redirecting to Index after Insert 
    } 

、あなたはViewModelにを作成し、表示するためにそれを渡すことができます。

関連する問題