首页 > 代码库 > Mvc参数的传递
Mvc参数的传递
<1>
Home控制器
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using System.Data; using MVC.Models; using System.Data.SqlClient; using LLSql.DAL; namespace MVC.Controllers { public class HomeController : Controller { // // GET: /Home/ public ActionResult Index() { List<UserInfo> userinfo = LLSql.DAL.SqlHelper.SelectDataToList<UserInfo>("select * from T_UserInfo"); ViewData["UserInfo"] = userinfo; return View("Show"); } public ActionResult Delete(int id) { LLSql.DAL.SqlHelper.ExecuteNonQuery("delete from T_UserInfo where id=@id", new SqlParameter("id", id)); return View("Show"); } [HttpGet] public ActionResult Edit(int id) { UserInfo userinfo = (UserInfo)SqlHelper.SelectDataToList<UserInfo>("select * from T_UserInfo where id=@id", new System.Data.SqlClient.SqlParameter("id", id)).First(); ViewData.Model = userinfo; return View("Edit"); } public ActionResult Edit(UserInfo userinfo) { SqlHelper.ExecuteNonQuery("update T_Userinfo set Name=@name, Age=@age where id=@id", new SqlParameter("name", userinfo.Name), new SqlParameter("age", userinfo.Age), new SqlParameter("id", userinfo.Id)); return View("Show"); } } }
UserInfo Model
using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace MVC.Models { public class UserInfo { public int Id { get; set; } public string Name { get; set; } public int Age { get; set; } } }
Show视图(用来展示数据)
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<dynamic>" %> <%@ Import Namespace="MVC.Models" %> <%@ Import Namespace="LLSql.DAL" %> <!DOCTYPE html> <html> <head runat="server"> <meta name="viewport" content="width=device-width" /> <title>Show</title> <script src=http://www.mamicode.com/Scripts/jquery-1.7.1.min.js" type="text/javascript"></script>>Edit 视图 (用来编辑)
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<MVC.Models.UserInfo>" %> <%@ Import Namespace="MVC.Models" %> <%@ Import Namespace="LLSql.DAL" %> <!DOCTYPE html> <html> <head runat="server"> <meta name="viewport" content="width=device-width" /> <title>Edit</title> </head> <body> <% using (Html.BeginForm()) {%> <div> <table> <tr><th>编码</th><td><%:Model.Id%> <%: Html.HiddenFor(u => u.Id)%></td></tr> <tr><td>姓名</td><td><%:Html.TextBoxFor(u => u.Name)%> </td></tr> <tr><td>年龄</td><td><%: Html.TextBoxFor(u => u.Age)%> </td></tr> <tr><td colspan='2'><input type="submit" value=http://www.mamicode.com/"保存"/>>
效果
Mvc参数的传递
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。