user3500913
user3500913

Reputation: 1

Passing Knockout Binding Parameter from View to Controller

Im trying to get started with Knockout in MVC and managed to get the normal binding/mapping working, I want to pass Model Id from View to Controller by using html.action, but it's pass a null value, Am using knockout binding. Help me in finding out the error...

My view :

// Foreach loop started  

<tbody data-bind="foreach: list">
        <tr>
            <td data-bind="text: DisplayFlatNumber, click: $parent.rowClick"></td>
            <td data-bind="text: DisplayFullName, click: $parent.rowClick"></td>
            <td data-bind="text: Flat.MaintainanceCharge, click: $parent.rowClick">0</td>
            <td data-bind="text: @Html.Action("DueAmount", "MemberDirectory", 
new { id = Model.Id })"></td> 
        </tr>
</tbody> 'Model.Id is not throwing any data to controller'
  //loop end

Upvotes: 0

Views: 1569

Answers (1)

Richard
Richard

Reputation: 30618

You can't use MVC and knockout together like that - it looks like you've got them both iterating over the same thing. You have two choices - render the data using MVC, or render it using knockout. If you want to use knockout, the easiest way is to have MVC render the data as JSON, and use this as the basis for your model.

e.g.

<script type="text/javascript">
    var model = @Html.Raw(JsonConvert.SerializeObject(Model));

    function ViewModel(data) {
        var self = this;
        self.Rows = data;// You could use ko.mapping here if you want observables
    }

    var vm = new ViewModel(model);
    ko.applyBindings(vm);
</script>

This way, you don't use any MVC html in your table. This means you can't use Html.ActionLink - you'll need to create the link yourself.

Upvotes: 2

Related Questions