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

问题描述 投票:0回答:2

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

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

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

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

c# asp.net textarea
2个回答
1
投票
var mystr = document.getElementById(id).value;

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


0
投票

您必须提供文本区域的 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.
    }

}

© www.soinside.com 2019 - 2024. All rights reserved.