首页 > 代码库 > 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参数的传递