0

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.

Zaz
  • 46,476
  • 14
  • 84
  • 101
  • when you look at the console, what is the error that your call is returning? – Matt Bodily Jul 29 '16 at 13:38
  • Nothing, just: HTML1300: Navigatin complished. File Index. PD: Sorry about not answer before but i work the whole week and i was on the beach hehe. Hope you can help me. (This is not 1sr priority of the project, but i want to add this function to the project on my job.). Thanks again. – Héctor J. Orihuela Ruiz Jul 30 '16 at 19:14
  • Looking at your code I think you may be missing the partial view name. see my answer http://stackoverflow.com/questions/28613630/how-do-i-refresh-a-partial-view-every-3-seconds-in-mvc-4/28615705#28615705 here for how I handle partial views – Matt Bodily Aug 01 '16 at 14:04
  • I've answer the this post to put code, so you can check i've used your code :) Regards and Thanks!!! – Héctor J. Orihuela Ruiz Aug 02 '16 at 07:02

0 Answers0