How to make Grails drop down readonly - grails

I had a requirement of making drop down read only in Grails gsp page. I had two gsp pages create and show. In create user select an element in drop down and submits the selection. In show.gsp, I would like to fetch and show the same element( element selected in create page ) and drop down must be readonly.
Example :
Create.gsp : drop-down 1 2 3
User selected 3
Show.gsp : drop-down must be readonly with value 3

You can disable the input, this does mean the value won't be submitted but if this is a read only view it shouldn't matter, if it does you could add a hidden field for the real value
<g:select name="mySelect" from="${[1,2,3]}" value="3" disabled="disabled" />

Related

ViewModel binding without SelectList in ASP.NET MVC

In my ASP.NET Core application, I have an ASP.NET MVC View which displays a list of products. (I guess this applies to other versions of MVC as well).
When I select an item from this list and submit the page, I want the selected product ID to be bound to a ViewModel property in the action method of the controller. I am using POST for this.
I do NOT want to use SelectList for rendering the list of products due to some formatting issues. Therefore I am looping over the list in my view.
How can I bind the selected product with the ProductId property of the inbound ViewModel object?
It's unclear what you mean by "select an item from this list and submit the page". Are you picking an item, potentially filling out more fields, and then submitting the whole shebang, or does picking an item constitute submitting the form. Depending on the situation there's a few approaches:
If picking an item submits the form, then you can simply make the "Select" button a submit button and give it a name and value. For example:
Item 1 <button type="submit" name="ProductId" value="1">Select</button>
Whichever "Select" button is clicked, then, will have its value posted as ProductId.
If you need to pick an item while remaining on the page to fill out other fields, then you can use radio buttons as an alternative to a select list. This is similar to approach above, except you will not instantly post the form. Your inputs would look similar though:
<label>
<input type="radio" name="ProductId" value="1"> Item 1
</label>
<label>
<input type="radio" name="ProductId" value="2"> Item 2
</label>
...
Finally, if you need to not instantly submit and you do not want to use radio buttons either, then your only real option is using JavaScript to set a hidden input. You would bind to the click event of your "Select" button or whatever and then set a hidden input with name="ProductId" to the appropriate value. Then, when you finally submit the form, the value of the hidden input will be posted as ProductId.

MVC6 select tag helper for edit applying current value

I have a viewmodel with a state dropdownlist.
The form is being populated from a database and I everything works except I dont know how to apply the current value for state as the default value for the select helper tag?
This is the CSHTML for the select:
<select asp-for="State" asp-items="#Model.StatesList" > </select>
This provides the all the states when you do a dropdown however is there a way to set the value to the current value for Model eg #Model.state so if you dont select any new dropdown value it takes the original value for the record?

MVC 5 - Returning an unordered list to the server, along with a form

I have an MVC 5 / Bootstrap application. On one of the pages, I have a number of fields all bound to the model associated with the page. However, I also have a simple unordered list which always starts out empty and the user can then add items to it. They do this by entering some text into a type ahead field. Once the user finds what he/she is looking for, he/she can click a button and have it added to the unordered list. Any number of items can be added to the list. All of this works fine.
My question is how I can get the contents of the unordered list posted back to the server along with the rest of the form contents, since the unordered list isn't part of the model?
Here is one way to skin this cat:
A) Add a collection to your model (which really should be a ViewModel, and not a domain model) to hold those items
B) In your button's click handler, create a hidden input field that conforms to the ASP.Net Wire Format: http://www.hanselman.com/blog/ASPNETWireFormatForModelBindingToArraysListsCollectionsDictionaries.aspx
If you had a collection of orders, you should end up generating controls like this:
<input type="hidden" name="Orders[0].Id" value="1" />
<input type="hidden" name="Orders[1].Id" value="2" />
Note sequential ordering is important, if you start removing items, you'll need to re-sequence your name values.
There couple of ways to work it out.
If you don't want to add to model, what I would prefer to do you can:
Directly access item that were posted via Controller.Request property;
You can post this items separately via Ajax request, and handle them in different controller action.

GSP- Select tag. How to achive selected="selected"

I have a select tag in my GSP file as
<g:select name="clientId" id="clientId" size = "4" from="${com.springcommunity.fleet.partymodel.roles.ClientRole.list()}" class = "filter_combo" optionKey="id" />
i want client with id 2 is selected initially (in simple html it is achived by using selected="selected")
how can i do it?
You need to specify the value attribute in this tag. http://grails.org/doc/2.0.x/ref/Tags/select.html
So in your example,
<g:select ... value="${com.springcommunity.fleet.partymodel.roles.ClientRole.get(2)}" />
One thing to be aware of here is that the value that you're selecting must be an object equal to the item in the list, and not just an id - this is where a lot of people get tripped up. So you can't just say value='2', you need to specify the object in the list that you have in your from attribute.
From the docs -
value (optional) - The current selected value that evaluates equals()
to true for one of the elements in the from list.

ASP.NET MVC how to handle the default empty value for dropdown list

I create a dropdown list by using ASP.NET MVC helper like this,
<%=Html.DropDownList("Group","-please select a group-")%>
and I get the html like this,
<label for="GroupId">Group:</label>
<select id="GroupId" name="GroupId"><option value="">-please select a group-</option>
<option value="15">Business</option>
<option value="16">Friends</option>
<option value="17">Others</option>
</select>
The default option is "-please selecta group-" and the value is empty.
How can I validate the select value to see if it's empty?
I mean if user doesn't choose a group, how can I know it and give user a error message.
Now, the code only shows exception error, because the value is empty for the default option.
The value of Group in the Action will be 0.
You could test if Group is zero and call ModelState.AddModelError.
It would be better, if it was possible to explicitly set the value of the default item in the dropdownlist, but that is not possible in the Html.DropDownList
Anyway I use the overload:
With that you have one Property in the Model for the list of Groups and one for the key of the selected Group. If I use
then ModelState.IsValid is always false for me.

Resources