简体   繁体   中英

Knockout set view-model field value

Model has a field, TotalCount . On button click, how I could change its value?

    <input id="Qty" type="text" style="width:25px;" 
     value="@Model.TotalCount" />

    var pageKoModel;
    var pageKoModelData = @Html.Raw(Json.Encode(Model));
    $(document).ready(function () {
        pageKoModel = new Object();
        ko.mapping.fromJS(pageKoModelData, {}, pageKoModel);
        ko.applyBindings(pageKoModel);
    });

data-bind is not present in the code. So, try replacing the input field with this code

<input id="Qty" type="text" style="width:25px;" data-bind="
value:TotalCount" />

if, still it does not work then,try with

<input id="Qty" type="text" style="width:25px;" data-bind="
value:pageKoModel.TotalCount" />

this works for me like:

<input tabindex="1" type="text" data-bind="value: $root.TotalCount" maxlength="5" 
onkeypress="return onlyNumberKey(event);" style="width:35px;" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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