I need some help. I write little app using ASP.NET MVC4 with JavaScript and Knockout and I can't send data from javascript to MVC Controller and conversely. For example, the part of JS looks like that:
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'm getting employer by ID and return it as Json:
C#
public JsonResult GetEmployer(int id)
{
var employer = unit.Repository<Employer>().GetByID(id);
return Json(employer, JsonRequestBehavior.AllowGet);
}
My View return another Controller (Home/KnockOutView). My View also gets another objects and depending what recieve, has different look:
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 great, I receive Vacancy and Employer objects in JS and show it in HTML using Knockout, but my URL all time still the same!!! But I want to change URL all time, when i'm getting Vacancy or Employer.
For example, if I want to get Employer, using method GetEmployer, URL should looks like that: ~/Home/GetEmployer?id=3
If I change this string of Code
Url.Action(GetEmployer, Home)
at url: window.location.href = /home/GetEmployer?id= + id
URL will be changed, but Controller returns me an Json object and shows it in window in Json format.
Help me, please, to change URL and get information in Controller from URL.
Thanks.