I need help. I am writing a small application using ASP.NET MVC4 with JavaScript and a knockout, and I cannot send data from javascript to MVC Controller and vice versa. For example, part of JS looks like this:
Javascript
self.Employer = ko.observable(); self.AboutEmployer = function (id) { $.ajax({ Url.Action("GetEmployer", "Home") cache: false, type: 'GET', data: "{id:" + id + "}", contentType: 'application/json; charset=utf-8', dataType: "json", success: function (data) { self.Employer(data); } }).fail( function () { alert("Fail"); }); };
In ASP.NET MVC Home Controller, I get the employer by ID and return it as Json:
FROM#
public JsonResult GetEmployer(int id) { var employer = unit.Repository<Employer>().GetByID(id); return Json(employer, JsonRequestBehavior.AllowGet); }
My View returns another controller (Home / KnockOutView). My view also receives other objects and, depending on what it receives, has a different view:
HTML
... <b>About Company: </b><a href="#" data-bind="click: $root.AboutEmployer.bind($data, Vacancy().EmployerID)"> <span data-bind=" text: Vacancy().Employer"></span></a> <div data-bind="if: Vacancy"> <div id="VacancyDescription"><span data-bind="text:DescriptionShort"></span> </div> </div> <div data-bind="if: Employer"> <div data-bind="text: Employer().EmployerDescription"></div> </div>
Everything works fine, I get job and employer objects in JS and display them in HTML using Knockout, but my URL stays the same all the time !!! But I want to change the URL all the time when I get a Job or Employer. For example, if I want to get Employer using the GetEmployer method, the URL should look like this: ~ / Home / GetEmployer? Id = 3 If I change this line of code Url.Action("GetEmployer", "Home") at url: window.location.href = "/home/GetEmployer?id=" + id URL will be changed, but Controller will return an object to me Json and show it in a window in Json format. Please help me change the URL and get the information in the controller at the URL. Thanks.