1

I am new to mvc so here is my problem I have created a jquery-datatble with a action link named "detalles". When a user clicks on it I what to be able to see the details of the item selected in the jquery-datatble. Once the user has clicked on it. It opens a new tab that has a form with fields and also an other jquery-datatble. Basicly my idea would be to only have one return in the controler that return the view and th json.

Here is my Action result for i "detalles"

public ActionResult Detalles(int? id)
{
    proveedorContext proveedorContext = new proveedorContext();

    if (id == null)
    {

    }
    proveedorModel proveedor = proveedorContext.GetAllProveedor().Find(obj => obj.proveedor_id == id);
     //When i am here my model is not empty 
    loadProveedorContactoTable(proveedor);
    if (proveedor == null)
    {

    }
    return View(proveedor);
}

But unfortunately after words my program calls "loadProveedorContactoTable" again and then my model is completely null

    public ActionResult loadProveedorContactoTable(proveedorModel model)
    {
        try
        {
            var proveedorsContactoData = model.contactos.OrderBy(a => a.nombre_contacto).ToList();

            return Json(new { data = proveedorsContactoData }, JsonRequestBehavior.AllowGet);
        }
        catch
        {
            return View();
        }

    }

Here is my proveedorModel

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace erp_colombia.Models
{
    public class proveedorModel
    {
        public int proveedor_id { get; set; }
        public string nombre { get; set; }
        public string direccion { get; set; }
        public string codigo_postal { get; set; }
        public string cuidad { get; set; }
        public string pais { get; set; }
        public string pagina_internet { get; set; }
        public int compras_cantidad { get; set; }
        public int componente_cantidad { get; set; }
        public int eliminado { get; set; }

        public List<ContactoModel> contactos { get; set; }

    }
}

And here is how I read the data for contactos and proveedor()

  public List<proveedorModel> GetAllProveedor()
        {
            List<proveedorModel> list = new List<proveedorModel>();

            using (MySqlConnection conn = GetConnection())
            {
                conn.Open();
                MySqlCommand cmd = new MySqlCommand("SELECT * FROM erp_proveedores",conn);


                using (var reader = cmd.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        list.Add(new proveedorModel()
                        {
                            proveedor_id = int.Parse(reader.GetString(reader.GetOrdinal("proveedor_id"))),
                            nombre = reader.GetString(reader.GetOrdinal("nombre")),
                            direccion = reader.GetString(reader.GetOrdinal("direccion")),
                            codigo_postal = reader.GetString(reader.GetOrdinal("codigo_postal")),
                            cuidad = reader.GetString(reader.GetOrdinal("cuidad")),
                            pais = reader.GetString(reader.GetOrdinal("pais")),
                            pagina_internet = reader.GetString(reader.GetOrdinal("pagina_internet")),
                            eliminado = int.Parse(reader.GetString(reader.GetOrdinal("eliminado"))),
                            contactos = new proveedorContactoContext().GetAllProveedorContactos(int.Parse(reader.GetString(reader.GetOrdinal("proveedor_id"))))
                        });
                    }
                }

                return list;
            }
        }     

What can I do to fix this problem thank you for your help. I am thinking about making one return that would return the json and the view how could I so thar

J.C
  • 632
  • 1
  • 10
  • 41
  • 2
    You should use `FirstOrDefault()` instead of `Find()` – Nguyễn Văn Phong Jan 13 '20 at 04:27
  • 1
    Could you give us your `proveedorModel` and `contactos` class ? – Nguyễn Văn Phong Jan 13 '20 at 04:30
  • 1
    Is there by any chance you have multiple calls to `loadProveedorContactoTable()` . – vikscool Jan 13 '20 at 05:15
  • 1
    You should not call an action result from another action result. Return the collection form a service class or repository class, then in your action result you cast to json in the return. – Vince Jan 13 '20 at 10:24
  • @vikscool I have not the reason why I called loadProveedorContactoTable in detalles was to test to see if my proveedor would be null. – J.C Jan 13 '20 at 11:21
  • @Vince yes you are correct the reason why I called anther action result was to be able to determine if proveedor would be null. – J.C Jan 13 '20 at 11:23
  • @vikschool I have change my title since it explains better my problem and what I have tried to do to fix it – J.C Jan 13 '20 at 21:41
  • 1
    The concept of "open a new tab" - that is, have details "pop up" based on a button click, is usually done using "ajax". That is, the page you have stays the same, and a component on that page calls an API (a controller) to get some data, and the "pop up" is rendered there and then on the page. This is very different to the classic "ASP.Net MVC" framework that is presented to you. In this framework, any change to a page makes a round trip to the server, the page is rendered there, and is displayed as a "partial view" inside the existing page. – Nick.Mc Jan 15 '20 at 04:03
  • 1
    Sorry for posting links all the time but... https://stackoverflow.com/questions/27030421/how-to-load-partial-view-on-clicking-html-actionlink?rq=1 This is an example of refreshing just a small piece of an existing page (i.e. a popup). The page is generated using Razor and MVC – Nick.Mc Jan 15 '20 at 04:30
  • 1
    Fundamentally you need to understand that ASP.Net MVC does a "round trip" every time. Razor runs _on the server_ and generates a page and is returned. This is _very_ different to AJAX, where javascript is used to alter the web page on the client side, based on data returned by a web API (A controller). AJAX is much snappier and usable than standard ASP.Net MVC – Nick.Mc Jan 15 '20 at 07:07

0 Answers0