尝试从 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 文件。