how to return json data from mvc controller and view

我的未来我决定 提交于 2020-01-24 19:50:13

问题


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

来源:https://stackoverflow.com/questions/59708648/how-to-return-json-data-from-mvc-controller-and-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!