2016-09-13 2 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

01,
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

당신은'[HttpPost]'로 표시되어있는'Index()'GET 메소드가 없습니다. –

+0

하지만 같은 뷰에서 데이터를 삽입하고 가져올 수있는 방법은 무엇입니까 –

+1

먼저 그러나 당신의 코드는 어쨌든 제대로 작동하지 않을 것입니다 (여러분은 모델의 콜렉션을'DataSet'가 아닌 –

답변

0

return View("Index") 인덱스보기를 반환하고 [HttpPost]을 제거 할 수 있습니다. 다른 것을 무시하십시오. 당신은 뷰 모델을 만들 수 있습니다, 대신보기 데이터 집합을 전달하는

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 
    } 

과 볼을 전달합니다

0

는 다음과 같이 코드를 수정합니다.

관련 문제