本文介绍了如何在mvc4的单视图中进行CRUD操作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! web.config < connectionStrings> < add name =connectconnectionString =Data Source = TC014PL28 \\ \\ TCDBS; Initial Catalog = Insertdemo; Persist Security Info = True; User ID = sa; Password = satest @ 123providerName =System.Data.SqlClient/> < / connectionStrings> BAL.CS 使用系统; 使用System.Collections .Generic; 使用System.ComponentModel.DataAnnotations; 使用System.Linq; 使用System.Web; 名称空间Insertdemo.Models { 公共类Bal { public int uid {get;组; } [必填] public string uname {get;组; } [必填] [StringLength(6)] public string ugender {get;组; } [必填] 公共字符串城市{get;组; } [必填] public string img {get;组; } //公共列表< Bal> userlist {get;组; } } } Dal.cs 使用System; 使用System.Collections.Generic; 使用System.Linq; 使用System.Web; 使用System.Web.Mvc; 使用System.Data; 使用System.Data.SqlClient; 使用System.Configuration; 使用Insertdemo.Models; 使用System.IO; 名称空间Insertdemo.Models { 公共类Dal { SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings [connect]。ConnectionString.ToString()); SqlCommand cmd; // SqlDataAdapter sda; // DataSet ds; public void Insert(Bal ba) { con.Open(); cmd = new SqlCommand(insert_data,con); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue(@ uname,ba.uname); cmd.Parameters.AddWithValue(@ ugender,ba.ugender ); cmd.Parameters.AddWithValue(@ city,ba.city); cmd.Parameters.AddWithValue(@ img,ba.img); cmd.ExecuteNonQuery(); con.Close(); } public List< Bal>详情() { List< Bal> li = new List< Bal>(); SqlCommand cmd = new SqlCommand(select * from tbl_user,con); con.Open(); SqlDataReader dr = cmd.ExecuteReader(); while(dr.Read()) { Bal ba = new Bal(); ba.uid = Convert.ToInt32(dr [uid]); ba.uname = dr [uname]。ToString(); ba.ugender = dr [ugender]。ToString(); ba.city = dr [city ] .ToString(); ba.img = dr [img]。ToString(); li.Add(ba); } con.Close(); 返回li; } public Bal getid(int id) { Bal ba = new Bal(); SqlCommand cmd = new SqlCommand( select * from tbl_use r其中uid ='+ id +',con); con.Open(); SqlDataReader dr = cmd .ExecuteReader(); while(dr.Read()) { ba.uid = Convert.ToInt32(dr [uid ]); ba.uname = dr [uname]。ToString(); ba.ugender = dr [ugender]。ToString(); ba.city = dr [city]。ToString(); ba.img = dr [img]。ToString(); } con.Close(); 返回ba; } 公共Bal getdeatails(int id) { Bal ba = new Bal(); SqlCommand cmd = new SqlCommand (select * from tbl_user where uid ='+ id +',con); con.Open(); SqlDataReader dr = cmd.ExecuteReader(); while(dr.Read()) { ba.uid = Convert.ToInt32(dr [uid]); ba .uname = dr [uname]。ToString(); ba.ugender = dr [ugender]。ToString(); ba.city = dr [city]。ToString(); ba.img = dr [img]。ToString(); } con.Close(); 返回ba; } public void删除(int id) { con.Open(); // Bal ba = new Bal(); cmd =新的SqlCommand(从tbl_user删除,其中uid ='+ id +',con); cmd.Parameters.AddWithValue(@ uid,id); cmd.ExecuteNonQuery(); con.Close(); } public void savechange (Bal ba,int id) { con.Open(); // Bal ba = new Bal(); cmd = new SqlCommand(updateuser,con); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue(@ uname,ba.uname); cmd。 Parameters.AddWithValue(@ ugender,ba.ugender); cmd.Parameters.AddWithValue(@ city,ba.city); cmd.Parameters。 AddWithValue(@ img,ba.img); cmd.Parameters.AddWithValue(@ uid,id); cmd.ExecuteNonQuery(); con.Close(); } 公共Bal getupdateid(int id) { Bal ba = new Bal(); SqlCommand cmd = new SqlCommand(select * from tbl_user where uid ='+ id +', con); con.Open(); SqlDataReader dr = cmd.E xecuteReader(); while(dr.Read()) { ba.uid = Convert.ToInt32(dr [uid ]); ba.uname = dr [uname]。ToString(); ba.ugender = dr [ugender]。ToString(); ba.city = dr [city]。ToString(); ba.img = dr [img]。ToString(); } con.Close(); 返回ba; } } } InsrtController.cs 使用System; 使用System.Collections.Generic; 使用System.Linq; 使用System.Web ; 使用System.Web.Mvc; 使用System.Data; 使用System.Data.SqlClient; 使用System.Configuration; 使用Insertdemo.Models; 使用System.IO; 名称空间Insertdemo.Contr ollers { 公共类InsertController:Controller { Bal ba = new Bal(); Dal da = new Dal(); public ActionResult Insert() { 返回查看(); } [HttpPost] public ActionResult Insert(Bal ba, HttpPostedFileBase文件) { if(file!= null) { file.SaveAs(Server.MapPath(〜/ Images /+ file.FileName)); ba.img =〜/ Images /+ file.FileName; } da.Insert(ba); Response.Write(成功插入!!); 返回RedirectToAction(详细信息,插入); } public ActionResult Details() { 返回查看(da.details()); } public ActionResult Delete(int id) { 返回查看(da.getid(id)); } [HttpPost] 公共ActionResult删除(Bal ba,int id) { da.Delete(id); 返回RedirectToAction(详细信息); } public ActionResult Getdeatails(int id) { 返回查看(da.getdeatails(id)); } public ActionResult编辑(int id) { 返回视图(da.getupdateid(id)); } [HttpPost] 公共ActionResult编辑(Bal ba,int id,HttpPostedFileBase文件) { if(file!= null) { file.SaveAs(Server.MapPath(〜/ Images /+ file.FileName)); ba.img =〜/ Images / + file.FileName; } da.savechange(ba,id); 返回RedirectToAction(Details); } // [HttpPost] //公共ActionResult FileUpload(HttpPostedFileBase uploadFile) // { // if(uploadFile.ContentLength> 0) // { //字符串filePath = Path.Combine(HttpContext.Server.MapPath(../ Images), // Path.GetFileName(uploadFile.FileName)); // uploadFile.SaveAs(filePath); //} //返回查看(); //} } } Insert.chtml @model Insertdemo.Models.Bal @ { 布局= null; } < ;!DOCTYPE html> < html> < head> < meta name = viewportcontent =width = device-width/> < title>插入< / title> < / head> < body> < script src =〜/ Scripts / jquery-1.7.1.min.js>< / script> < S cript src =〜/ Scripts / jquery.validate.min.js>< / script> < script src =〜/ Scripts / jquery.validate.unobtrusive.min.js >< / script> @using(Html.BeginForm(Insert,Insert,null,FormMethod.Post,new {enctype = multipart / form-data})) { @ Html.ValidationSummary(true) < ; fieldset> < legend>插入用户信息< / legend> < div class =editor-label> 用户名 < / div> < div class =editor-field> ; @ Html.EditorFor(model => model.uname) @ Html.ValidationMessageFor(model => model.uname) < / div> < div class =editor-label> 性别 < / div> < div class =editor-field> @ Html.EditorFor(model => model.ugender) @ Html.ValidationMessageFor(model => model.ugender) < / div> < div class =editor-label> City < / div> < div class =editor-field> @ Html.EditorFor(model => model.city) @ Html.ValidationMessageFor(model => model.city) < / div> < div class =editor-label> 用户照片 < / div> < div class =editor-field> < input type =filename =file/> < / div> < p> < input type =submitvalue =Create/> < / p> < / fieldset> } < div> @ Html.ActionLink(返回列表,详情) < / div> < / body> < / html> getdetails.chatml @model Insertdemo.Models.Bal @ { 布局= null; } <!DOCTYPE html> < html> < head> < meta name =viewportcontent =width = device-width/> < title> getdeatails< ; / title> < / head> < body> < fieldset> < ;传奇>用户信息< / legend> < div class =display-label> 用户名 < / div> < div class =display-field> @ Html.DisplayFor(model => model.uname) < / div> < div class =display-label> 性别 < / div> < div class =display-field> @ Html.DisplayFor( model => model.ugender) < / div> < div class =display-label> City < / div> < div class =display-field> @ Html.DisplayFor(model => model.city) < / div> < / fieldset> < p> @ Html.ActionLink(编辑,编辑,新{id = Model.uid})| @ Html.ActionLink(返回列表, 详情) < / p> < / body> < / html> Edit.chtml @model Insertdemo.Models.Bal @ { 布局= null; } <!DOCTYPE html> < html> < head> < meta name =viewportcontent =width = device-width /> < title>编辑< / title> < / head> < body> < script src =〜/ Scripts / jquery-1.7.1.min.js>< / script> < script src =〜/ Scripts / jquery .validate.min.js>< / script> < script src =〜/ Scripts / jquery.validate.unobtrusive.min.js>< / script> @using(Html.BeginForm(编辑,插入,null,FormMethod.Post,new {enctype =multipart / form-data})) { @ Html.ValidationSummary(true) < fieldset> < legend>编辑用户名< / legend> < div class =editor-label> U ser name < / div> < div class =editor-field> @ Html.EditorFor(model = > model.uname) @ Html.ValidationMessageFor(model => model.uname) < / div> < div class =editor-label> 性别 < / div> < div class =editor-field> @ Html.EditorFor(model => model.ugender) @ Html.ValidationMessageFor(model => model.ugender) < / div> < div class =editor-label> City < / div> < div class =editor-field> @ Html.EditorFor(model => model.city) @ Html.ValidationMessageFor(model => model.city) < / div> < div class =editor-label> 用户照片 < / div> < div class =editor-field> < input type =filename =file/> < / div> < p> < input type =submitvalue =Save/> < / p> < / fieldset> } < div> @ Html.ActionLink(返回列表,详情) < / div> < / body> < / html> Details.chtml @model IEnumerable< Insertdemo.Models.Bal> @ { 布局= null; } <!DOCTYPE html> < html> < head> < meta name =viewportcontent =width = device-width/> < title>用户详情< / title> < / head> < body> < p> @ Html.ActionLink(新建,插入) < / p&g t; < table> < tr> < th> 用户名 < / th> < th> 性别 < / th> < th> City < / th> < th> 用户照片 < / th> < th> 运营 < / th> < / tr> @foreach(模型中的var项目){ < tr> < td> @ Html.DisplayFor(modelItem => item.uname) < / td> < td> @ Html.DisplayFor(modelItem => item.ugender) < / td> < td> @ Html.DisplayFor(modelItem => item.city) < / td> < td> @ * @ Html.DisplayFor(modelItem => item.Photo)* @ < img src =@ Url.Content(item.img)height =100pxwidth =100px/> < / td> < td> @ Html.ActionLink(编辑,编辑,新{id = item.uid})| @Html。 ActionLink(详细信息,Getdeatails,新{id = item.uid})| @ Html.ActionLink(删除,删除,新{id = item.uid}) < / td> < / tr> } < / table> < / body> < / html> 删除.chtml @model Insertdemo.Models.Bal @ { 布局= null; } <!DOCTYPE html> < html> < head> < meta name =viewportcontent =width = device-width/> < title>删除< / title> < / head> < body> < h3>您确定要删除吗?< / h3> < fieldset> < legend>删除用户记录< / legend> < div class =display-label> 用户名 < ; / div> < div class =display-field> @ Html.DisplayFor(model => model.uname) < / div> < div class =display-label> 性别 < / div> < div class =display-field> @ Html.DisplayFor( model => model.ugender) < / div> < div class =display-label> City < / div> < div class =display-field> @ Html.DisplayFor(model => model.city) < / div> < / fieldset> @using(Html .BeginForm()){ < p> < input type =submitvalue =Delete/> | @ Html.ActionLink(返回列表,详情) < / p> } < / body> < / html>web.config<connectionStrings> <add name="connect" connectionString="Data Source=TC014PL28\TCDBS;Initial Catalog=Insertdemo;Persist Security Info=True;User ID=sa;Password=satest@123" providerName="System.Data.SqlClient" /> </connectionStrings>BAL.CSusing System;using System.Collections.Generic;using System.ComponentModel.DataAnnotations;using System.Linq;using System.Web;namespace Insertdemo.Models{ public class Bal { public int uid { get; set; } [Required] public string uname { get; set; } [Required] [StringLength(6)] public string ugender { get; set; } [Required] public string city { get; set; } [Required] public string img { get; set; } //public List<Bal> userlist { get; set; } }}Dal.csusing System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Mvc;using System.Data;using System.Data.SqlClient;using System.Configuration;using Insertdemo.Models;using System.IO;namespace Insertdemo.Models{ public class Dal { SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["connect"].ConnectionString.ToString()); SqlCommand cmd; //SqlDataAdapter sda; //DataSet ds; public void Insert(Bal ba) { con.Open(); cmd = new SqlCommand("insert_data", con); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@uname", ba.uname); cmd.Parameters.AddWithValue("@ugender", ba.ugender); cmd.Parameters.AddWithValue("@city", ba.city); cmd.Parameters.AddWithValue("@img", ba.img); cmd.ExecuteNonQuery(); con.Close(); } public List<Bal> details() { List<Bal> li = new List<Bal>(); SqlCommand cmd = new SqlCommand("select * from tbl_user", con); con.Open(); SqlDataReader dr = cmd.ExecuteReader(); while (dr.Read()) { Bal ba = new Bal(); ba.uid = Convert.ToInt32(dr["uid"]); ba.uname = dr["uname"].ToString(); ba.ugender = dr["ugender"].ToString(); ba.city = dr["city"].ToString(); ba.img = dr["img"].ToString(); li.Add(ba); } con.Close(); return li; } public Bal getid(int id) { Bal ba = new Bal(); SqlCommand cmd = new SqlCommand("select * from tbl_user where uid='" + id + "'", con); con.Open(); SqlDataReader dr = cmd.ExecuteReader(); while (dr.Read()) { ba.uid = Convert.ToInt32(dr["uid"]); ba.uname = dr["uname"].ToString(); ba.ugender = dr["ugender"].ToString(); ba.city = dr["city"].ToString(); ba.img = dr["img"].ToString(); } con.Close(); return ba; } public Bal getdeatails(int id) { Bal ba = new Bal(); SqlCommand cmd = new SqlCommand("select * from tbl_user where uid='" + id + "'", con); con.Open(); SqlDataReader dr = cmd.ExecuteReader(); while (dr.Read()) { ba.uid = Convert.ToInt32(dr["uid"]); ba.uname = dr["uname"].ToString(); ba.ugender = dr["ugender"].ToString(); ba.city = dr["city"].ToString(); ba.img = dr["img"].ToString(); } con.Close(); return ba; } public void Delete(int id) { con.Open(); // Bal ba=new Bal(); cmd = new SqlCommand("delete from tbl_user where uid='" + id + "'", con); cmd.Parameters.AddWithValue("@uid", id); cmd.ExecuteNonQuery(); con.Close(); } public void savechange(Bal ba, int id) { con.Open(); // Bal ba = new Bal(); cmd = new SqlCommand("updateuser", con); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@uname", ba.uname); cmd.Parameters.AddWithValue("@ugender", ba.ugender); cmd.Parameters.AddWithValue("@city", ba.city); cmd.Parameters.AddWithValue("@img", ba.img); cmd.Parameters.AddWithValue("@uid", id); cmd.ExecuteNonQuery(); con.Close(); } public Bal getupdateid(int id) { Bal ba = new Bal(); SqlCommand cmd = new SqlCommand("select * from tbl_user where uid='" + id + "'", con); con.Open(); SqlDataReader dr = cmd.ExecuteReader(); while (dr.Read()) { ba.uid = Convert.ToInt32(dr["uid"]); ba.uname = dr["uname"].ToString(); ba.ugender = dr["ugender"].ToString(); ba.city = dr["city"].ToString(); ba.img = dr["img"].ToString(); } con.Close(); return ba; } }}InsrtController.csusing System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Mvc;using System.Data;using System.Data.SqlClient;using System.Configuration;using Insertdemo.Models;using System.IO;namespace Insertdemo.Controllers{ public class InsertController : Controller { Bal ba = new Bal(); Dal da = new Dal(); public ActionResult Insert() { return View(); } [HttpPost] public ActionResult Insert(Bal ba, HttpPostedFileBase file) { if (file != null) { file.SaveAs(Server.MapPath("~/Images/" + file.FileName)); ba.img = "~/Images/" + file.FileName; } da.Insert(ba); Response.Write("Successfully insert!!"); return RedirectToAction("Details", "Insert"); } public ActionResult Details() { return View(da.details()); } public ActionResult Delete(int id) { return View(da.getid(id)); } [HttpPost] public ActionResult Delete(Bal ba,int id) { da.Delete(id); return RedirectToAction("Details"); } public ActionResult Getdeatails(int id) { return View(da.getdeatails(id)); } public ActionResult Edit(int id) { return View(da.getupdateid(id)); } [HttpPost] public ActionResult Edit(Bal ba, int id, HttpPostedFileBase file) { if (file != null) { file.SaveAs(Server.MapPath("~/Images/" + file.FileName)); ba.img = "~/Images/" + file.FileName; } da.savechange(ba,id); return RedirectToAction("Details"); } //[HttpPost] //public ActionResult FileUpload(HttpPostedFileBase uploadFile) //{ // if (uploadFile.ContentLength > 0) // { // string filePath = Path.Combine(HttpContext.Server.MapPath("../Images"), // Path.GetFileName(uploadFile.FileName)); // uploadFile.SaveAs(filePath); // } // return View(); //} }}Insert.chtml@model Insertdemo.Models.Bal@{ Layout = null;}<!DOCTYPE html><html><head> <meta name="viewport" content="width=device-width" /> <title>Insert</title></head><body> <script src="~/Scripts/jquery-1.7.1.min.js"></script> <script src="~/Scripts/jquery.validate.min.js"></script> <script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script> @using (Html.BeginForm("Insert", "Insert", null, FormMethod.Post, new { enctype = "multipart/form-data" })) { @Html.ValidationSummary(true) <fieldset> <legend>Insert User Inforamation</legend> <div class="editor-label"> User Name </div> <div class="editor-field"> @Html.EditorFor(model => model.uname) @Html.ValidationMessageFor(model => model.uname) </div> <div class="editor-label"> Gender </div> <div class="editor-field"> @Html.EditorFor(model => model.ugender) @Html.ValidationMessageFor(model => model.ugender) </div> <div class="editor-label"> City </div> <div class="editor-field"> @Html.EditorFor(model => model.city) @Html.ValidationMessageFor(model => model.city) </div> <div class="editor-label"> User Photo </div> <div class="editor-field"> <input type="file" name="file"/> </div> <p> <input type="submit" value="Create" /> </p> </fieldset> } <div> @Html.ActionLink("Back to List", "Details") </div></body></html>getdetails.chatml@model Insertdemo.Models.Bal@{ Layout = null;}<!DOCTYPE html><html><head> <meta name="viewport" content="width=device-width" /> <title>getdeatails</title></head><body> <fieldset> <legend>User Inforamation</legend> <div class="display-label"> User Name </div> <div class="display-field"> @Html.DisplayFor(model => model.uname) </div> <div class="display-label"> Gender </div> <div class="display-field"> @Html.DisplayFor(model => model.ugender) </div> <div class="display-label"> City </div> <div class="display-field"> @Html.DisplayFor(model => model.city) </div> </fieldset> <p> @Html.ActionLink("Edit", "Edit", new { id= Model.uid}) | @Html.ActionLink("Back to List", "Details") </p></body></html>Edit.chtml@model Insertdemo.Models.Bal@{ Layout = null;}<!DOCTYPE html><html><head> <meta name="viewport" content="width=device-width" /> <title>Edit</title></head><body> <script src="~/Scripts/jquery-1.7.1.min.js"></script> <script src="~/Scripts/jquery.validate.min.js"></script> <script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script> @using (Html.BeginForm("Edit", "Insert", null, FormMethod.Post, new { enctype = "multipart/form-data" })) { @Html.ValidationSummary(true) <fieldset> <legend>Edit User Name</legend> <div class="editor-label"> User Name </div> <div class="editor-field"> @Html.EditorFor(model => model.uname) @Html.ValidationMessageFor(model => model.uname) </div> <div class="editor-label"> Gender </div> <div class="editor-field"> @Html.EditorFor(model => model.ugender) @Html.ValidationMessageFor(model => model.ugender) </div> <div class="editor-label"> City </div> <div class="editor-field"> @Html.EditorFor(model => model.city) @Html.ValidationMessageFor(model => model.city) </div> <div class="editor-label"> User Photo </div> <div class="editor-field"> <input type="file" name="file"/> </div> <p> <input type="submit" value="Save" /> </p> </fieldset> } <div> @Html.ActionLink("Back to List", "Details") </div></body></html>Details.chtml@model IEnumerable<Insertdemo.Models.Bal>@{ Layout = null;}<!DOCTYPE html><html><head> <meta name="viewport" content="width=device-width" /> <title>Details Of User</title></head> <body> <p> @Html.ActionLink("Create New", "Insert") </p> <table> <tr> <th> User Name </th> <th> Gender </th> <th> City </th> <th> User Photo </th> <th> Operations </th> </tr> @foreach (var item in Model) { <tr> <td> @Html.DisplayFor(modelItem => item.uname) </td> <td> @Html.DisplayFor(modelItem => item.ugender) </td> <td> @Html.DisplayFor(modelItem => item.city) </td> <td> @* @Html.DisplayFor(modelItem => item.Photo)*@ <img src="@Url.Content(item.img)" height="100px" width="100px"/> </td> <td> @Html.ActionLink("Edit", "Edit", new { id = item.uid }) | @Html.ActionLink("Details", "Getdeatails", new { id = item.uid}) | @Html.ActionLink("Delete", "Delete", new { id = item.uid}) </td> </tr> } </table></body></html>Delete.chtml@model Insertdemo.Models.Bal@{ Layout = null;}<!DOCTYPE html><html><head> <meta name="viewport" content="width=device-width" /> <title>Delete</title></head><body> <h3>Are you sure you want to delete this?</h3> <fieldset> <legend>Delete User Record</legend> <div class="display-label"> User Name </div> <div class="display-field"> @Html.DisplayFor(model => model.uname) </div> <div class="display-label"> Gender </div> <div class="display-field"> @Html.DisplayFor(model => model.ugender) </div> <div class="display-label"> City </div> <div class="display-field"> @Html.DisplayFor(model => model.city) </div> </fieldset> @using (Html.BeginForm()) { <p> <input type="submit" value="Delete" /> | @Html.ActionLink("Back to List", "Details") </p> }</body></html>推荐答案直接投入从控制器调用db。 你用通用存储库创建一个insertmanager和insertinterface。 和来自控制器的呼叫管理器。insted of direct call from controller to db.you create one "insertmanager" and "insertinterface" with "generic repository".and call manager from controller. 这篇关于如何在mvc4的单视图中进行CRUD操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-20 08:14