0

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 2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Thats JS not C#!
is you app a windows form?
No my app is not windows form
0

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 System.Security;
using System.Text.RegularExpressions;
using System.Web.Mvc;
using System.Windows.Forms;
using System.Xml.Linq;
using System;

@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();

    // 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
    [HttpPost]
    public IActionResult Index(string path, string text)
    {
        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.
    }

}

4 Comments

Does this require me to install packages of any sort?
No. However, you will have to add the following settings in your launchconfig file: "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development", "ASPNETCORE_URLS": "http://localhost:52794", // Something like http://my-project.localdomain:52794 "ASPNETCORE_ENABLE_PROFILING": "true" }, Anything other than Development will make it fail The last "true" is only needed if you want profiling information to be generated in case you are running netcoreapp2.0 or above.
Okay thats really complicated is there no other way to just get the input and store it and do nothing else. I just want to get whats on the screen.
This is how I did it, but you can try to Google another way to do it. I assume that you've already tried that and haven't gotten any good results. I encourage you to try out what I have posted.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.