如何使用 C# 从文本区域获取用户输入

发布于 2025-01-12 20:37:24 字数 275 浏览 1 评论 0原文

我试图获取用户在页面上的文本区域中输入的值,并将该值作为字符串存储在变量中。

我花了一个多小时研究从 C# 中的文本区域获取值的所有不同方法,并尝试了示例代码的多种组合,并尝试使其适应我的情况,但它们都不起作用。要么该库不再存在,要么示例代码有问题,我不想修复 8 年前的问题。

2022 年是否有任何新方法可以获取我的 razor 页面文本区域中的值并将其存储在字符串中,以便我可以根据我的需要重新使用它?

我看过关于堆栈溢出的帖子,该帖子已发布超过 8 年,但它不起作用,或者我实施错误。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

贱人配狗天长地久 2025-01-19 20:37:24
var mystr = document.getElementById(id).value;

您可以将其放入 JavaScript 函数中,然后在 onlcick(如提交按钮)和/或文本区域上调用 OnTextChanged。

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.

冷夜 2025-01-19 20:37:24

您必须提供文本区域的 Id 或使用 Html 帮助器方法来获取它。这是一个工作示例:

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.
    }

}

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,
                
quot;<input name=\"Text\" type=\"text\" value=\"\s*?.*?\" />",
                
quot;<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.
    }

}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文