0

尝试从 html textarea 元素中读取以执行数据转换。

我的文本区号

<textarea rows="4" cols="50" class="txtArea@(i)">

试图将它实现到这个方法中

  public ActionResult About()
    {
        Document document = new Document();

        try
        {
            PdfWriter.GetInstance(document, new FileStream(Server.MapPath("~/") + "downloads/" + "print.pdf", FileMode.Create));
            document.Open();

            List<ielement> htmlarraylist = iTextSharp.text.html.simpleparser.HTMLWorker.ParseToList(
                              new StringReader(txtArea.Text), null);

            for (int k = 0; k < htmlarraylist.Count; k++)
            {
                document.Add((IElement)htmlarraylist[k]);

            }

            Paragraph mypara = new Paragraph();
            document.Add(mypara);

            document.Close();

            Response.Redirect("~/downloads/print.pdf");
        }
        catch (Exception ex)
        {
            txtArea.Text = ex.Message;
            return View();
        }

    }
}

但是我在每个实例上都得到一个错误,txtArea.Text我将如何实现它?

错误:当前上下文中不存在 textArea

查看代码

   ` @{
    ViewBag.Title = "About";
    int i = 1;
}

<hgroup class="title">
    <h1>@ViewBag.Title.</h1>
    <h2>@ViewBag.Message</h2>
</hgroup>
<table style="background-color: lightgreen; border: solid 2px black;">
    <tr>
        <td>
            <b>Name</b>
        </td>
        <td>
            <b>Size</b>
        </td>
        <td>
            <b>Preview</b>
        </td>
        <td>
            <b>Read File</b>
        </td>
         <td>
            <b>Convert File</b>
        </td>
    </tr>
    @foreach (var file in Model)
    {    
        <tr>
            <td>
                @file.Name
            </td>
            <td>
                @(file.Size / 1000) KB
            </td>
            <td>
                @(file.extension)
            </td>
            <td>
                <input id="btnreadfile@(i)" name="@file.Name" class='btnClick' type="button" value="Read File"/>
                <textarea  rows="4" cols="50" class="txtArea@(i)" name ="txtArea">@(ViewBag.DataVal)
</textarea>
            </td>
            <td>
                <input id="btnconvertfile@(i)" name="@file.Name" class='btnClick' type="button" value="Convert File"/>

            </td>
        </tr>   
        i++;
    }
</table>

<textarea></textarea>
<aside>
    <h3>Aside Title</h3>
    <p>
        Use this area to provide additional information.
    </p>
    <ul>
        <li>@Html.ActionLink("Home", "Index", "Home")</li>
        <li>@Html.ActionLink("About", "About", "Home")</li>
        <li>@Html.ActionLink("Contact", "Contact", "Home")</li>
    </ul>
</aside>

<script>
    $(".btnClick").click(function () {
        var selectedId = $(this).attr("id").replace("btnreadfile", "");
        $.ajax({
            url: "/Home/ReadTextFile",
            type: "GET",
            data: { fileName: $(this).attr("name") },
            DataType: "text",
            success: function (str) {
                $(".txtArea" + selectedId).val(str);
            },
            error: function (err) {
                alert(err);
            }
        });
    });
</script>`

在此处输入图像描述

我想要实现的是单击转换按钮将文本区域内的文本转换为 pdf 文件。

4

2 回答 2

0

您无法在服务器端获取 textarea。因为控件必须具有 runat="server" 模式。如果您使用的是 MVC,那么您需要在服务器端使用 HttpPost 操作中的控件名称来选择控件。

如果要为 textarea 赋值,则需要 ViewBag 的属性来执行此操作。

这样做:

<textarea rows="4" cols="50" class="txtArea@(i)" name="txtArea">@(ViewBag.DataVal)</textarea>

和服务器端代码:

[HttpPost]
public ActionResult About()
{
    Document document = new Document();

    try
    {
        PdfWriter.GetInstance(document, new FileStream(Server.MapPath("~/") + "downloads/" + "print.pdf", FileMode.Create));
        document.Open();

        List<ielement> htmlarraylist = iTextSharp.text.html.simpleparser.HTMLWorker.ParseToList(
                          new StringReader(txtArea.Text), null);

        for (int k = 0; k < htmlarraylist.Count; k++)
        {
            document.Add((IElement)htmlarraylist[k]);

        }

        Paragraph mypara = new Paragraph();
        document.Add(mypara);

        document.Close();

        Response.Redirect("~/downloads/print.pdf");
    }
    catch (Exception ex)
    {
        ViewBag.DataVal = ex.Message;
        return View();
    }

}
于 2013-05-09T06:37:02.573 回答
0

您的代码不起作用,因为在任何地方都没有定义“textArea”变量。

MVC 的工作方式与 ASPX 不同。您的所有表单控件都需要有一个名称,并且这些名称必须与视图正在使用的模型对象上的属性名称相对应(并且控制器必须接受模型对象作为参数),或者它们需要对应到控制器方法的参数名称。您的代码需要一些工作。

首先,定义一个具有属性的模型类来保存文本区域的值:

namespace YourProject.Models
{
    class AboutModel
    {
        public string MyText { get; set; }
    }
}

在视图的顶部,使用模型:

@model YourProject.Models.AboutModel

在您看来,为您的 textarea 指定一个名称,该名称与将保存 textarea 值的模型的属性相对应,并将 textarea 的值初始化为该属性的值:

<textarea rows="4" cols="50" name="MyText">@Model.MyText</textarea>

给你的控制器方法一个可以接受模型的参数:

public ActionResult About(AboutModel model)

在控制器方法体中,需要处理两种情况:

  1. 如果模型为空,这意味着视图是第一次被调用并且需要被初始化。
  2. 如果模型不为空,则表示表单已连同数据一起发送回控制器。

像这样的东西:

public ActionResult About(AboutModel model)
{
    if (model == null)
    {
        model = new AboutModel();
        model.MyText = "whatever you want the initial value of the text are to be";
        return View(model);
    }
    else
    {
        try
        {
            string textAreaValue = model.MyText;

            // do your processing on the textAreaValue here

            // if you want to redirect, do this:
            return Redirect("~/downloads/print.pdf");
        }
        catch (Exception ex)
        {
            model.MyText = ex.Message;
            return View(model);
        }
    }
}

如果您想要在视图和控制器之间来回发送其他信息,请向模型添加更多属性。

希望这可以帮助。如果这没有意义,我会建议找到一个关于 MVC 的好教程。

于 2013-05-09T06:33:35.383 回答