我如何通过从View一个ID的视图模型作为GET函数的参数? [英] How do I pass an ID from View to the ViewModel as a parameter for GET function?

查看:238
本文介绍了我如何通过从View一个ID的视图模型作为GET函数的参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建使用MVC,knockoutJS,网络API,引导等项目,使用的数据库是MSSQL Server 2012的这一切都工作得很好,控制器已正确创建CRUD操作。从数据库中的数据显示在UI网格表,每一行的点击,并打开了其中显示有关确切元素的数据的模式。我遇到的问题是无法通过的行的一定值,在此情况下的ID,以视图模型作为用于获得模态的单个结果的参数。我可以做手工,并把一些价值在视图模型,数据将显示,但我无法从View发送的值。

I'm creating a project using MVC, knockoutJS, Web API, Bootstrap and so forth, the database in use is MSSQL Server 2012. It's all working very well, the controllers have properly created CRUD operations. The data from DB is show in a grid table in the UI, and every row is clickable, and opens up a modal in which the data about that exact element is shown. The problem I'm experiencing is the inability to pass a certain value of the row, in this case an ID, to ViewModel as a parameter for getting a single result in modal. I can do it manually, and put some value in the ViewModel, and the data will show, but I'm unable to send the value from the View.

这里的code为视图模型:

Here's the code for ViewModel:

 var regionsModel = {
    regionId: ko.observable(),
    companyId: ko.observable(),
    name: ko.observable(),
    companyName: ko.observable()    
};

var regionsListModel = {
    regions: ko.observable()
};

function getRegions() {
    get(apiUrl + "Regions/GetRegions", {}, function (data) {
        regionsListModel.regions(data);
    });
}

function getRegion() {
    get(apiUrl + "Regions/GetRegion", { aiId: regionsModel.regionId() }, function (data) {
        regionsModel.regionId(data.RegionID);
        regionsModel.companyName(data.CompanyName);
        regionsModel.companyId(data.CompanyID);
        regionsModel.name(data.Name);
    });
}

function viewRegion() {
    $("#ViewRegionModal").modal('show');
    //regionsModel.regionId($('#ViewRegion').val());
    getRegion();
    return false;
}

这里的code的视图:

Here's the code for the View:

<table class="table table-striped table-bordered responsive" id="dtable">
                            <thead>
                                <tr>
                                    <th style="width: 20px;">ID</th>
                                    <th>Region Name</th>
                                    <th>Company Name</th>
                                </tr>
                            </thead>
                            <tbody data-bind="foreach: regionsListModel.regions">
                                <tr id="ViewRegion" data-toggle="modal" data-bind="click: viewRegion, value: RegionID">
                                    <td data-bind="text: RegionID"></td>
                                    <td data-bind="text: Name"></td>
                                    <td data-bind="text: CompanyName"></td>
                                </tr>
                            </tbody>
                        </table>

AIID参数是在控制器的GetRegion方法。

aiId parameter is for the GetRegion method in Controller.

这是code的视图中显示的数据有一定的元素:

This is the code for the View in which shows the data for a certain element:

<table class="table table-striped" data-bind="with: regionsModel">
        <tbody>
            <tr>
                <th>Region ID:</th>
                <td><span data-bind="text: regionsModel.regionId"></span></td>
            </tr>
            <tr>
                <th>Region Name:</th>
                <td><span data-bind="text: regionsModel.name"></span></td>
            </tr>
            <tr>
                <th>Company Name:</th>
                <td><span data-bind="text: regionsModel.companyName"></span></td>
            </tr>
        </tbody>
    </table>

任何帮助将是AP preciated!

Any help would be appreciated!

推荐答案

淘汰赛将当前绑定的对象作为第一个参数时,它调用的事件处理程序。
第二个参数是事件对象。

Knockout adds the current bound object as first argument when it calls the event handler. The second argument is the event object.

所以,你需要做的唯一事情是一个参数添加到viewRegion功能。

So the only thing you need to do is add a parameter to the viewRegion function.

function viewRegion(region) {
    var regionID = region.RegionID;
    // get data
    return false;
}

我希望它能帮助。

I hope it helps.

这篇关于我如何通过从View一个ID的视图模型作为GET函数的参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆