This question is related to my previous question, i want to display the details of product on the same page, for that i have used Ajax and partial View but the partial view is not rendering and only the Json Data is Updated in the div through Ajax, I dont want to display json data but only the product Desciption in text format the following code i have used, and the output is also shown, when i click on product1 it displays details regarding with product1 and when i click on product2 it shows details regarding with product2 and so on, In the controller method search i have called view, but that view is also not called. Please help to sort these problems. thanks
CategoryController.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PartialView.Models;
namespace PartialView.Controllers
{
public class CategoryController : Controller
{
dbEntities dbentity = new dbEntities();
public ActionResult Create()
{
return View(dbentity.tbl_product.ToList());
}
public JsonResult Search(int data)
{
var query = dbentity.tbl_product.Where(c => c.ProductId == data);
return Json(query,"Record Found");
//return View("ClickUC", query);
}
}
}
Seach.cshtml
@model List<PartialView.Models.tbl_product>
@foreach(var items in Model)
{
@items.ProductDesc
}
Create.cshtml
@model List<PartialView.Models.tbl_product>
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<title>Create</title>
<script src="@Url.Content("~/Scripts/jquery-1.5.1.js")" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function () {
$('.msg').click(function () {
var id = this.id;
alert(id);
$.ajax({
url: "/Category/Search",
data: { data: id },
success: function (mydata) {
alert("success");
$('#link').empty().append(mydata);
},
error: function (mydata) { alert("error"); alert(mydata); },
type: 'POST'
});
return false;
});
});
</script>
</head>
<body>
@foreach (var item in Model)
{
<a class="msg" href="#" id="@item.ProductId">@item.ProductName</a>
}
<div id="link">
</div>
</body>
</html>
Output:
View Product Details : Product 1 Product2
[{"ProductId":2,"ProductName":"Product 2","ProductDesc":"Description 2","EntityState":2,"EntityKey":{"EntitySetName":"tbl_product","EntityContainerName":"dbEntities","EntityKeyValues":[{"Key":"ProductId","Value":2}],"IsTemporary":false}}]