HtmlInputText Name property is not working and replace by ID - asp.net

I am creating a series of input text controls from asp.net codebehind. I am using the below code, but this code doesn't set the name attribute in input control instead ID is set to the name attribute of input control. How to make name attribute different from ID.
HtmlInputText sno = new HtmlInputText();
sno.ID = "txtSno" + j;
sno.Name = "txtSno-name";
sno.Value = snoList[i].ToString();
sno.Attributes.Add("class", "form-control");
cellsno.Controls.Add(sno);
This is the final html which is rendered
<input name="txtSno1" type="text" id="txtSno1" value="1" class="form-control">
Expected is
<input name="txtSno-name" type="text" id="txtSno1" value="1" class="form-control">

You can't change the name property of the input using the HtmlInputText object's Name property.As mentioned in Microsoft docs here
Use the Name property to determine the unique identifier name for an
HtmlInputControl. In this implementation, the get accessor returns the
value of the Control.UniqueID property. However, the set accessor does
not assign a value to this property.

Related

Input an enum value in a form as hidden using thymeleaf

I would like to fill an form input hidden using a value of an Enum:
Enum:
public enum TipoEndereco {
Residencial, Comercial
}
Summarizing the html code, i've tried to use the form input like this:
<input type="hidden" th:field="*{endereco[0].tipoEndereco}" th:value="Residencial"/>
But unfortunately, this field is being recorded as null. The others fields that don't use Enum in this way, are being recorded normal. I've used Enum to fill an option, but directly as input type hidden isn't working. I'm using thymeleaf.

RadioButtonList and their 'Value' property

I was wondering about the 'Value' property of an item in a RadioButtonList.
If I want to get the value of the selected Radio Button that is in the list, these must be unique or it will auto-select the first item it finds with the value I'm looking for. So no duplicate values.
Why is this? I've looked around the net, used them, and such so I know HOW it is working, but I would like to know WHY it works this way.
If I select an item in the list, it knows which item I selected. The button fills in, you can get the index of the item that is selected...so why doesn't it go: "Okay, you selected the item at index X. You also want the value? Okay, let me access the list, go to item X and get its value."
I can only think that when you want the value of an item, it is looking by value rather than by index then by value?
UPDATE 1:
In my particular case I was doing this:
I have 1 RadioButtonList that has 3 items(RadioButtons) in it. The following Select...Case occurs inside of a button click.
Select Case RadioButtonList1.SelectedItem.Text
Case "TextA"
Case "TextB"
//This RadioButton has a value of 200
Case "TextC"
//This RadioButton has a value of 200
End Select
This works for the first Case and only ONE other. As I debugged, the first and second cases went through fine. When I selected the third RadioButton, then execute the event, it will automatically select the second RadioButton because it has the same value as the third, but comes first in the list.
Changing one of the identical values fixes this.
Not sure I completely understand, Do you have your HTML, script to share? You might have a problem if you're using duplicate IDs to group the radio button set; you should just use same name attribute. In that case, if you try to get the value using selectElementById it will just grab the first one.
Since you "want to get the value of the selected Radio Button that is in the list", you can get that array of same-named inputs (getElementsByName) and loop through and look for the 'CHECKED' condition:
<input type="radio" name="cars" value="beamer" checked="checked">BMW<br><br>
<input type="radio" name="cars" value="prius">Prius<br><br>
<input type="radio" name="cars" value="volvo">Volvo<br><br>
<input type="radio" name="cars" value="beamer">BMW<br><br>
<input type="radio" name="cars" value="prius">Prius<br><br>
<input type="radio" name="cars" value="volvo">Volvo<br><br>
<p>Selected Car Value and Index:</p>
<div id="selectedCar"></div><br><br>
<button onclick="getVal()">Get Value and Index</button>
<script>
var getVal = function(){
var cars = document.getElementsByName('cars');
var result = document.getElementById("selectedCar");
for (var i = 0, length = cars.length; i < length; i++) {
if (cars[i].checked) {
result.innerHTML = "value: " + cars[i].value + " and index: " + i;
break;
}
}
}
</script>

Pass multiple check box values into Controller in ASP.NET MVC

I have an Enum list. I create these check boxes with the Enum list dynamically.
Here is the Enum list:
[LocalizableDescription(#"BankPos", typeof(Resources.DisplayNames))]
BankPos = 1,
[LocalizableDescription(#"PrarkingPlace", typeof(Resources.DisplayNames))]
PrarkingPlace = 2,
[LocalizableDescription(#"OutdoorPlace", typeof(Resources.DisplayNames))]
OutdoorPlace = 4`
I created the check box with foreach in Html tag .
Here are the check boxes:
<div class="checkbox-container">
<% foreach (var feature in Model.Features)
{%>
<label>
<input type="checkbox" name="q5" />
<span><%= feature.Text %></span>
</label>
<%}
%>
</div>
Up to here every thing is fine. My check boxes are create in correctly .
My problem is, how can I pass these check box values into controller?
As I said I want to pass them multiple.
Here is my controller
[HttpPost]
public ActionResult Review(ReviewMemberRatingViewModel model, HttpPostedFileBase picUpload)
{
return null;
}
You need to include the checkboxes in your model, and bind the values to them. So when you change a value on the model, it will be available in your action result. You could generate your inputs with numerical indexes or there is a great example of what you are doing while taking it a step further with an editor template here.

wrong data shows in combobox

I have a Kendo ComboBox.in mvc4.
and bind it to database.
my code:
#Html.Kendo().DropDownList().Name("id_team").BindTo(Model.tbl_team.Select(c=>new SelectListItem{Text = c.team, Value = c.id.ToString()}));
but below cods shows in my combobox:
Select Team:
<input data-val="true" data-val-number="The field id_team must be a number." id="id_team" name="id_team" type="text"/> <script> jQuery(function(){jQuery("#id_team").kendoDropDownList({"dataSource":[{"Text":"Barcelona","Value":"1"},{"Text":"manchester united","Value":"2"}],"dataTextField":"Text","dataValueField":"Value"});});
why?
I assume your problem is in empty drop-down list, and the most likely reason is in required js miss. Please make sure you have referenced all the scripts from Kendo introduction page: http://docs.kendoui.com/getting-started/using-kendo-with/aspnet-mvc/introduction

symfony setting value of specific checkbox widgets in form

I have a widget defined with a series of checkboxes that is essentially used to build up a bit flag. So I have a DB column 'access_bits', and I have several checkboxes generated on in the form class, in the format of 'access_bits[]', with the appropriate values (i.e. 1,2,16,1024 etc)
following the solution on this post (Symfony: How to change a form field attribute in an action?) - which is simple and sensible - I am able to magically set the attribute for all the checkboxes. But how can I target specific ones (preferably by value)?
TO CLARIFY:
HTML:
<input name="user[access_bit][]" value="1" /> Level 1
<input name="user[access_bit][]" value="2" /> Level 2
<input name="user[access_bit][]" value="4" /> Level 3
<input name="user[access_bit][]" value="8" /> Level 4
In the DB I have a column that has a bitwise integer. So for example, to have Level 1 and Level 2 checked, the value in the column would be 3. To have them all checked, the value would be 15.
"What I Want" PHP in action.class.php
$exampleValue = 4;
foreach ($form->getWidget('access_bit') as $thisWidget)
{
if ($thisWidget->getValue() == $exampleValue)
{
$thisWidget->setAttribute('checked','checked');
}
}
... which would cunningly set the checkbox next to Level 3 as ticked.
I do not want to use jQuery to do this.
you can try this....on your form save you can use this
$samplevar = $this->your_form->save();
$samplevar->setYOURFORMFIELDNAME(array of values(1,2,3));
$this->sample = YOURFORM($samplevar);
or,
on showing the form in first you can also pass the value in form while setting
$this->samplevariable = YOURFORM($this->getYOURMODEL->setYOURFORMFIELDNAME(array of values(1,2,3) which u get form db getYOURFORMFIELDNAME);
if you have values in comma seperated you cane use explode directly
$samplevar = $this->YOURFORMNAME->save();
$samplevar->setYOURFORMFIELDNAME(explode(',', $samplevar ->getYOURFORMFIELDNAME()));
$this->YOURFORMNAME = new YOURFORMForm($samplevar);
Try using the setDefault method :
$thisWidget->setDefault(array($examplevalue));

Resources