C# MVC 传统方法实现功能

Index.cshtml

@{
    Layout = null;
}
<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
    <script src="~/Scripts/jquery-1.10.2.min.js"></script>
    <script type="text/javascript">
        function DoDelete(StudentID) {
            $.post('@Url.Action("DoDeleteStudent", "Home")', { StudentID: StudentID }, function (result) {
                alert(result.Msg);
            });
        }
    </script>
</head>
<body>
    <div>
        <a href="@Url.Action("AddStudent","Home")">添加</a>
        <table>
            <tr>
                <th>编号</th>
                <th>名称</th>
            </tr>
            @*遍历学校信息*@
            @foreach (var school in Model.Schools)
            {
                <tr>
                    <td>@school.SchoolID</td>
                    <td>@school.SchoolName</td>
                </tr>
            }
        </table>
        <table>
            <tr>
                <th>编号</th>
                <th>姓名</th>
                <th>操作</th>
            </tr>
            @*遍历学生信息*@
            @foreach (var student in Model.Students)
            {
                <tr>
                    <td>@student.StudentID</td>
                    <td>@student.StudentName</td>
                    <td><a href="javascript:void(0);" οnclick="DoDelete(@student.StudentID)">删除</a></td>
                </tr>
            }
        </table>
    </div>
</body>
</html>

AddStudent.cshtml

@model WebApplication9.Controllers.Student
@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>AddStudent</title>
    <!--验证必须添加js-->
    <script src="~/Scripts/jquery-1.10.2.min.js"></script>
    <script src="~/Scripts/jquery.validate.min.js"></script>
    <script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
</head>
<body>
    <div>
        @using (Html.BeginForm("DoAddStudent", "Home", FormMethod.Post, new { @class = "MyForm" }))
        {
            @Html.ValidationSummary(true)
            <table>
                <tr>
                    <td>@Html.LabelFor(s => s.StudentID, new { @class = "MyLabel" })</td>
                    <td>
                        @Html.TextBoxFor(s => s.StudentID, new { @class = "MyTextBox" })
                        @Html.ValidationMessageFor(s=>s.StudentID)
                    </td>
                </tr>
                <tr>
                    <td>@Html.LabelFor(s => s.StudentName, new { @class = "MyLabel" })</td>
                    <td>
                        @Html.TextBoxFor(s => s.StudentName, new { @class = "MyTextBox" })
                        @Html.ValidationMessageFor(s => s.StudentName)
                    </td>
                </tr>
                <tr>
                    <td colspan="2">
                        <input type="submit" value="提交" />
                    </td>
                </tr>
            </table>
        }
    </div>
</body>
</html>

HomeController

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace WebApplication9.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            Test test = GetTest();
            return View(test);//MVC返回多个对象
        }
        public ActionResult AddStudent()
        {
            return View();
        }
        public ActionResult DoAddStudent(Student student)
        {
            Test test = GetTest();
            return View("Index", test);
        }
        public JsonResult DoDeleteStudent(int StudentID)
        {
            Message message = new Message { Success = 1, Msg = "删除成功" };
            return Json(message);
        }
        private Test GetTest()
        {
            List<Student> Students = new List<Student>();
            Students.Add(new Student { StudentID = 1, StudentName = "张三" });
            Students.Add(new Student { StudentID = 2, StudentName = "李四" });
            Students.Add(new Student { StudentID = 3, StudentName = "王五" });
            List<School> Schools = new List<School>();
            Schools.Add(new School { SchoolID = 1, SchoolName = "清华大学" });
            Schools.Add(new School { SchoolID = 2, SchoolName = "北京大学" });
            Schools.Add(new School { SchoolID = 3, SchoolName = "浙江大学" });
            Test test = new Test { Schools = Schools, Students = Students };
            return test;
        }
    }
    #region 测试对象
    public class Message
    {
        public int Success { get; set; }
        public string Msg { get; set; }
    }
    public class Test
    {
        public List<School> Schools { get; set; }
        public List<Student> Students { get; set; }
    }
    public class Student
    {
        //显示和必填字段
        [DisplayName("学号")]
        [Required(ErrorMessage = "请填写学号")]
        public int StudentID { get; set; }
        [DisplayName("学生姓名")]
        [Required(ErrorMessage = "请填写学生姓名")]
        public string StudentName { get; set; }
    }
    public class School
    {
        public int SchoolID { get; set; }
        public string SchoolName { get; set; }
    }
    #endregion
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在C# MVC项目中实现拖拽式添加数据,您需要使用JavaScript库来处理拖拽操作,并使用Ajax将数据发送到服务器以保存。下面是一个基本的实现步骤: 1. 在MVC视图中添加一个可拖拽的元素,例如一个div: ```html <div draggable="true" ondragstart="drag(event)">数据</div> ``` 2. 使用JavaScript编写drag函数,将数据放入拖拽数据传输对象中: ```javascript function drag(event) { event.dataTransfer.setData("text", event.target.textContent); } ``` 3. 在接受数据的MVC视图中添加一个可放置的元素,例如一个div: ```html <div id="dropzone" ondrop="drop(event)" ondragover="allowDrop(event)"></div> ``` 4. 使用JavaScript编写allowDrop函数和drop函数,将数据从拖拽数据传输对象中提取出来,并使用Ajax将数据发送到服务器: ```javascript function allowDrop(event) { event.preventDefault(); } function drop(event) { event.preventDefault(); var data = event.dataTransfer.getData("text"); $.ajax({ type: "POST", url: "/Controller/AddData", data: { data: data }, success: function (result) { // 处理添加成功后的响应 }, error: function (result) { // 处理添加失败后的响应 } }); } ``` 5. 在MVC Controller中添加AddData方法,处理接收到的数据并将其存储到数据库中: ```csharp [HttpPost] public ActionResult AddData(string data) { // 处理接收到的数据并将其存储到数据库中 return Json(new { success = true }); } ``` 以上是一个基本的实现步骤,您可以根据具体需求进行修改和扩展。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值