4

I am using knockoutmvc to bind controls.(http://knockoutmvc.com/)

I am getting following error while clicking on button.

Following is my Razor view implementation :

@using PerpetuumSoft.Knockout

@model OpManWeb.ViewModel.ManageAddressVM

@{
    var ko = Html.CreateKnockoutContext();
}

@{
    ViewBag.Title = "Manage Addresses";
}

@Scripts.Render("~/customjs")
@Scripts.Render("~/knockout")
<div class="container-fluid zeropadding" style="margin-top:15px; width:100%; font-size:13px;">

    <div class="row">
        <div class="col-md-3 col-lg-3 clmargin">
            <div class="form-group  col-md-4 zeropadding div1adjustments">
                @Html.LabelFor(m => m.IndexNo, new { @class = "fieldtext" })
            </div>
            <div class=" form-group col-md-8 div2adjustments ">
                @ko.Html.TextBox(m => m.IndexNo, new { @class = "form-control input-sm fieldtextinput " })
                <input type="reset" class="btncross" data-toggle="tooltip" title="Click to clear" value="X" />
                <button type="button" class="btn btn-search searchbtn" data-toggle="tooltip" data-placement="bottom"
                        title="Search customer as per Index No">
                    <i class="fa fa-search"></i>
                </button>

            </div>
        </div>
        <div class="col-md-2">
                @ko.Html.Button("Add Address", "AddAddress", "Customer", null, new { @class = "btn btn-large btn-primary" })
        </div>
    </div>

</div>

Following is the code for CustomerController File

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ServiceStack.Mvc;
using OpManWeb.Models;
using PerpetuumSoft.Knockout;
using OpManWeb.ViewModel;
using MvcJqGrid;

namespace OpManWeb.Controllers
{
    public class CustomerController : KnockoutController
    {
        public ActionResult AddAddress()
        {
            AddressVM addressVM = new AddressVM();
            addressVM.selectmode = Mode.Add;
            return View("AddEditAddress", addressVM);
        }
     }
}

Following is ther error Html Response Error Response While Clicking on Button

Aditya Raval
  • 590
  • 4
  • 20
  • Well, you are executing an ajax call on the browser but return an html page from the controller. What result are you expecting? See here for an example what you are expected to provide in your controller: http://knockoutmvc.com/ClickCounter – Andrew Savinykh Jan 28 '15 at 11:08
  • Also, what you posted does not look like Bundle.config, could you please double check. Also could you please explain why Bundle.config is significant in your case? – Andrew Savinykh Jan 28 '15 at 11:14
  • Hi zespri, Thanks for your reply. I want to use ko.Html.Button as "Submit" button which can post the data as well as can open different view, if required. So how to achieve that? Sorry for the bundleconfig confusion. The code is for Razor view file. – Aditya Raval Jan 28 '15 at 11:27
  • Sorry, this is not what ko.Html.Button is for. – Andrew Savinykh Jan 29 '15 at 02:06
  • Then can you tell me how can I achieve this? Can you provide a code snippet ? – Aditya Raval Jan 29 '15 at 05:06
  • That's hard to do with the description you provided. The code that you shown does not have a form. Your action method does not have parameters. One can only guess where you are going with this. For example you can look at this tutorial http://weblogs.asp.net/scottgu/asp-net-mvc-framework-part-4-handling-form-edit-and-post-scenarios it might be useful to you. – Andrew Savinykh Jan 29 '15 at 05:14
  • Thanks for the valuable time. I am grateful to you. – Aditya Raval Jan 29 '15 at 05:44

1 Answers1

3

While using @Ko.Html.Button we must return Json Object From Controller Method like below.

public ActionResult AddAddress()
    {

        return Json(new { Url = Url.Action("Stock", "Inventory") });
    }
Aditya Raval
  • 590
  • 4
  • 20
  • You can also use return Json(Model); – Aditya Raval Mar 02 '15 at 10:50
  • I tried this with an external url as string instead of url.action etc and it did not work. Too bad there is no time to refactor and remove kmvc for our project. http://stackoverflow.com/a/11618190/169714 – JP Hellemons Jul 01 '15 at 12:31
  • yeah http://stackoverflow.com/questions/11618042/is-there-a-reason-i-would-use-knockout-mvc-instead-of-knockout-js/11618190#11618190 It is true its really Bastard Child.:( – Aditya Raval Aug 04 '15 at 11:16