Click here to Skip to main content
16,020,378 members
Home / Discussions / ASP.NET
   

ASP.NET

 
GeneralRe: trouble sending message to the view from controller Pin
Hamiltonian1330-May-17 7:23
Hamiltonian1330-May-17 7:23 
GeneralRe: trouble sending message to the view from controller Pin
F-ES Sitecore30-May-17 22:13
professionalF-ES Sitecore30-May-17 22:13 
AnswerRe: trouble sending message to the view from controller Pin
John C Rayan31-May-17 1:56
professionalJohn C Rayan31-May-17 1:56 
GeneralRe: trouble sending message to the view from controller Pin
Hamiltonian1331-May-17 9:16
Hamiltonian1331-May-17 9:16 
GeneralRe: trouble sending message to the view from controller Pin
John C Rayan31-May-17 22:17
professionalJohn C Rayan31-May-17 22:17 
GeneralRe: trouble sending message to the view from controller Pin
F-ES Sitecore1-Jun-17 0:41
professionalF-ES Sitecore1-Jun-17 0:41 
GeneralRe: trouble sending message to the view from controller Pin
John C Rayan1-Jun-17 22:05
professionalJohn C Rayan1-Jun-17 22:05 
GeneralRe: trouble sending message to the view from controller Pin
Hamiltonian139-Jun-17 6:28
Hamiltonian139-Jun-17 6:28 
Hi everyone,

Firstly, thanks for trying to help me out with this. I have fixed the issue. The problem was not in the controller, it was in fact the view.

My problem is I am completely new to this type of coding, In fact, up to a couple of months ago, the only programming I had dabbled in was matlab for scientific analysis purposes. I probably should spend more time learning the basics, but as I have absolutely no patience, I have decided to build first and analyse later.

I would appreciate it if I could get some help understanding where I went wrong.

The view I was using was utilizing Ajax. Data was added to the form data collection and then passed to the controller via Ajax call.

The View:
@{
    ViewBag.Title = "Upload";
}
<h2>Upload</h2>
<script src="~/Scripts/jquery-1.11.1.min.js"></script>
<script>
    $(document).ready(function () {
        $(‘#btnUploadFile’).on(‘click’, function () {
            var data = new FormData();
            var files = $("#fileUpload").get(0).files;
            // Add the uploaded image content to the form data collection
            if (files.length > 0) {
                data.append("UploadedImage", files[0]);
            }
            // Make Ajax request with the contentType = false, and procesDate = false
            var ajaxRequest = $.ajax({
                type: "POST",
                url: "",
                contentType: false,
                processData: false,
                data: data
            });
            ajaxRequest.done(function (xhr, textStatus) {
                // Do other operation
            });
        });
    });
</script>
<input type="file" name="FileUpload1″ id="fileUpload" /><br />
<input id="btnUploadFile" type="button" value="Upload File" />
@Html.ActionLink("Documents", "Downloads")
<p> 
        @TempData["msg"] 
</p>



I had a feeling (Uneducated guess!)that using the Ajax request was the problem so I changed my approach. I used the Html.BeginForm() extension method

I changed my view to:

@using (Html.BeginForm("UploadFiles", "CIsAdmin", FormMethod.Post, new { enctype = "multipart/form-data" }))
{

    <input type="file" name="FileUpload1" /><br />
    <input type="submit" name="Submit" id="Submit" value="Upload" />
}
@Html.ActionLink("Documents", "Downloads")
<p> 
        @TempData["msg"] 
</p>


Can someone explain in simple terms, what the issue was?

FYI, this is my controller code:
public ActionResult UploadFiles(int? id,string msg)
       {
           CI cI = db.LotusCIs.Find(id);
           foreach (string upload in Request.Files)
           {

               if (Request.Files[upload].FileName != "")

               {
                   string path = Server.MapPath(@"/App_Data/uploads/" + id + "/");
                   string filename = Path.GetFileName(Request.Files[upload].FileName);
                   Response.Write(path);

                   bool check = System.IO.File.Exists(Path.Combine(path, filename));
                   //System.Diagnostics.Debug.Write(System.IO.File.Exists(Path.Combine(path, filename)) ? "File exists." : "File does not exist.");

                   //Check if the filename exists or not, send success/fail message
                   if (check==true)
                   {
                       TempData["msg"] = "File with the same name already exists - Choose another file or change the file name";
                      // System.Diagnostics.Debug.Write("file exists");
                       return RedirectToAction("UploadFiles", "CIsAdmin") ;
                   }

                   if (check == false)
                   {
                       TempData["msg"] = "Successfull Upload";
                       Request.Files[upload].SaveAs(Path.Combine(path, filename));
                       return RedirectToAction("UploadFiles", "CIsAdmin");
                   }
               }
           }

                    return View("Upload");
       }


Thanks in Advance Thumbs Up | :thumbsup:
GeneralRe: trouble sending message to the view from controller Pin
John C Rayan16-Jun-17 2:48
professionalJohn C Rayan16-Jun-17 2:48 
Questionadd small delete button or image to the options of select Pin
indian14326-May-17 13:51
indian14326-May-17 13:51 
QuestionNeed some help in implementing Partial View Pin
indian14323-May-17 13:44
indian14323-May-17 13:44 
AnswerRe: Need some help in implementing Partial View Pin
jkirkerx24-May-17 12:33
professionaljkirkerx24-May-17 12:33 
GeneralRe: Need some help in implementing Partial View Pin
indian14324-May-17 13:26
indian14324-May-17 13:26 
GeneralRe: Need some help in implementing Partial View Pin
jkirkerx25-May-17 10:08
professionaljkirkerx25-May-17 10:08 
Questioncell_double click even on grid view Pin
Mayank Kumar23-May-17 0:14
Mayank Kumar23-May-17 0:14 
AnswerRe: cell_double click even on grid view Pin
Maciej Los23-May-17 20:19
mveMaciej Los23-May-17 20:19 
QuestionReturn message to user if file already exists Pin
Hamiltonian1322-May-17 10:59
Hamiltonian1322-May-17 10:59 
AnswerRe: Return message to user if file already exists Pin
F-ES Sitecore22-May-17 22:59
professionalF-ES Sitecore22-May-17 22:59 
GeneralRe: Return message to user if file already exists Pin
Hamiltonian1324-May-17 8:14
Hamiltonian1324-May-17 8:14 
QuestionSingleton Class in Asp.net application Pin
Rafique Abdullah19-May-17 21:54
Rafique Abdullah19-May-17 21:54 
AnswerRe: Singleton Class in Asp.net application Pin
Nathan Minier20-May-17 12:43
professionalNathan Minier20-May-17 12:43 
GeneralRe: Singleton Class in Asp.net application Pin
Rafique Abdullah22-May-17 7:15
Rafique Abdullah22-May-17 7:15 
GeneralRe: Singleton Class in Asp.net application Pin
F-ES Sitecore22-May-17 22:50
professionalF-ES Sitecore22-May-17 22:50 
QuestionHow to populate dropdownlist in the partial view, base upon the selection of another dropdown value in the view, using angular js Pin
Member 1318632317-May-17 20:16
Member 1318632317-May-17 20:16 
QuestionError Pin
Member 1116162516-May-17 20:10
Member 1116162516-May-17 20:10 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.