Asp.Net之MVC(三)

1.添加用户

using MvcApplication1.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MvcApplication1.Controllers
{
    public class HomeController : Controller
    {
        // GET: Home
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult About()
        {
            return View();
        }

        public ActionResult Register()
        {
            return View();
        }
        [HttpPost]//只能POST请求,优先!!!!
        public ActionResult Register(Student student)
        {
            itcastdbEntities db = new itcastdbEntities();
            student.RegTime = DateTime.Now;
            db.Student.Add(student);
            if (db.SaveChanges() > 0)
            {
               // return Content("添加成功!!!");
                return RedirectToAction("Index","Student");//列表页  Student控制器下的Index方法
            }
            else
            {
                return Content("添加失败!!!");
            }
        }


        //  public ActionResult AddStudent(string txtUserName,string txtUserPwd,string txtEmail)自动填充
        // public ActionResult AddStudent(Student student)form表中name属性要与实体类中一样
        public ActionResult AddStudent()
        {
            Student student = new Student();
            student.UserName = Request["txtUserName"];
            student.UserPwd = Request["txtUserPwd"];
            student.Email = Request["txtEmail"];
            student.RegTime = DateTime.Now;
            itcastdbEntities db = new itcastdbEntities();
            db.Student.Add(student);
            if (db.SaveChanges() > 0)
            {
                //      return Content("添加成功!!!");
                //      return Content("ok");
                //return RedirectToAction("Index","Student");//列表页  Student控制器下的Index方法
                return Redirect("/Student/Index/");
            }
            else
            {
                return Content("添加失败!!!");
            }
        }

    }
}

@{
    ViewBag.Title = "Register";
}
<h2>注册用户</h2>
<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script  type="text/javascript">
    $(function () {
        $("#btnAdd").click(function () {
            var pars = $("#form1").serializeArray();
            $.post("/Home/AddStudent", "pars", function (data) {
                if (data == "ok") {
                    alert("添加成功!!");
                }
            });
        });
    });
</script>

<div>
    <form method="post" action="/Home/Register" id="form1">
        用户名:<input type="text" name="UserName" id="UserName" /><br/>
        密&ensp;&ensp;码:<input type="password" name="UserPwd" id="Userpwd" /><br />
        邮&ensp;&ensp;箱:<input type="text" name="Email" id="Email" /><br />
        <input type="button" value="Ajax注册" id="btnAdd" />
        <input type="submit" value="注册" />
    </form>

</div>

2.列表展示

using MvcApplication1.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;

namespace MvcApplication1.Controllers
{
    public class StudentController : Controller
    {

        // GET: Student
        public ActionResult Index()
        {
            itcastdbEntities db = new itcastdbEntities();
            var studentList = db.Student.Where<Student>(s=>true);
            StringBuilder sb = new StringBuilder();
            foreach (var student in studentList)
            {
                sb.AppendFormat("<tr><td>{0}</td><td>{1}</td><td>{2}</td><td>{3}</td><td>{4}</td></tr>", student.UserId, student.UserName, student.UserPwd, student.Email, student.RegTime.ToShortDateString());
            }
            ViewData["studentList"] = sb.ToString();
            return View();
        }
    }
}

需要注意问题:

当在Index.cshtml接收数据时。


@{
    /**/

    ViewBag.Title = "Index";
}
<style  type="text/css">
    .txt {
    align-items:center;
    }
</style>
<body>
    <div>
        <h2 align="center">列表信息</h2>
        <table align="center" border="1">
            <tr>
                <th  align="center">学号</th>
                <th align="center">姓名</th>
                <th>密码</th>
                <th>邮箱</th>
                <th>时间</th>
            </tr>
            <tr>
                @MvcHtmlString.Create(ViewData["studentList"].ToString())
                @ViewData["studentList"]

            </tr>
        </table>
    </div>
</body>

使用【 @ViewData["studentList"]】接收:

浏览器不会对数据进行渲染,右击选择【查看网页源代码】

返现"<"与“>”已经被转义了,不会起到标签的作用。因此浏览器无法渲染为表格形式。

使用【   @MvcHtmlString.Create(ViewData["studentList"].ToString())】接收:

显示正常。再次查看源代码,

3.详细信息

4.删除

   public ActionResult ShowDetail()
        {
            int id = Convert.ToInt32(Request["id"]);
            var student = db.Student.Where<Student>(s => s.UserId == id).FirstOrDefault();
            ViewData["student"] = student;
           // ViewData.Model= student;
            return View();
        }

        public ActionResult Edit(int id)
        {
            return View();
        }



        public ActionResult Delete()
        {
            int id = Convert.ToInt32(Request["id"]);
            var student = db.Student.Where<Student>(s => s.UserId == id).FirstOrDefault();
            if (student != null)
            {
                db.Entry<Student>(student).State = System.Data.Entity.EntityState.Deleted;
                if (db.SaveChanges() > 0)
                {
                    return RedirectToAction("Index");
                }
                else
                {
                    return Content("删除失败!!");
                }
            }
            else
            {
                return Content("要删除的数据不存在!!");
            }
        }

@{
    ViewBag.Title = "ShowDetail";
}

<h2>详细信息</h2>

<div>
    @{ MvcApplication1.Models.Student student = (MvcApplication1.Models.Student)ViewData["student"]; }
    <table align="center">
        <tr>
            <td>学&ensp;&ensp;号:</td>
            <td>@student.UserId</td>
            @*<td>@ViewData.Model.UserId</td>*@
        </tr>
        <tr>
            <td>用户名:</td>
            <td>@student.UserName</td>
        </tr>
        <tr>
            <td>密&ensp;&ensp;码:</td>
            <td>@student.UserPwd</td>
        </tr>
        <tr>
            <td>日&ensp;&ensp;期:</td>
            <td>@student.RegTime</td>
        </tr>
        <tr>
            <td>邮&ensp;&ensp;箱 :</td>
            <td>@student.Email</td>
        </tr>

    </table>


</div>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

挑战不可

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值