I’ve been working with WebMatrix lately and I’ve been wondering if I can inject data into objects.
I used to work with Struts2 and when if you’re familiar with it, if you had an input like <s:textfield name="model.property" .../> the value inserted into that textfield would be injected into the model object or the value in the model object would be injected into the textfield when the page is loaded (and that depends on the logic handled into the web page).
So far in WebMatrix, I’ve seen this type of data injection value="@Request.QueryString["searchGenre"]" where you can access the value from a variable inside Razor code and asign it to a html control, but can it be done in reverse? Can I place a reference to an object in Razor code inside a html control and inject a value into the object when I submit the page?
I know I can access to the query string values inside the request, but I want to know if this approach can be done.
So can it be done? Or there’s a different approach I can take? I want to agilize my coding time so I’m open to suggestions.
Thanks for any help.
Yes, you can provide any default value you like to an HTML form control. The convention is to set the value to the corresponding Request.Form or QueryString value so that if the form fails validation, the values provided by the user are persisted:
However, you could set that to a default value, and reset it in the event that the form is posted so that you pick up whatever the user provided:
Or, if you want to use the Html Helpers for forms for a more “agile” feel: