0

I'm very new to MVC (current Web Forms developer), I'm having difficulty assigning values to the view. Can anyone tell me what I did wrong?

The app is supposed to output the encrypted or decrypted string.

Here's my controller:

        public ActionResult Index(EncryptionModel encryption)
    {
        string Key = encryption.Key;
        string EncryptString = encryption.EncryptString;
        string DecryptString = encryption.DecryptString;
        string Output = encryption.Output;


        var process = new EncryptionModel();
        if (!String.IsNullOrEmpty(EncryptString))
        {
            process.Output = StringCipher.Encrypt(EncryptString, Key);
        }
        else
            process.Output = StringCipher.Decrypt(DecryptString, Key);


        return View("Index",process);         
    }

My view:

<body>
@using (Html.BeginForm("Index", "Home", FormMethod.Post))
{
    <table cellpadding="0" cellspacing="0">
        <tr>
            <th colspan="2" align="center">Encrypt and Decrypt</th>
        </tr>
        <tr>
            <td>Key: </td>
            <td>
                @Html.TextBoxFor(m => m.Key)
            </td>
        </tr>
        <tr>
            <td>Encrypt String: </td>
            <td>
                @Html.TextBoxFor(m => m.EncryptString)
            </td>
        </tr>
        <tr>
            <td>Decrypt String: </td>
            <td>
                @Html.TextBoxFor(m => m.DecryptString)
            </td>
        </tr>
        <tr>
            <td>Output: </td>
            <td>
                @Html.TextBoxFor(m => m.Output)
            </td>
        </tr>
        <tr>
            <td></td>
            <td><input type="submit" value="Submit" /></td>
        </tr>
    </table>
}

Thanks in advance!

sojim2
  • 1,245
  • 2
  • 15
  • 38
  • 1
    so what's exactly the question? assign the value ? what is not working exactly? – esiprogrammer Nov 29 '16 at 22:14
  • The output(view) doesn't show anything. `@Html.TextBoxFor(m => m.Output)` – sojim2 Nov 29 '16 at 22:15
  • 1
    Before asking you should try and do some research by yourself... https://www.asp.net/mvc/overview/older-versions/mvc-music-store/mvc-music-store-part-3 – JleruOHeP Nov 29 '16 at 22:15
  • 2
    Because the value of `Output` has been added to `ModelState` by the `DefaultModelBinder` and the `TextBoxFor()` method uses the values from `ModelState`. You can use `ModelState.Clear()` before updating the values in the controller (but you should be following the PRG pattern and redirecting, not returning the view). For a detailed explanation, refer [this answer](http://stackoverflow.com/questions/26654862/textboxfor-displaying-initial-value-not-the-value-updated-from-code/26664111#26664111) –  Nov 29 '16 at 22:16
  • @StephenMuecke clearing model worked! Will look into PRG pattern. – sojim2 Nov 29 '16 at 22:25

0 Answers0