在“YourText”文本区域中键入并按下“Encode”按钮后,编码文本不会出现在“EncodedText”文本区域中。如果将@html.textarea 替换为“DisplayFor”,则会显示编码文本。
我需要在文本区域中显示编码文本。如果可行,则读取 EncodedText 文本区域,然后按下“解码”按钮,将文本解码为“解码文本”文本区域。
控制器:
[HttpGet]
public ActionResult EncodeText(FormData formData)
{
SelectList items = new SelectList(ciphers);
ViewBag.Ciphers = items;
// Caesar cipher decode CODE here...
return View(formData);
}
[HttpPost]
public ActionResult EncodeText(FormData formData, FormCollection form)
{
SelectList items = new SelectList(ciphers);
ViewBag.Ciphers = items;
// Caesar cipher encode CODE here...
return View(formData);
}
查看“索引”:
@model StringCoder_ASP.NET_MVC.Models.FormData
<div>
<div>
@using (Html.BeginForm("EncodeText", "Home", FormMethod.Post))
{
@Html.Label("Input your text:") <br />
@Html.TextArea("YourText") <br />
@Html.Label("Your encoded text:") <br />
@Html.TextAreaFor(e => e.EncodedText, ViewBag.EncText as string) <br />
@Html.Label("Your decoded text:") <br />
@Html.TextAreaFor(d => d.DecodedText) <br />
<div id="buttonEncode">
<input id="button" type="submit" value="Encode" formaction="@Url.Action("EncodeText", "Home")" /> <br />
</div>
<div id="buttonDecode">
<input id="button" type="submit" value="Decode" formaction="@Url.Action("DecodeText", "Home")" /> <br />
</div>
<div>@Html.Label("Select a cipher:", null, new { @id = "labelCipher" })</div>
<div>@Html.DropDownList("Ciphers", ViewBag.Ciphers as SelectList, null, new { @id = "dropdownlistCipher" })</div>
<div>@Html.Label("Enter a key:", null, new { @id = "labelKey" })</div>
<div>@Html.TextBox("tbkey", null, new { @id = "tbkey" })</div>
}
</div>
</div>
查看“编码文本:”
@using (Html.BeginForm("EncodeText", "Home", FormMethod.Get))
{
@Html.Label("Input your text:") <br />
@Html.TextArea("YourText") <br />
@Html.Label("Your encoded text:") <br />
@Html.TextAreaFor(e => e.EncodedText@*(string)ViewBag.EncText*@) <br />
@Html.Label("Your decoded text:") <br />
@Html.TextAreaFor(d => d.DecodedText) <br />
<div id="buttonEncode">
<input id="button" type="submit" value="Encode" formaction="@Url.Action("EncodeText", "Home")" /> <br />
</div>
<div id="buttonDecode">
<input id="button" type="submit" value="Decode" formaction="@Url.Action("DecodeText", "Home")" /> <br />
</div>
<div>@Html.Label("Select a cipher:", null, new { @id = "labelCipher" })</div>
<div>@Html.DropDownList("Ciphers", ViewBag.Ciphers as SelectList, null, new { @id = "dropdownlistCipher" })</div>
<div>@Html.Label("Enter a key:", null, new { @id = "labelKey" })</div>
<div>@Html.TextBox("tbkey", null, new { @id = "tbkey" })</div>
}
视图“解码”是相同的。
为什么你有一个属性 Model.EncodedText 和一个 ViewBag 变量 ViewBag.EncText?理想情况下,您应该将编码文本放在模型属性中。
您可以尝试在没有 HtmlHelper 的情况下显示文本:
<textarea id="EncodedText" name="EncodedText">@Model.EncodedText</textarea> @*or ViewBag.EncText*@
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句