本文介绍了如何在单个viewmodel中添加多个knockout功能?又如何将这个viewmodel调用到MVC视图来运行应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用ASP.NET MVC,KnockoutJS和数据库编写CRUD应用程序。当我为Create,Read,Update和Delete编写单独的viewmodel并在单独的视图中调用每个viewmodel(创建,读取,更新和删除)时,它工作正常。但是当我在一个视图模型中包含所有viewmodel然后在每个View中调用该viewmodel时。然后我既不能创建也无法编辑。

请帮帮我。我尝试了很多方法,但无法得到适当的解决方案。



我尝试过的方法:



我用Knockout编写了这个函数:



I am writing CRUD application using ASP.NET MVC,KnockoutJS and database. When i write separate viewmodel for Create, Read, Update and Delete.and calling each viewmodel in separate view (Create, Read, Update and Delete) then It's working fine. But when i include all viewmodel in one single viewmodel then calling that viewmodel in every View. Then i am neither able to create not able to edit.
Please help me with this. I tried so many ways but could not get proper solution.

What I have tried:

I have written this function using Knockout:

$(function () {
        ko.applyBindings(modelView);
    });

    var parsedSelectedCourse = $.parseJSON(selectedCourse);

    var modelView = {

        Read: {
            Courses: ko.observableArray([]),
            viewCourses: function () {
                var thisObj = this;
                try {
                    $.ajax({
                        url: '/Home/ListCourses',
                        type: 'GET',
                        dataType: 'json',
                        contentType: 'application/json',
                        success: function (data) {
                            thisObj.Courses(data); //Here we are assigning values to KO Observable array
                        },
                        error: function (err) {
                            alert(err.status + " : " + err.statusText);
                        }
                    });
                } catch (e) {
                    window.location.href = '/Home/Read/';
                }
            }
        },

        Create: {
            CourseName: ko.observable(),
            CourseDescription: ko.observable(),
            createCourse: function () {
                try {
                    $.ajax({
                        url: '/Home/Create',
                        type: 'post',
                        dataType: 'json',
                        data: ko.toJSON(this), //Here the data wil be converted to JSON
                        contentType: 'application/json',
                        success: successCallback,
                        error: errorCallback
                    });
                } catch (e) {
                    window.location.href = '/Home/Read/';
                }
            }
        },

        Update: {
            CourseID: ko.observable(parsedSelectedCourse.CourseID),
            CourseName: ko.observable(parsedSelectedCourse.CourseName),
            CourseDescription: ko.observable(parsedSelectedCourse.CourseDescription),
            updateCourse: function () {
                try {
                    $.ajax({
                        url: '/Home/Update',
                        type: 'POST',
                        dataType: 'json',
                        data: ko.toJSON(this),
                        contentType: 'application/json',
                        success: successCallback,
                        error: errorCallback
                    });
                } catch (e) {
                    window.location.href = '/Home/Read/';
                }
            }
        },

        Delete: {
            CourseID: ko.observable(parsedSelectedCourse.CourseID),
            CourseName: ko.observable(parsedSelectedCourse.CourseName),
            CourseDescription: ko.observable(parsedSelectedCourse.CourseDescription),
            deleteCourse: function () {
                try {
                    $.ajax({
                        url: '/Home/Delete',
                        type: 'POST',
                        dataType: 'json',
                        data: ko.toJSON(this),
                        contentType: 'application/json',
                        success: successCallback,
                        error: errorCallback
                    });
                } catch (e) {
                    window.location.href = '/Home/Read/';
                }
            }
        }
    };

    function successCallback(data) {
        window.location.href = '/Home/Read/';
    }
    function errorCallback(err) {
        window.location.href = '/Home/Read/';
    }





请帮我一个主意。谢谢



Please help me with an idea. Thanks

推荐答案




这篇关于如何在单个viewmodel中添加多个knockout功能?又如何将这个viewmodel调用到MVC视图来运行应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 15:26