简体   繁体   English

Knockout下拉列表数据绑定不能在ajax调用中工作

[英]Knockout dropdownlist databind is not working in ajax call

I try to bind a dropdownlist in knockout with MVC 4. Please see my code below. 我尝试使用MVC 4绑定下拉列表中的下拉列表。请参阅下面的代码。

Action 行动

    public JsonResult GetUserTypes()
    {
        using (QuestApplicationEntities db = new QuestApplicationEntities())
        {
            var usertypes = (from usertype in db.UserTypes
                             select new
                             {
                                 usertype.Id,
                                 usertype.Name
                             }).ToArray();

            return Json(usertypes, JsonRequestBehavior.AllowGet);
        }
    }

Knockout.js Knockout.js

var Register =
{
    Name: ko.observable("Ragesh"),
    Email: ko.observable().extend({ email: true }),
    Password: ko.observable(),
    UserTypes: ko.observableArray([]),

    UserType: ko.observable(),

    SaveRegistration: function () {
        //alert('saved');
        $.ajax({
            url: '/Home/RegisterUser',
            type: 'post',
            datatype: 'json',
            data: ko.toJSON(this),
            contentType: 'application/json',
            success: function () {
                alert('saved');
            }
        });
    }
};

$.ajax({
            url: '/Home/GetUserTypes',
            type: 'post',
            datatype: 'json',
            data: ko.toJSON(this),
            contentType: 'application/json',
            success: function () {
                ko.mapping.fromJS(data,Register.UserTypes);
        }
    });

    ko.applyBindings(Register);

Html HTML

  <h4>Register</h4>
 <fieldset>
<legend>New User Registration</legend>
<div class="editor-label">
    Name 
</div>
<div class="editor-field">
    <input data-bind="value:Name" />
</div>
<div class="editor-label">
    Email 
</div>
<div class="editor-field">
    <input data-bind="value:Email" />
</div>
<div class="editor-label">
    User Type 
</div>
<div class="editor-field">
    <select data-bind="options: UserTypes, value: UserType, optionsCaption: 'Select User Type...'">
    </select>
</div>
<p>
    <button type="button" data-bind="click:SaveRegistration">Register</button>
</p>
</fieldset>
<script src="~/Scripts/knockout-2.2.1.js"></script>
<script src="~/Scripts/knockout.validation.js"></script>
<script src="~/Scripts/App/Register.js"></script>

But the GetUserTypes action is not fired. 但是不会触发GetUserTypes操作。

And there is another error show in the firebug . 在萤火虫中还有另一个错误显示。

在此输入图像描述

Please help. 请帮忙。

I think the list is not getting to data variable. 我认为列表没有达到数据变量。 so the data variable must be null. 所以数据变量必须为null。

try this. 尝试这个。 add data parameter in the success function 在success函数中添加data参数

$.ajax({
        url: '/Home/GetUserTypes',
        type: 'post',
        datatype: 'json',
        data: ko.toJSON(this),
        contentType: 'application/json',
        success: function (data) {
            ko.mapping.fromJS(data,Register.UserTypes);
    }
});

Your action GetUserTypes doesn't expected any parameters, but you pass viewModel object: 您的操作GetUserTypes不期望任何参数,但您传递viewModel对象:

....
$.ajax({
    url: '/Home/GetUserTypes',
     type: 'post',
     datatype: 'json',
     data: ko.toJSON(this),
     ...

Try to remove this property from ajax call. 尝试从ajax调用中删除此属性。

About error in FireBug , just include jQuery script in your page. 关于FireBug中的错误 ,只需在页面中包含jQuery脚本即可。

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

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