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="../../Scripts/jquery-1.7.1.min.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(function () {
            $("a:contains('删除')").click(function () {  return confirm("确定要删除吗") })
        })
    </script>
   
</head>
<body>
    <div>
       <% List<UserInfo> list= SqlHelper.SelectDataToList<UserInfo>("select * from T_UserInfo" ) ;%> 
       <table>
            <tr><th>编码</th><th>姓名</th><th>年龄</th><th>删除</th></tr>
            <% foreach (var item in list)
               { %>
                    <tr><td><%: item.Id%></td><td><%:item.Name %></td><td><%:item.Age %></td><td> <%:Html.ActionLink("删除","Delete", "Home", new { id = item.Id }, new { })%></td>
                    
                    <td> <%:Html.ActionLink("编辑", "Edit", "Home", new { id = item.Id }, new { })%></td></tr>

             <%} %>  
                  

           
       </table>
    </div>
</body>
</html>

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="保存"/></td></tr>
        </table>
    </div>
    <%} %>
</body>
</html>


效果




郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。