Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
H

Hamiltonian13

@Hamiltonian13
About
Posts
7
Topics
2
Shares
0
Groups
0
Followers
0
Following
0

Posts

Recent Best Controversial

  • trouble sending message to the view from controller
    H Hamiltonian13

    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";
    }

    Upload

    $(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
            });
        });
    });
    

    @Html.ActionLink("Documents", "Downloads")

        @TempData\["msg"\] 
    

    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" }))
    {

    }
    @Html.ActionLink("Documents", "Downloads")

        @TempData\["msg"\] 
    

    Can someone explain in simple terms, what the issue was? FYI, this is my controller code:

    public ActionResult UploadFiles(int? id,string msg)

    ASP.NET database help asp-net sysadmin debugging

  • trouble sending message to the view from controller
    H Hamiltonian13

    Yes

    ASP.NET database help asp-net sysadmin debugging

  • trouble sending message to the view from controller
    H Hamiltonian13

    Thanks for your reply. I have been using all three ViewBag, TempData & Session while testing but to no avail. I agree Temp data is the best here. From my little knowledge, I have read that it is best practice to use TempData for redirects.

    ASP.NET database help asp-net sysadmin debugging

  • trouble sending message to the view from controller
    H Hamiltonian13

    Thanks for your answer. Unfortunately it did not solve my issue. The code seems to be operating as expected when using breakpoints, it is getting to the RedirectToAction line but no reload. Refresh browser and message is there!

    ASP.NET database help asp-net sysadmin debugging

  • trouble sending message to the view from controller
    H Hamiltonian13

    Hi all, I am having trouble sending a success/fail message to a view from my controller. I have a file upload section of my website. User chooses file, presses upload button and file is saved in a folder. My Controller code:

    public ActionResult UploadFiles(int? id)
    {
    CI cI = db.DataBase.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));
                    
    
                    //Check if the filename exists or not, send message and refresh
                    if (check==true)
                    {
                        Session\["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");
                    }
    
                    if (check == false)
                    {
                        Session\["msg"\] = "Successfull Upload";
                        Request.Files\[upload\].SaveAs(Path.Combine(path, filename));
                        System.Diagnostics.Debug.Write("Upload success");
                        return RedirectToAction("UploadFiles");
                    }
                }
            }
    
                     return View("Upload");
        }
    

    Then I am calling the message in my view using:

    @Session["msg"];

    The code is working for the most part i.e. it will check if the file exists/doesn't correctly and print the correct message to the output. My problem is getting the message to the view. If I manually refresh the page my message gets to the view. So that tells me I am making a mistake with how I am redirecting. I am quite new to Mvc so I can't identify the problem. Any help would be greatly appreciated.

    ASP.NET database help asp-net sysadmin debugging

  • Return message to user if file already exists
    H Hamiltonian13

    Thanks a mill! Got the basic function working using:

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

    You are a star. I know I should be able to find this info by myself but I am very very early in my development so sometimes it is hard to know what to look for. Thanks!

    ASP.NET database html sysadmin question

  • Return message to user if file already exists
    H Hamiltonian13

    Hi all, I am developing an application that allows users to upload/download/delete files. I have it working but I am struggling to add the following function. Check if the file already exists in the specified folder, if the file exists already, return a message telling the user to choose another name. if the file doesn't exist, save the file in the specified folder. This is my controller code:

    public ActionResult UploadFiles(int? id)
    {
    CI cI = db.Database.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);
                  
                    Request.Files\[upload\].SaveAs(Path.Combine(path, filename));
               
                }
            }
           
            return View("Upload");
        }
    

    The upload view:

    @{
    ViewBag.Title = "Upload";
    }

    Upload

    $(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) {
                @ViewBag.Message
            });
        });
    });
    

    @Html.ActionLink("Documents", "Downloads")

    ASP.NET database html sysadmin question
  • Login

  • Don't have an account? Register

  • Login or register to search.
  • First post
    Last post
0
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups