I have a model from ADO.NET, so it's Linq. I can show my tables perfectly, but now I want to add a div
where I show the details of the item with a button (the button contains the id_element
).
I decided to use JQuery and AJAX to do this. Let's resume: I have an Index view, and I have a partial view with the "details content".
I receive an ERROR from AJAX instead of success.
Here my Index View:
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<IEnumerable<AppDataGrid.Models.EMPRESA>>" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Index
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>Index</h2>
<table>
<tr>
<th>ID Centro</th>
<th>ID Elemento</th>
<th></th>
</tr>
<% foreach (var item in Model) { %>
<tr>
<td><%: item.ID_CENTRO %></td>
<td><%: item.id_elemento %></td>
<%-- <td><%: item.Fecha_Llegada_Centro %></td>
<td><%: item.Fecha_Salida_Centro %></td>--%>
<td> <%: Html.ActionLink("Detalles", "Welcome", new { id=item.id_elemento })%></td>
<td><button id="detalle" onclick="send();" value="<%:item.id_elemento%>">DETALLE</button></td>
</tr>
<tr id="result">
</tr>
<% } %>
</table>
<%-- <div id="result">
</div>--%>
<script type="text/javascript">
function send() {
document.getElementById("detalle").addEventListener("click", function (event) {
event.preventDefault()
});
var id_elem = document.getElementById("detalle").value;
updateServerText(id_elem);
}
function updateServerText(id_elem) {
document.getElementById("detalle").disabled = true;
$.ajax({
cache: false,
url: '<%= Url.Action("Welcome", "Store") %>',
type: "GET",
data: {
id: id_elem
},
contentType: "application/html",
dataType: "html",
success: function (msg) {
partialView = $(msg);
$("#result").append(partialView);
//$("#result").html(msg).show("slow");
}
,
error: function (msg) {
$("#result").html("Bad parameters!").show("slow");
}
});
}
</script>
</asp:Content>
Here is my Controller:
StoreModelDataContext context = new StoreModelDataContext();
public ActionResult Index()
{
return View(context.EMPRESA.Where);
}
public PartialViewResult Welcome(int id)
{
return PartialView(context.EMPRESA.Where(c => c.id_elemento == id)
.Select(c => c));
}
Here is the Partial View (Welcome):
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<AppDataGrid.Models.EMPRESA>" %>
<div>
<%= ViewData.Model.ID_CENTRO %>
<%= ViewData.Model.id_elemento %>
</div>
NOTE: If I do that with a MasterView there is no problem, except that I can only do it for one, not far all, so it's not a solution. I can do it if I don't use StrongTyped View but I can't access the model, so that's why I'm here asking you.
I have to say that I am using MVC 2, is that only working on later versions? EDIT: I've tested with MVC 4 and I have the same result.
Anyway, thanks for your help and sorry if you have to read a lot. Feel free to ask me anything you need.