I have a primitive view: a form containing a validatable text input component and a command button. The input value is pointing to a session-scoped backing bean.
I open the page, enter an invalid value and submit the form: after the postback, a validation error is appearing and the input component is displaying the submitted value which did not pass validation. The model value in my session-scoped bean is left intact, as expected.
Ok, now I open another tab in the browser and open the same page. To my surprise, the input component is displaying the submitted value from the first tab. I've been supposing the view state to be new at another GET request and the plain model value from my session-scoped bean to be shown instead.
If I use a view-scoped bean instead of a session-scoped one the model value is being rendered for the input component in the second tab, not the submitted value from the first tab.
Is the submitted value not the part of the view state and kept somehow along with the model? Or is its scope tuned up in some smart way depending on the referenced bean's scope?
Sorry in advance if this question is stupid but I will be very grateful for removing my misunderstanding.
Related
I have an dot net 5.0 mvc page which takes a model object (ClassFoo) which when constructed generates an instance of (classBar). ClassBar has various properties (FieldA, FieldB, FieldC) all are strings.
The Views content is all in a form and the form's input fields are all from ClassFoo.ClassBar's various properties. Initially, when the page is accessed, all form input values are empty. However when I put data into them and then submit the form, the form values are still there when the page loads. However I don't understand why this is because I'm explicitly creating a new model during the controller operation but I am not actually populating the Model.ClassBar with the content from the post before I return model to the View for generation.
What I would expect is that all of the form fields would be empty however that is not the case. I know if asp.net the form values are stored and restored automatically but I didn't think that happened in mvc.
After looking into ModelState recommended by Nick Albrech in the comments I reviewed the hint associated w/ the HtmlHelper.TextBoxFor() which states the following:
... Adds a "value" attribute to the element containing the first non-null value found in: the ActionContext.ModelState entry with full name, or the expression evaluated against ViewDataDictionary.Model. See [IHtmlHelper.NameFor] for more information about a "full name".
So effectively what's happening is similar to what I thought asp.net mvc wasn't doing in that it populates the ModelState from a get/post request with the name and values of the form being submitted. Then based on the use of these helper functions (and also asp-for attributes used in razor views views), it either provides values from the saved model state form values OR the model passed to the view. Note: this does not seem to work if you set the value of an input element = #Model.[someProperty]
The take away from this is that you do not necessarily need to update your model object with content from the previous form submit in order to have the page populate the form contents back to the screen. Allow asp.net mvc to do the manual tasks by making use of these razor helpers.
Shoutout to Nick for the assist on this one. A solid member of the stackOverflow community.
On a .cshtml page I have a single textbox inside a form. When I enter some text and press return it triggers a POST to a controller of the same name (different signature). The code processes the text and a model is created and passed back to the same page where in addition to the original textbox a grid is now populated (conditionally). Everything works, except the text in the textbox is retained.
This is a little odd, considering that most of the questions on here are about retaining the text after a POST and the indication has been that the text should not automatically be retained.
I can probably assign the textbox an empty string but I am simply wondering if I have this wrong?
Even if it was a simple textbox, html textbox is a input control that has two very important properties, i.e.
name
value
browser will send this name value pair to server
and the default behavior of server is to return the collection of all pairs back to browser when its done whatever it was supposed to do.
this collection is called post data. (or sometimes form data)
Yes, you could clear the textbox in code if everything was fine on postback
If something was wrong (exception or validation) you could do nothing ( as you do now) and text will be there again, what's makes sense in the UI
.
I am new to JSF. I have 3 cascading SelectOneMenu i.e Countries, States and Cities. The functionlity is when country is selected corressponding states are populated and when state is selected cities are populated.
I am using a4j:support tag for implementing it. My managed bean is in request scope and is spring managed beans.
I have written ActionListener for CountriesChanged(), StatesChanged() and CitiesChanged().
In my application when one selects countries, action event is fired and states are populated.
However, when one changes states, it throws "Validation Error : Value is not valid"
It would be helpful if somebody guides me proper direction.
You need to prepare exactly the same list of states during the form submit request as it was during the form display request. JSF will namely re-validate the submitted value against the list of available items. In case of a request scoped bean, you have to prepare it in the bean's (post)constructor. Another way is to put the bean in the new JSF 2.0 view scope so that the bean don't get recreated whenever you submit the form against the same view.
I am using JSF 2.0/CDI and PrimeFaces 2.2.1 for a number of CRUD forms that let the user view or update the attributes of an existing entity by clicking on a link in a datatable, where the identifier of the entity is passed to the CRUD form as a View Parameter. I display the entity's ID (often just an integer) on the CRUD form in a PrimeFaces InputText field with the readonly attribute set to true (since I can't let them change it), so the user knows which entity they're editing. The backing bean of the CRUD form is RequestScoped, which works fine except when validation fails. In that case, the value of the View Parameter is lost, so a 0 is displayed in the entity ID field on validation failure.
I am able to maintain the actual entity ID in a hidden field so it's available to update the database once validation succeeds, but it's rather maddening that I've not been able to find a way to maintain the value in a visible field of some sort after a validation failure. Ideally the InputText field would retain its functionality as an inputted and validated field even with its readonly (or disabled) attribute set to true, which would let me forgo the hidden field entirely. But it doesn't appear that I can make it work that way. Any suggestions besides making the backing bean ConversationScoped, which I'd prefer to avoid?
Actually, after stating what I'm looking for a little differently in a Google search I found a novel suggestion at the link below that seems to work cleanly. Instead of making the entity ID field readonly or disabled, I leave it enabled but blur it as soon as it receives focus. I'm able to get rid of the hidden field, the user can't change the value and it survives a validation failure.
<p:inputText id="entid" value="#{RequestBean.entityID}" onfocus="blur();" />
http://www.codingforums.com/archive/index.php/t-1738.html
I understand that fields such as Html.TextBox() accept two values, the first one being the name and the second one being the value. And so does Html.TextArea(). But in a case where the form is submitted as AJAX and the div where the form is placed is replaced with a view from the server, the form fields insist on taking the previous values. An image is worth a thousand words:
image http://img132.imageshack.us/img132/4171/aspnetmvcbug.png
I've checked everything on the controller and the model and the image is from debugging the view itself. The model is empty but the fields generated from it take the value of the previous submission.
The postback data is held in the ModelState. The built in HtmlHelper methods will look for values stored in the model state based on the name of the form element when rendering their content.
Check the View.ModelState property. Forms can grab values from there in certain circumstances.
Do you have an entry ViewData["Body"]? MVC will also attempt to bind a control to a ViewData item based on the name.