Note that there are some explanatory texts on larger screens.

plurals
  1. POReturn data to jquery/ajax call from entity framework object in asp.net
    text
    copied!<p>I am trying to get <code>List&lt;Product&gt;</code> from Entity Framework using jquery/ajax call. Here's the ajax request.</p> <pre><code>$.ajax({ type: "POST", url: "searchService.asmx/search", data: "{'lookup':'itemName'}", contentType: "application/json; charset=utf-8", dataType: "json", success: function (products) { // Just printing the result for now. console.log(products); } }); </code></pre> <p>Search method in web service is implemented as:</p> <pre><code>[WebMethod] public List&lt;Product&gt; search(string lookup) { using (eCommerceDBEntities context = new eCommerceDBEntities()) { List&lt;Product&gt; pr = context.Products.Where(i =&gt; i.ProductName.Contains(lookup)).ToList(); return pr; } } </code></pre> <p>For some reason I'm getting a 500 (Internal server error) in the console log.</p> <p>To my surprise, the following code works:</p> <pre><code>[WebMethod] public List&lt;Product&gt; search(string lookup) { using (eCommerceDBEntities context = new eCommerceDBEntities()) { List&lt;Product&gt; pr = context.Products.Where(i =&gt; i.ProductName.Contains(lookup)).ToList(); return new List&lt;Product&gt; {new Product(){ ProductName="abc", ProductPrice=123}, new Product(){ ProductName ="xyz", ProductPrice=321 } }; } } </code></pre> <p>Above hardcoded <code>List&lt;Product&gt;</code> returns a nice object, but the one where I query from database doesn't. I would have assumed it's the same thing whether it's hardcoded or queried form DB.</p> <p>I am trying this approach for the first time so I might be doing something horribly wrong here. How do I get the data from DB?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload