Title: How to make Html.TextBox(string name) render sanitized input?
Tags: c#;asp.net-mvc-4
Question: I am using ASP.NET MVC4 with .NET Framework 4.5. I have a controller action that accepts a model of one type with a property named 'Name' but renders a view using a model of another type. I am still able to use @Html.TextBox("Name") and @Html.ValidationMessage("Name").
I want the textbox to display the sanitized input, that is, the input without leading/trailing/extra spaces the user may have entered. The setter for my model sanitizes the value for me, and I am successfully obtaining the sanitized value using the getter within the controller action. It's just that upon submitting the form, the textbox still displays the unclean input.
Is there some mechanism I am missing? Is the @Html.TextBox(string name) helper looking at the raw request data and not the model? If so, how come the validation message is working?
Update
I have just tried defining a new view model that includes my textbox field so I could hopefully just use the @Html.TextBoxFor helper. Everything is still working as it was after a re-build, I am still not getting sanitized input appearing in the textbox. I still don't know a solution for this.
[AcceptVerbs(HttpVerbs.Get | HttpVerbs.Post)]
public ActionResult MyAction(MyViewModel model)
if (this.ModelState.IsValid)
{
using (var service = new MyService())
{
model.MyResults = service.DoSomething(model.MySanitizedProperty);
}
}
return this.View("MyView", model);
Then, in "MyView":
@Html.TextBoxFor(m =&gt; m.MySanitizedProperty)
<input type="submit" value="Go" /&gt;
@Html.ValidationMessageFor(m =&gt; m.MySanitizedProperty)
In the controller, invoking model.MySanitizedProperty returns the sanitized value while the textbox goes on to display the unsanitized data.
Comment: No, it doesn't... even after defining everything into one model just now to see if that would help, it didn't.

Comment: I am sanitizing it in the model's setter. Then I use the getter in the controller to pass the sanitized value to a service. That much is working as desired, but the page is showing the unsanitized data in the textbox. I will add code to my question.

Comment: In ASP.NET MVC, when you specify an object as the parameter in the controller action, the submitted values are bound to the model's properties. That's where the MySanitizedProperty is being set. So if I navigated to `/MyController/MyAction?MySanitizedProperty=whatever`, the property is going to be set to 'whatever'. When I put 'whatever++++', that is, 'whatever' with some trailing whitespaces, the value is set to 'whatever'. I have verified that much in debug.

Comment: Oh, I see what you mean. Thanks

Comment: does using textboxfor make any difference?

Comment: so you are sanitizing it on the controller.  When you refresh the page  it is showing something different than the controller?

Comment: I am not sure what is going on in the do something method but from what you are showing MyResults is being set.  I don't see anything being set to MySanitizedProperty.  If you put MyResults into a TextBoxFor does it show the value you are wanting?

Comment: well aware.  Was expecting to see something like model.MySanitizedProperty = model.MySanitizedProperty.Trim().  good luck


Here is another answer: It sounds like a problem with the models; make sure you are properly accessing the value from the model you wish to populate it with, i.e., possibly discretely specifying the model "Name" is coming from.
Also, check to see that the setter has a chance to operate on the value - if the controller is activating before the setter function is used, then you'll only get the original input value.
Realize you have to go to the server for the setter to work, possibly you need a async postback or such, and the value reloaded.
Comment for this answer: I am posting the entire page. Within the controller action, the bound model's getter returns the sanitized input.

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 8, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2014:00:02/06/14', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<c#><asp.net-mvc-4>', 'Title': 'How to make Html.TextBox(string name) render sanitized input?', 'ViewCount': 608, 'id': 21610019, 'user_AboutMe': '', 'user_AccountId': 1020429.0, 'user_DisplayName': 'tuespetre', 'user_Reputation': 1807.0, 'user_Views': 277.0, 'user_id': 1030925.0}}