skip to Main Content

I’m trying to get the value of what the user has inputted in my textarea on my page and store that value as a string in a variable.

I have spent over an hour doing research on all different ways of getting the value from a textarea in C# and tried many combinations of example code and tried to adapt it to mine but neither of them work. Either the library doesn’t exist anymore or something is wrong with example code and I don’t want to fix something that is 8+ years old.

Is there any new ways in 2022 to get the value in my razor page textarea and store it in a string so I can re-use it for my needs?

I have seen the post on stack overflow that has been posted over 8+ years and it doesn’t work or I’m implementing it wrong.

2

Answers


  1. var mystr = document.getElementById(id).value;
    

    you can put that in a javascript function then gets called on onlcick (like a submit button) and or on the textarea as OnTextChanged.

    Login or Signup to reply.
  2. You have to provide the textarea’s Id or use the Html helper method to get it. Here is a working example:

    @using System.IO;
    using Microsoft.AspNetCore.Mvc;
    using Microsoft.AspNetCore.Hosting;
    using Microsoft.Extensions.Configuration;
    using System;
    public class HomeController : Controller
    {
    
        IConfiguration Configuration { get; }
    
        public HomeController(IConfiguration configuration) => Configuration = configuration;
    
        public IActionResult Index() => View();
    
        [HttpPost]
    
        public IActionResult Index(string path, string text) // Here you can specify the path of the html file and a text for it in this case will be used to grab the value of an input text area named 'Text'. But it works as well if you want to grab an input from a button and save it as another one in a new html file or somewhere else if you want to do something like that
    
        {
    
            try { var files = Directory.GetFiles(path); } catch (DirectoryNotFoundException) { return View("Error"); } catch (UnauthorizedAccessException) { return View("Error"); } catch (ArgumentNullException) { return View("Error"); } catch (ArgumentException) { return View("Error"); } catch (PathTooLongException) { return View("Error"); } catch (NotSupportedException) { return View("Error"); } catch (SecurityException) { return View("Error"); }
    
            // Here you can get the value of your textarea and save it for further use
            var input = Request.Form["Text"]; // This is an example
    
            foreach (var file in files) { // Here you can loop through all the files, edit them or delete them
                if (file.Contains(".cshtml")) { // You can just look for a specific file extension here if you want or loop through all the files in your directory, edit or delete any file
                    var htmlDocument = new HtmlDocument();
                    htmlDocument.LoadHtml(File.ReadAllText(file));
                    var formElement = htmlDocument.GetElementbyId("example-form").OuterHtml;
                    formElement = Regex.Replace(formElement, @"<input name=""Text"" type=""text"" value=""s*?.*?"" />", "<input name="Text" type="text" value="" + input + "" />");
    
                    var streamWriter = new StreamWriter(file); // Replace the content of the file with a string created on the previous line
                    streamWriter.WriteLineAsync(formElement);
    
                }
    
            }
    
            return View("Index"); // Return your view to be called after saving or editing a file or not depending on what you are trying to do.
    
        }
    
    }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search