码迷,mamicode.com
首页 > Web开发 > 详细

Mvc参数的传递

时间:2014-09-26 00:28:28      阅读:271      评论:0      收藏:0      [点我收藏+]

标签: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>


效果

bubuko.com,布布扣

bubuko.com,布布扣




Mvc参数的传递

标签:mvc参数的传递

原文地址:http://blog.csdn.net/fanbin168/article/details/39560115

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!