I'm trying to use a Checkboxlist in MonoRail to represent a many to many table relationship. There is a Special table, SpecialTag table, and then a SpecialTagging table which is the many to many mapping table between Special and SpecialTag.
Here is an excerpt from the Special model class:
[HasAndBelongsToMany(typeof(SpecialTag),
Table = "SpecialTagging", ColumnKey = "SpecialId", ColumnRef = "SpecialTagId")]
public IList<SpecialTag> Tags { get; set; }
And then in my add/edit special view:
$Form.LabelFor("special.Tags", "Tags")<br/>
#set($items = $FormHelper.CreateCheckboxList("special.Tags", $specialTags))
#foreach($specialTag in $items)
$items.Item("$specialTag.Id") $Form.LabelFor("$specialTag.Id", $specialTag.Name)
#end
The checkboxlist renders correctly, but if I select some and then click Save, it doesn't save the special/tag associations to the SpecialTagging table (the entity passed to the Save controller action has an empty Tags list.) One thing I noticed was that the name and value attributes on the checkboxes are funky:
<label for="special_Tags">Tags</label><br>
<input id="3" name="special.Tags[0]" value="UCampus.Core.Models.SpecialTag" type="checkbox"> <label for="3">Buy 1 Get 1 Free</label>
<input id="1" name="special.Tags[1]" value="UCampus.Core.Models.SpecialTag" type="checkbox"> <label for="1">Free</label>
<input id="2" name="special.Tags[2]" value="UCampus.Core.Models.SpecialTag" type="checkbox"> <label for="2">Half Price</label>
<input id="5" name="special.Tags[3]" value="UCampus.Core.Models.SpecialTag" type="checkbox"> <label for="5">Live Music</label>
<input id="4" name="special.Tags[4]" value="UCampus.Core.Models.SpecialTag" type="checkbox"> <label for="4">Outdoor Seating</label>
Anyone have any ideas?
Thanks!
Justin
The checkboxlist renders correctly
it seems to me that you could also render something like
<input type="checkbox" name="special.Tags" value="1"/>
<input type="checkbox" name="special.Tags" value="2"/>
which make it simpler (no index to output for the name, it will be properly resolved as an array via controller action parameter binding
also, in your sample, the fact that all checkboxes having the same value UCampus.Core.Models.SpecialTag is probably not right, you may want to output actual primary key identifier from the tags (not sure, could you display the class you are binding back on the form handling action?)
I was able to get it working by specifying the id and text attributes...
$Form.LabelFor("special.Tags", "Tags")<br/>
#set($items = $FormHelper.CreateCheckboxList("special.Tags", $specialTags, "%{value='Id', text='Name'}"))
#foreach($specialTag in $items)
$items.Item("$specialTag.Id") $Form.LabelFor("$specialTag.Id", $specialTag.Name)
#end
Related
Here are the codes:
<EditForm OnValidSubmit="#SubmitText" id="inputText">
<InputText #bind-Value="_InputMsgModel.Msg" />
</EditForm>
After the program ran, it turned out to be this:
<form id="inputText">
<input class="valid">
</form>
Now I wanna add an attribute type="text" to the input element, how can I achieve this?
I tried to modify the code like this:
<EditForm OnValidSubmit="#SubmitText" id="inputText">
<input type="text" #bind-Value="_InputMsgModel.Msg" />
</EditForm>
Meanwhile, now visual studio reports an error:
I can not bind the model anymore.
I need to set the type to text for needing to set the keyboard in mobile correctly.
How can I solve this? Thank you.
What is wrong with this code:
<EditForm Model="#_InputMsgModel" OnValidSubmit="#SubmitText" id="inputText" >
<InputText #bind-Value="#_InputMsgModel.Msg" />
</EditForm>
Run this code with the above:
#code {
InputMsgModel _InputMsgModel = new InputMsgModel();
private void SubmitText()
{
Console.WriteLine(_InputMsgModel.Msg);
}
public class InputMsgModel
{
public string Msg { get; set; } = "My new message";
}
}
Do you see the text "My new message" in the text box ? I believe you do... All is well, and the two-way binding mechanism works well. Go and see now the Html...it's still <input class="valid"> which does not reflect the real state of the text box. Think about it...
Update: Of course you can use the following:
<EditForm Model="#_InputMsgModel" OnValidSubmit="#SubmitText" id="inputText" >
<input type="text" #bind-value="#_InputMsgModel.Msg" />
</EditForm>
Important: The error "The attribute names could not..." is triggered because you use capital "V" in #bind-Value. You should use lower case: #bind-value. This is because your using input 'Html element' here, and it has a value attribute, not a Value attribute. But when you use the InputText Component, the capital Value in #bind-Value refers to a Value property defined in the component.
I have created a search query to filter records from the database. Whenever I enter the value I want to filter for eg the email as a criteria and click on search, it retrieves the record from the database, if I click on search again instead of adding the new search result it repopulate the list with the new records and keeping the old record in view.
editted
this is adminList initialization
List<Admin> adminList = new ArrayList<Admin>();
this is the query I am using to search
#SuppressWarnings("unchecked")
public List<Admin> getAllAdmins(String singleAdmin) {
try{
String query = "SELECT DISTINCT e.* FROM admin e WHERE e.email like '%"+ singleAdmin +"%'";
List<Object[]> adminObjects = fetchAll(query);
for(Object[] adminObject: adminObjects) {
Admin adminIndividual = new Admin();
Integer id = ((Integer) adminObject[0]);
String email = (String) adminObject[1];
String name = (String) adminObject[2];
adminIndividual.setId(id);
adminIndividual.setEmail(email);
adminIndividual.setName(name);
adminList.add(adminIndividual);
}
System.out.println("database values for admin>>>>> "+adminList);
return adminList;
}catch(Exception e){
e.printStackTrace();
return null;
}
}
this is the snippet of code in my .jsp file that send the data to the controller to make a search for the query criteria
<form method="post" action="searchAdmin">
<p><label for="name">Admin Email</label>
<input class="form-control" type='text' name='searchName' id='searchName'/>
<input class="btn btn-success" type='submit' value='RETRIEVE'/>
</p>
this is how I am populating the list that gets the values whenever a new search is made
<c:forEach items="${adminList}" var="admin">
<p>
<label for="name">FULLNAME</label>
<input type="text" name="name" placeholder="" value="${admin.name}" readonly/>
</p>
<p>
<label for="email">Email</label>
<input type="text" name="email" placeholder="" value="${admin.email}" readonly/>
</p>
</c:forEach>
you can see the fullname repeating twice and if I enter an email again to click on search, it doubles to three retaining the two previous records. Please how do I tackle this issue whenever I try to search, as the page refreshes let is populates the search query with out repeating with the previous search query. Kindly assist!
Weird this one.
On my .NET MVC 4 project I've added a file on App_Code who contains this method:
#helper CheckBox(string name, bool isChecked = false, string className = "") {
<div class="checkboxHolder">
<input id="#name" name="#name" type="hidden" value="#isChecked") />
<i class="#className checkboxBts fa #((isChecked) ? "fa-check-square-o" : "fa-square-o")" data-checkbox-associated="#name"></i>
</div>
}
I'm using it to style checkboxes using font-awesome, so my app checkboxes are made of an input type hidden who stores a boolean value and an icon to give feedback to users.
Weird thing is, on executing when isChecked == false, the hidden returned by this method is like:
<input id="myCheckboxId" name="myCheckboxId" type="hidden" />
There is no value at all, when I try to save it to the model an exception is thrown saying that model cannot be saved.
I've fixed it changing the method to use:
<input id="#name" name="#name" type="hidden" #((isChecked) ? "value=true" : "value=false") />
Which is working fine. However, I wonder if anyone know what could be happening on the original output.
Thank you all.
It's not entirely a duplicate, but this is answered in Why is my hidden input writing: value=“value” instead of true/false?:
if you have:
<input name="somefield" type="hidden" someprop="#(SomeBooleanExpression)"/>
[and #SomeBooleanExpression] is false it is omitted completely:
<input name="somefield" type="hidden"/>
To get around this, consider .ToString()
So, use:
<input id="#name" name="#name" type="hidden" value="value="#(isChecked.ToString())" />
I have the following partial view binded to a view model.
#model Omega.UI.WebMvc.Models.InputWithLabelViewModelBase
#{
string required = null;
}
#if (Model.IsRequired)
{
{
required = "required";
}
}
<td><label id="#Model.LabelId">#Model.LabelText</label></td>
<td>
<input id="#Model.InputId" data-bind= "value: #Model.CurrentValueProperty" required="#required"/>
<span class="k-invalid-msg" data-for="#Model.InputId"></span>
</td>
When IsRequired is true I want the required attribute to be available, otherwise not. When I run that code and IsRequired is false the required attribute is presented with empty value: required="". I am quite new to asp mvc and I do not see what am I doing wrong.
UPDATE per dove`s post:
This does not work:
#{
bool required = Model.IsRequired;
}
<td><label id="#Model.LabelId">#Model.LabelText</label></td>
<td>
<input id="#Model.InputId" data-bind= "value: #Model.CurrentValueProperty" required="#required" />
<span class="k-invalid-msg" data-for="#Model.InputId"></span>
</td>
This renders the following html:
required="False" (if IsRequired is false and vice versa)
which is incorrect:
from the html documentation:
The required attribute is a boolean attribute, and can be set in the following ways:
<input required>
<input required="required">
<input required="">
Note: I am using Razor 2.0
Am I doing something wrong?
Mvc takes works with booleans in these special cases (assuming required works like checked). Try this:
required="#Model.IsRequired"
Can you also check your project references to confirm you are using Razor 2 as your example above should work also. Right click on System.Web.Razor view the properties and see what Version it is. It should be 2.0.0.0
A list is passed to the display:table. In the table i need to have a checkbox column to select/deselect any transactions that are selected. How do i associate the checked item with the transaction. Also i need to pass this checked transaction to a java script.
Thanks
Jsp code:
<display:column title="<input type='checkbox' name='selectall' onClick='selectAll()' />" media="html">
<input type="checkbox" name="selectBox" class="selectableCheckbox" id="selectBox" value="${sample.txniD}"></>
</display:column>
<display:column property="txniD" sortable="true" scope="all" title="Transaction ID %>" />
Try alerting the value of the checkbox, alert(checkboxArray[i].value);, not toString.