When uploading file I can't get the primary key value of the row that refers to this file

When uploading file I can't get the primary key value of the row that refers to this file

azonekz@gmail.comazonekz@gmail.com Posts: 32Questions: 9Answers: 1

When uploading file I can't get the primary key value of the row that refers to this file(string idRow in my code), thanks.

 var request = System.Web.HttpContext.Current.Request;          
        using (var db = new DataTables.Database(ConfigurationManager.AppSettings["DbType"], ConfigurationManager.AppSettings["DbConnection"]))
        {
            if (ele_id != null)
            {
                 eleId = ele_id;                
            }
            string file_id = "";
            var response = new Editor(db, "lectures", "id")
                .Field(
                    new Field("lectures.enr_audio")
                    .Upload(
                           new Upload((file,id) =>
                           {                                 
                               file.SaveAs(request.PhysicalApplicationPath + @"uploads\"+file.FileName);
                               string path = request.PhysicalApplicationPath + @"uploads\" + file.FileName;
                               string idRow = @"__ID__";                                 
                               SaveChrono(path, idRow);
                               return  file_id = file.FileName ;
                           }).AllowedExtensions(new string[] { "mp3", ".mp3" }, "Veuillez télécharger un fichier audio mp3")
                            .Validator(file => {
                                if (file.ContentLength >= 9000000)
                                {
                                    return "Les fichiers doivent être inférieurs à 9Mo";
                                }
                                return null;
                            })
                           )

                        .SetFormatter(Format.NullEmpty())
                        )
                .Model<JoinModel1>()                    
                    .LeftJoin("documents", "documents.id", "=", "lectures.id_doc")                      
                    .Where("lectures.id_ele", eleId, "=")
                    .Process(request)
                    .Data();
            return Json(response, JsonRequestBehavior.AllowGet);
        }           

Answers

  • allanallan Posts: 61,715Questions: 1Answers: 10,107 Site admin

    You won't be able to get the host id for the file from the foreign table (i.e. the host table) since it might not exist yet. Consider the "create" case - you click the create button and then upload a file. Because the file upload is async to the rest of the form, it uploads immediately and needs to be saved in the database. At this point there is no host row.

    That's why you would typically refer to the file id from the host table rather than the other way around.

    Allan

  • azonekz@gmail.comazonekz@gmail.com Posts: 32Questions: 9Answers: 1

    Thanks for response Allan!
    Can you tell me at what moment can I get id_row in which file was unloaded?

  • allanallan Posts: 61,715Questions: 1Answers: 10,107 Site admin

    It will exist when this function runs:

    new Upload((file,id) => ...

    But that might happen before the host row is created!

    Allan

  • azonekz@gmail.comazonekz@gmail.com Posts: 32Questions: 9Answers: 1

    in my example new Upload((file,id) it return always id=null

  • allanallan Posts: 61,715Questions: 1Answers: 10,107 Site admin

    Ah I see - yes. That's because there is no Db() method being specified like you can see in the examples here.

    If there isn't a Db method then the libraries can't know how to get the primary key from the database.

    If you are saving it to the database yourself then simply return the id that you want to have used from your custom action method.

    Allan

This discussion has been closed.