Asp.net MVC + Vue.js

Posted ZHANGKAIXUAN

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Asp.net MVC + Vue.js相关的知识,希望对你有一定的参考价值。

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title></title>
    @*基库*@
    <script src="https://unpkg.com/vue/dist/vue.js"></script>
    @*ajax支持*@
    <script src="http://cdn.bootcss.com/vue-resource/1.0.3/vue-resource.js"></script> 
</head>
<body>
    <div id="demo">
        <table border="0">
            <tr class="textCenter">
                <td style="width: 100px;">姓名</td>
                <td style="width: 60px;">年龄</td>
                <td style="width: 200px;">课程</td>
            </tr>
            <tr v-for="x in studentList">
                <td>
                    {{x.Name}}
                </td>
                <td>
                    {{x.Age}}
                </td>
                <td>
                    <div v-for="cc in x.Courses">{{cc.Name}}</div>
                </td>
                <td>
                    <input type="button" v-on:click="DeleteStudent(x)" value="删除" />
                </td>
            </tr>
        </table>
        <form name="myForm">
            <table>
                <tr>
                    <td style="width: 50px;">姓名:</td>
                    <td>
                        <input type="text" v-model="newStudent.Name" />
                    </td>
                </tr>
                <tr>
                    <td style="width: 50px;">年龄:</td>
                    <td>
                        <input type="number" v-model="newStudent.Age" />
                    </td>
                </tr>
                <tr>
                    <table>
                        <tr v-for="(x,index) in newStudent.Courses"><td style="width: 50px;">课程{{index+1}}</td><td><input type="text" v-model="x.Name" /></td></tr>
                    </table>
                </tr>
                <tr>
                    <td colspan="2" style="text-align: right;">
                        <input type="button" v-on:click="addCourses()" value="添加课程" />
                        <input type="submit" v-on:click="addStudent()" value="添加" />
                    </td>
                </tr>
            </table>
        </form>
    </div>
    <script type="text/javascript">

        var vm = new Vue({
            el: "#demo",
            data: {
                studentList: [],
                newStudent: { Name: ‘‘, Age: ‘‘, Courses: [] }
            },
            methods: {
                GetAllStudent: function () {
                    var _self = this;
                    _self.studentList = [];
                    this.$http.get("/home/GetAllStudent").then(function (res) {
                        for (var i = 0; i < res.body.length; i++) {
                            _self.studentList.push(res.body[i]);
                        }
                    });
                },
                DeleteStudent: function (student) {
                    var _self = this;
                    _self.$http.post("/Home/DeleteStudent", { name: student.Name }).then(function (res) {
                        if (res.body.Code == 1) {
                            _self.GetAllStudent();
                        }
                        else {
                            alert(response.body.Msg);
                        }
                    });
                },
                addCourses: function () {
                    this.newStudent.Courses.push({ Name: "" });
                },
                addStudent: function () { 
                    var _self = this;
                    _self.$http.post("/Home/AddStudent", _self.newStudent).then(function (res) {
                        if (res.body.Code == 1) {
                            _self.GetAllStudent();
                        }
                        else {
                            alert(response.body.Msg);
                        }
                    });
                    return false;
                }
            }
        });
        vm.GetAllStudent();
    </script>
</body>
</html>

 后台代码

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

namespace Test.VueJS.Controllers
{
    /// <summary>
    /// coder 释迦苦僧
    /// </summary>
    public class HomeController : Controller
    {
        /// <summary>
        /// 静态
        /// </summary>
        public static List<Student> students = new List<Student>();
        //
        // GET: /Home/
        [HttpGet]
        public ActionResult Index()
        {
            return View();
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="student"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult AddStudent(Student student)
        {
            if (student == null)
            {
                return Json(new ReturnCode(-1, "error"), JsonRequestBehavior.AllowGet);
            }
            students.Add(student);
            return Json(new ReturnCode(1, "success"), JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 获取所有
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public JsonResult GetAllStudent()
        { 
            return Json(students, JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public JsonResult DeleteStudent(string name)
        {
            var student = students.FirstOrDefault(p => p.Name == name);
            if (student != null)
            {
                students.Remove(student);
            }
            return Json(new ReturnCode(1, "success"), JsonRequestBehavior.AllowGet);
        }
    }
    /// <summary>
    /// 学生
    /// </summary>
    public class Student
    {
        /// <summary>
        /// 姓名
        /// </summary>
        public string Name { get; set; }
        /// <summary>
        /// 年龄
        /// </summary>
        public int Age { get; set; }
        /// <summary>
        /// 拥有的课程
        /// </summary>
        public List<Course> Courses
        {
            get;
            set;
        }
    }
    /// <summary>
    /// 课程
    /// </summary>
    public class Course
    {
        public string Name { get; set; }
    }
    /// <summary>
    /// 处理结果返回值
    /// </summary>
    public class ReturnCode
    {
        public ReturnCode(int code, string msg)
        {
            this.Code = code;
            this.Msg = msg;
        }
        public int Code { get; set; }
        public string Msg { get; set; }
    }
}

以上是关于Asp.net MVC + Vue.js的主要内容,如果未能解决你的问题,请参考以下文章

ASP.NET 5 MVC 6 CORS vue.js 请求不允许

ASP.net MVC 代码片段问题中的 Jqgrid 实现

ASP.NET MVC+Vue.js实现联系人管理

如何在 asp.net mvc 部分视图中使用 Vue 组件 (*.vue)

在ASP.NET MVC的Action中直接接受客户端发送过来的HTML内容片段

有没有办法在使用 Asp.Net MVC ActionLink、RedirectToAction 等时包含片段标识符?