不显示值从控制器到文本区域

时间:2019-08-09 12:34:48

标签: c# razor asp.net-mvc-5

在“ YourText”文本区域中键入并按“ Encode”按钮后,编码后的文本不会出现在“ EncodedText”文本区域中。如果将@ html.textarea替换为“ DisplayFor”,则会显示编码的文本。

我需要在文本区域中显示编码的文本。 如果可以解决问题,则读取EncodedText文本区域,然后按“解码”按钮,将文本解码为“解码的文本”文本区域。

This project on GitHub

控制器:

 [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>

查看“ EncodeText:”

@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>
        }

视图“解码”相同。

1 个答案:

答案 0 :(得分:0)

为什么具有属性Model.EncodedText和ViewBag变量ViewBag.EncText?理想情况下,应将编码后的文本放入模型属性中。

您可以尝试在没有HtmlHelper的情况下显示文本:

<textarea id="EncodedText" name="EncodedText">@Model.EncodedText</textarea> @*or ViewBag.EncText*@