Greatings :D, I'm a MVC noob, I admit it. I'm working on a test project to get myself in tuned with the idea. It's super simple text to hex program.
I'll break down what's it's doing, then what's happening.
I have my view, which has a textarea, and a submit button. User types in data, clicks submit.
@model MVC.Models.ClsTextToHex
@{
ViewBag.Title = "Text To Hex";
}
<h2>Convert To Hex</h2>
<form method="post">
<textarea name="message" style="margin: 0px; width: 313px; height: 150px"> @Model.HexText</textarea>
<div>
<input type="submit" value="Convert" />
</div>
</form>
it goes to the controller:
[HttpPost]
public ActionResult CTHView(ClsTextToHex ConvertToHex, string message)
{
ConvertToHex.ConvertTTH(message, ConvertToHex.Hexed);
return CTHView(ConvertToHex);
}
Controller then talks to my model which sets two variables 1.RealText (to contain what it was) and 2.Hexed (to contain the Hex...). My controller then reloads the view the view now showing the hex instead of the text.
However, Now I'd like to turn the Hex, back into text. My model has the code, but since the model resets everytime, I'm unable to keep track of anything to resend values or update new ones, it just keeps converting the text into hex.
So my noob question comes down to... How can I use a model to where my variable data isn't wiped clean every time. Or am I just on the wrong track? I come from a C# application based background so doing something like this is as easy as initializing a class, setting the values and passing them out...(although I wouldn't really need to do it like that, but I could) seems a little trickier is asp.net...
I've read a lot of answer, and seen videos, and before anyone says anything I'd like to address the single page. And Really, I don't see any reason this can't be one page instead of two, although most of the things I've seen typically just send you to a new page with the same look tricking the user in thinking it's the same page. I get the concept, just not feeling how it comes into play here.
Basically every time you convert the plain text, you need to keep track of that information (converted plain text to hex) somewhere so that next time the form is submitted you can use that to determine whether to convert to hex or convert it back to plain text.
Since Http is stateless, we need to send the state in the form submit request. So every time you convert plain text to hex, keep a boolean variable inside your form with value true and when the form gets submitted inspect that.
You can add this boolean property to your view model class.
public class ClsTextToHex
{
public string Text { set; get; }
public bool IsHex { set; get; }
}
And in your view, keep this in a hidden variable. Based on the value of this boolean property, you can conditionally update your button text as well.
@model YourNameSpaceGoesHere.ClsTextToHex
@using (Html.BeginForm("ToHex", "Home"))
{
@Html.HiddenFor(f => f.IsHex)
@Html.TextAreaFor(f => f.Text)
<div>
@{
var btnText = Model != null && Model.IsHex ? "Convert back" : "Convert to Hex";
}
<input type="submit" value="@btnText"/>
</div>
}
Now all you have to do is to check this property value in your HttPost action. The important thing to remember is, since we are updating the property value of the view model and sending to the same view, we need to clear the Model state dictionary explicitly so that the helper methods(TextAreaFor
and HiddenFor
) will use the correct (updated) value. You can use the ModelState.Clear()
method to do so.
[HttpPost]
public ActionResult ToHex(ClsTextToHex model)
{
if (!model.IsHex)
{
model.Text = "Hex" + model.Text; // My temp hexification code :). Fix this
model.IsHex = true;
}
else
{
//My temp un-hexification code :) Fix this with your actual code
model.Text = model.Text.Replace("Hex", "");
model.IsHex = false;
}
ModelState.Clear(); // This will clear the model state dictionary
return View(model);
}