简体   繁体   English

如何使用敲除和ASP.NET MVC将多个视图模型绑定到控制器

[英]How to bind multiple view models to controller using knockout and asp.net mvc

Iam using knockout , asp.net MVC , entity framework and I would like to know how I can go about saving all the info from my sub view models into one controller method 我使用剔除,asp.net MVC,实体框架,我想知道如何将子视图模型中的所有信息保存到一个控制器方法中

In my knockout I have multiple viewmodels which contain different info which is related to each other such that when I pass to the controller I have to pass info pertaining to all view models, such that when I save upon clciking the save button all the information is saved to the various "different tables at simultaneously I know how to pass one view model but I would like to know how to pass two models in one statement to the controller "two different methods in controller" my current structure is like this 在我的淘汰赛中,我有多个视图模型,其中包含彼此相关的不同信息,因此,当我传递给控制器​​时,我必须传递与所有视图模型有关的信息,以便在单击保存按钮时保存所有信息同时保存到各种“不同表”中,我知道如何传递一个视图模型,但我想知道如何在一个语句中将两个模型传递给控制器​​“控制器中的两种不同方法”,我当前的结构是这样的

function FullEmployeeProfile ()
 var fep = this 
{
      fep.ei.Name = ko.observable("");
      fep.ei.ID = ko.observable("");
      fep.ei.Gender = ko.observable("");
      fep.ei.address = ko.observable("") ; 
      fep.eh.CompanyName = ko.observable();
      fep.eh.Designation = ko.observable();
      fep.eh.StartDate = ko.observable();
      fep.eh.EndDate = ko.observable();

}

function employeeHistory() {
        var eh = this 

         var self = this;
        eh.CompanyName = ko.observable();
        eh.Designation = ko.observable();
        eh.StartDate = ko.observable();
        eh.EndDate = ko.observable();
}

function employeeEducation() {
    var ee = this;


        ee.Degree = ko.observable();
        ee.YearOfPassing = ko.observable();
        ee.Percentage = ko.observable();
        ee.Institute = ko.observable()


    ee.fullEmployeeDetails = new FullEmployeeProfile();

    ee.saveEmployeeDetails = function() {

        $.when(postSecureData("/api/EmployeeProfile/", ko.toJSON(ee.fullEmployeeDetails)))
        .done(function (empProfile) {
            if (response.ResponseRequired == false) {
            document.getElementById("save-empDetails-btn").innerHTML = "Saving...";
            setTimeout(function () { document.getElementById("save-empDetails-btn").innerHTML = "Save" }, 2500);
            $.msgGrowl({
                type: 'info',
                title: 'Employee information saved',
                text: 'Employee information details succesfully saved',
                position: 'bottom-right',
                lifetime: 3000
        }); }

        });
    };




}
function EmployeeInfo() {
    var ei = this;

     var ei = this;
        ei.Name = ko.observable("");
        ei.ID = ko.observable("");
        ei.Gender = ko.observable("");
        ei.address = ko.observable("") ;     
    }

As suggested by DavidG, an encapsulating model would be the approach to take. 正如DavidG所建议的那样,将采用封装模型。 It seems like you're confusing the idea of view models and domain/data models, or at the very least your concept of view models is a little confused. 似乎您在混淆视图模型和域/数据模型的概念,或者至少,您对视图模型的概念有些困惑。

Your view model should be exactly, literally, what it sounds like. 从字面上看,您的视图模型应该完全像听起来一样。 It is a model that contains all the information you need for the specific view. 它是一个模型,其中包含特定视图所需的所有信息。 If you need to contain multiple views inside your main view, you also need to contain multiple view models inside your main view model. 如果需要在主视图中包含多个视图,则还需要在主视图模型中包含多个视图模型。 This is common practice. 这是惯例。

For example: 例如:

function EmployeeProfileAndHistory() {
    var profile = new FullEmployeeProfile();
    var history = new employeeHistory();
}

Pass this to your API. 将此传递给您的API。 (code could be wrong as not overly familiar with knockout, but the gist is the same.) (代码可能不正确,因为对淘汰赛不太熟悉,但是要旨是一样的。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM