在mvc中插入数据并显示在同一视图中

时间:2016-09-13 07:58:58

标签: c# asp.net-mvc

在此输入图片说明我想插入数据并显示插入的数据从同一观点来看。我们可以使用局部视图。请帮忙,这里是插入时出错。





“错误屏幕截图”





实体类




 使用System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Web;
 using System.ComponentModel.DataAnnotations;
 namespace WebApplication7.Models& #xA; {
公共类Class1
 {
 [密钥]
 public int id {get;组; }
 [必需(ErrorMessage =“Name plz”)]
 public string Name {get;组; }
 public string Address {get;组; }
 public string email {get;组; }
公共串电话{get;组; }
 }
}
  




数据层类


&#xA ;
  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;
 }

公共字符串插入(Class1 cl1)
 {
 string STR =“”;
 SqlCommand cmd = new SqlCommand();
 cmd.CommandText =“插入员工(姓名,地址,电子邮件,电话)值(@姓名,@地址,@电子邮件,@电话)”;
 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


&# xA;
 使用System;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.Web.Mvc ;
使用WebApplication7.Models;
使用WebApplication7.DATALAYER;
使用System.Data;
命名空间WebApplication7.Controllers
 {
 public class DefaultController:Controller
 {
 public ActionResult Index()
 {
 dlayer dl = new dlayer();
 DataSet ds = new DataSet();
 DataTable dt = new DataTable();
 dt = dl.getalldata();
列表与LT;&的Class1 GT; 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()}); 
 
返回查看(产品);
 }

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

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



2 个答案:

答案 0 :(得分:0)

您可以return View("Index")返回索引视图并删除[HttpPost]。 只要忽略其他任何事情。

答案 1 :(得分:0)

将您的代码修改为:

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并将其传递给view。

,而不是将DataSet传递给View