【问题标题】:Not displayed value into textarea from controller未从控制器将值显示到文本区域中
【发布时间】:2019-12-17 04:53:07
【问题描述】:

在“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>
        }

查看“解码”是一样的。

【问题讨论】:

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


    【解决方案1】:

    为什么你有一个属性 Model.EncodedText 和一个 ViewBag 变量 ViewBag.EncText?理想情况下,您应该将编码文本放在模型属性中。

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

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

    【讨论】:

    • 成功了!谢谢你!但是为什么这不适用于 @html.textarea("EncodedText", Model.EncodedText)
    • 我不知道为什么,但是 html 标签助手无法显示您的编码文本。
    猜你喜欢
    • 1970-01-01
    • 2016-06-08
    • 2013-08-15
    • 2021-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-23
    • 1970-01-01
    相关资源
    最近更新 更多