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

i2loujxw  于 5个月前  发布在  .NET
关注(0)|答案(2)|浏览(76)

我试图获取用户在页面上的文本区域中输入的值,并将该值存储为变量中的字符串。
我花了一个多小时研究从C#中的文本区域获取值的所有不同方法,并尝试了许多示例代码的组合,并试图使其适应我的,但它们都不起作用。要么是库不再存在,要么是示例代码有问题,我不想修复8年多前的东西。
在2022年,是否有新的方法可以获取我的razor page textarea中的值并将其存储在字符串中,以便我可以根据需要重用它?
我看过一篇关于栈溢出的文章,已经发表了8年多了,它不起作用,或者我实现错了。

qkf9rpyu

qkf9rpyu1#

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

字符串
你可以把它放在一个JavaScript函数中,然后在onlcick(像一个提交按钮)和/或在文本区域作为OnTextChanged调用。

pjngdqdw

pjngdqdw2#

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

}

字符串

相关问题