0

I have /Views/Movies/Index.cshtml with

<input type="button" id="getmoviex" value="Get moviex" />
<ul id="moviex_list"/>
 <p>
        Title: @Html.TextBox("SearchTitle") <br />
    </p>

I have /Controllers/MoviesController.cs with

    [AcceptVerbs(HttpVerbs.Get)]
    public ActionResult moviex(string SearchGenre, string SearchTitle, string SearchActor)
    {
        var db = new CinemaContext();
        db.Configuration.ProxyCreationEnabled = false;
        var Movie = from m in db.Movie
                    select m;
        if (!String.IsNullOrEmpty(SearchTitle))
        {
            Movie = Movie.Where(s => s.Title.Contains(SearchTitle));
        }
         return Json(db.Movie.ToList(), JsonRequestBehavior.AllowGet);
    }

I have Javascript.js with

$(document).ready(function () {
    $('#getmoviex').click(function () {
        $.getJSON("/Movies", null, getmoviex);
    });
});

Have I correctly written /Movies? Or this should be /Views/Movies?

function getmoviex(moviex) {
    $("#moviex_list").text("");
    $.each(moviex, function (i) {
        $("#moviex_list").append("<li>" + this + "</li>");
    });
}

How can I display info or list info from my query? Or view some output with error?

2 Answers 2

2

First make sure you button click does not trigger a request to server. Preventing default behavior is a standard way of doing it:

$('#getmoviex').click(function (event) {
    $.getJSON("/Movies", null, getmoviex);
    event.preventDefault();
});

As for the URL, it should not be to view, but to action instead. Your action is moviex and controller is Movies, so

$.getJSON("/Movies/moviex", null, getmoviex);

The rest looks fine, so that should do it.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks. I try $(document).ready(function () { $('#getmoviex').click(function (event) { $.getJSON("/Movies/moviex", null, getmoviex); event.preventDefault(); }); }); //But This does not work
How to see them? I would try.
OK, i start it directly localhost:27161/Movies/moviex and get This request has been blocked because sensitive information could be disclosed to third party web sites when this is used in a GET request. To allow GET requests, set JsonRequestBehavior to AllowGet.
I remade return Json(db.Movie.ToList(), JsonRequestBehavior.AllowGet); A circular reference was detected while serializing an object of type 'System.Data.Entity.DynamicProxies.Movie_B1FE93473F6D1D9C6D1‌​FB5C5C23CAD32C5A4BE8‌​E694908458170AF6743A‌​A96B6'. –
var db = new CinemaContext(); db.Configuration.ProxyCreationEnabled = false; var Movie = from m in db.Movie select m;
1

you need to pass your arguments as well in url (GET). Something like this could work:

$('#getmoviex').click(function(event) {
  event.preventDefault();
  $.getJSON("/Movies/moviex?SearchGenre=yuorgenre&SearchTitle=Cal&SearchActor=youractor", function(moviex) {
      var lis;
      //please check the console
      console.log(moviex);
      $.each(moviex, function(b) {
          lis += "<li id='" + b.Id + "'>" + b.Title + "</li>");
      }); document.getElementById("moviex_list").innerHTML += lis;
  });
});

To avoid circular reference in Serializing you may use:

if (String.IsNullOrEmpty(SearchTitle)) {
  return View("Error");
}

var db = new CinemaContext();
var Movie = (from m in db.Movie
            Where m.Title.Contains(SearchTitle)
            select new {
             Id = m.MovieID,
             Title = m.Title // can add more properties
            }).ToList();

return Json(Movie, JsonRequestBehavior.AllowGet);

17 Comments

i already wrote query for Movies and i created moviex for json.
On which part it did not work? Was your moviex method in Movies controller triggered? - try to set breakpoint inside method or try to Run your project then in browser Location bar edit to something like localhost:xxxx/Movies/moviex?SearchGenre=yuorgenre&SearchTitle=a&SearchActor=youractor and see what it return
OK, i start it directly localhost:27161/Movies/moviex and get This request has been blocked because sensitive information could be disclosed to third party web sites when this is used in a GET request. To allow GET requests, set JsonRequestBehavior to AllowGet.
I remade return Json(db.Movie.ToList(), JsonRequestBehavior.AllowGet);
A circular reference was detected while serializing an object of type 'System.Data.Entity.DynamicProxies.Movie_B1FE93473F6D1D9C6D1FB5C5C23CAD32C5A4BE8E694908458170AF6743AA96B6'.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.