How to save a specific row of a data table: contains an attachment and texts?

2

I have a list of data in a table, where each row has its respective save . This button saves the date and document attached.

I have it this way:

When I click on save and call my action, it brings me all the rows, so I have to do a for to create criteria and only save the row where I made click However, I think this practice is wrong.

Is there any way to just bring me the data from the row where I clicked on save ?

This is my current coding:

Website:

Controller Action:

public ActionResult GuardarCargoCierreAtencion(List<int> FgCargo, List<String> Mes,List<String> Periodo,
        List<int> CierreAtencionId, List<String> FechaCargo, List<HttpPostedFileBase> DireccionArchivo)
    {
        if (HttpContext.Session["Usuario"] == null)
        {
            if (Request.IsAjaxRequest())
            {
                return JavaScript(string.Format("alert('Su sesión ha terminado, debe volver a iniciar sesión'); window.location.href='{0}'", Url.Action("IniciarSession", "Logueo")));
            }
            else return RedirectToAction("IniciarSession", "Logueo");
        }
        //EstablecimientoId = ((Usuario)Session["Usuario"]).Establecimiento.EstablecimientoId;
        UsuarioID = ((Usuario)Session["Usuario"]).UsuarioId;

        CierreAtencionAD CierreAtencion = new CierreAtencionAD();
        CierreAtencion CierreAt = new CierreAtencion();
       // ControlMedicoUdrEstablecimiento Save=new ControlMedicoUdrEstablecimiento();
        int cont = 0;
        if (DireccionArchivo != null)
        {
            foreach (HttpPostedFileBase requestFile in DireccionArchivo)
            {

                if (requestFile != null)
                {

                    CierreAt = Guardar(CierreAtencionId.ElementAt(cont), FechaCargo.ElementAt(cont).ToString(), Mes.ElementAt(cont).ToString(), Periodo.ElementAt(cont).ToString(), requestFile);
                  //  errorMensaje = errorMensaje + "<br>" + "- " + Save.error + "\n";
                }                    
                cont++;
            }
        }
        //System.Web.HttpContext.Current.Session.Add("CierreGuardar", CierreAt);
        HttpContext.Session["Error"] = CierreAt.Error;
        return RedirectToAction("Index");
    }
    
asked by Abel Reynalte 30.11.2016 в 16:27
source

1 answer

0

In the view with the grid I used this code (it's simplified).

Notice that it hid the ids in the grid that I needed to associate the files with the model. Capable in your case is not necessary.

 <td class="tabla-td">
   @Html.HiddenFor(modelItem => Model.IDPozo)
   @Html.HiddenFor(modelItem => item.IDEnsayo)
     <input type="button" value="Agregar archivo" class="button" id="ButtonAdd" />
   @Html.HiddenFor(modelItem => item.Fecha)
 </td>

In the same view I used the function mostrarVentanaArchivo to call the action of the controller with the parameters I needed

<script type="text/javascript">
$(document).ready(function () {
    $(".button").click(function () {
        var ensayo = $(this).prev("input:hidden").val();
        var fecha = $(this).next("input:hidden").val();
        var pozo = $(this).prev("input:hidden").prev("input:hidden").val();
        mostrarVentanaArchivo(ensayo, fecha, pozo);
    });
});
</script>

<script type="text/javascript">

 function mostrarVentanaArchivo(ensayo, fecha, pozo) {
    $.ajax({
        url: '@Url.Action("Add", "Archivos")',
        type: "GET",
        dataType: "html",
        traditional: true,
        data: { IDEnsayo: ensayo, Fecha: fecha, IDPozo: pozo}
    })
    .done(function (result) {
        // Display the section contents.
        $('#VentanaArchivos').html(result);
    })

}
</script>  

On the controller:

   public ActionResult Add(int IDEnsayo, string Fecha, int IDPozo)
    {
        DateTime FechaTemp = DateTime.Parse(Fecha);
        Ensayo Ensayo = new Ensayo { IDEnsayo = IDEnsayo, Fecha = FechaTemp, IDPozo = IDPozo };
        return PartialView(Ensayo);
    }

    [HttpPost]
    public ActionResult Add(Ensayo Ensayo, HttpPostedFileBase uploadFile, int IDPozo)
    {
        if (uploadFile != null && uploadFile.ContentLength > 0)
        {
            var ensayo = new Ensayo { IDEnsayo = Ensayo.IDEnsayo };
            ensayo.Archivo.Longitud = uploadFile.ContentLength;
            ensayo.Archivo.Nombre = uploadFile.FileName;
            ensayo.Archivo.Tipo = uploadFile.ContentType;
            byte[] tempFile = new byte[uploadFile.ContentLength];
            uploadFile.InputStream.Read(tempFile, 0, uploadFile.ContentLength);
            ensayo.Archivo.Contenido = tempFile;
            ensayo.SaveArchivo();

            Log.Add(DateTime.Now, TipoLog.AsociacionArchivo, HttpContext.User.Identity.Name, "Asocia archivo " + ensayo.Archivo.Nombre);
        }

        return RedirectToAction("Index", new {IDPozo = IDPozo });
    }
    
answered by 30.11.2016 в 22:05