Handle HTML array input elements - asp.net

I know in PHP we can easily create something like this to handle HTML array elements:
<input name="test[0]" value="1" />
<input name="test[1]" value="2" />
<input name="test[2]" value="3" />
Then in code I can access this as an array:
$arrElements = $_POST['test'];
echo $arrElements[0]; // prints: 1
I have been trying to do the same in ASP.NET (Web Applications). But unfortunately this (also) doesn't work that easy.
So if i use the same HTML as above, i then tried to do the following in my CodeBehind:
var test = Request.Form.GetValues("test");
But this results in test being null. Is there anyway to handle HTML array elements like i can with PHP?

Change your html :
<input name="test" value="1" />
<input name="test" value="2" />
<input name="test" value="3" />
code behind :
var test = Request.Form.GetValues("test");
And now you will get array of values in test var.
If you can't change html you can use this code :
var testList = new List<string>();
foreach (string key in Request.Form.AllKeys)
{
if (key.StartsWith("test[") && key.EndsWith("]"))
{
testList.Add(Request.Form[key]);
}
}

Related

How to get which radio button is selected in symfony 3?

I have these inputs in my twig file :
<input type="text" name="txtNom" id="txtNom" value="{{user.nom}}" />
<input type="text" name="txtPrenom" id="txtPrenom" value="{{user.prenom}}" />
<input type="radio" name="rbSexe" id="rbHomme" onclick="changeGender(this.id);" />
<input type="radio" name="rbSexe" id="rbFemme" onclick="changeGender(this.id);" />
So, for calling those inputs in my Controller, I use the name attribute, for the first two it's okay :
$utilisateur->setNom($request->get('txtNom'));
$utilisateur->setPrenom($request->get('txtPrenom'));
but those with radio type have the same name, so how can I call specific one of them?
$utilisateur->setSexe(?????????);
I solved the problem :
I give the inputs a value, and make the name looks like an array:
<input type="radio" name="rbSexe[]" value="Homme" id="rbHomme" onclick="changeGender(this.id);" />
<input type="radio" name="rbSexe[]" value="Femme" id="rbFemme" onclick="changeGender(this.id);" />
and for call it in Controller, I use this :
$s = $request->get('rbSexe',0)[0];

How to handle multiple checkboxes in ASP.NET WEb Form and Code-Behind File

Here is the Code in the .aspx web form What is the best way to handle a group of multiple checkboxes.
<input id="nonunionexempt" type="checkbox" value="0" name="employeeType" tabindex="8" runat="server" />
<input id="nonexempthourly" type="checkbox" value="1" name="employeeType" tabindex="9" />
<input id="eleven99" type="checkbox" value="2" name="employeeType" tabindex="10" />
<input id="nysna" type="checkbox" value="3" name="employeeType" tabindex="11" />
<input id="cir" type="checkbox" value="4" name="employeeType" tabindex="12" />
Here is the code behind file Is there a better way to deal with multiple checkboxes?
protected void SaveEmployee()
{
Employee model = new Employee();
if (nonunionexempt.Checked)
{
model.EmployeeType = nonunionexempt.Value;
}
if (nonunionexempt.Checked)
{
model.EmployeeType = nonexempthourly.Value;
}
IValueProvider provider = new FormValueProvider(ModelBindingExecutionContext);
if (TryUpdateModel<Employee>(model, provider))
{
LoaRepository.saveData(model);
}
else
{
throw new FormatException("Could not model bind");
}
}
First, your code sounds good and clean. But if I were you I would use web controls instead of HTML ones. Also, I don't think there is a need to write the if statements even if not using web controls. Simply assign the 'checked' property which is of value true or false. Finally, if embedding the check boxes in a web user control then in the case of multiple usages this could bring a valuable help and of course better maintenance. Hope it helps, good luck!
If you can use the ASP.NET Checkbox control instead of input tags, you won't need the if statements. In code behind use checkBoxID.Checked property which will return true or false.

How to change the content of ViewResult

bool isChecked = false;
<input type="checkbox" name="x" checked="#isChecked" />
In MVC 4, The above code will be generate as
<input type="checkbox" name="x" />
But in MVC 3,Need to write like this:
bool isChecked = false;
#if(isChecked)
{
<input type="checkbox" name="x" checked="checked" />
}
else
{
<input type="checkbox" name="x" />
}
If we are Microsoft developers, Which assembly need to modify and how to modify it?
How to customize the upgrade code?
Plase help me,thanks!
To be honest I don't really understand the question after those code blocks, but I can say that you can use inline condition in your views in ASP.NET MVC3. Something like that for example:
bool isChecked = false;
<input type="checkbox" name="x" #(isChecked ? "checked=checked" : "") />
It's shorter and it will produce code like that:
<input type="checkbox" name="x">
And BTW, there is a helper method Html.CheckBox to create checkbox in your view and in second parameter you can indicate if you want it to be checked:
#{bool isChecked = false;}
#Html.CheckBox("x", isChecked)
And that will rendrer this:
<input id="x" type="checkbox" value="true" name="x">
<input type="hidden" value="false" name="x">
Try it on your own.

How can I add additional fields to a form and still use MVC?

I would like to display a form that lets a user enter up to 10 rows of information. If they need to go over, I'm going to use an "add additional row" button that will add one row at a time. What will my Model class look like for something like this? When I use javascript to add a new row, how can I tie that new row into the Model as well?
This article from Phil Haack shows you how to bind to collections. You'll need to use the javascript to create the new row with the correct names.
Probably this rows contains related values, so you can give the same name to all theses inputs in the html, and declare that you action receive an array of values.
Assume that you have this
<form method="post" action="/Controller/YourAction">
<input type="text" name="row" value="1" />
<input type="text" name="row" value="2" />
<input type="text" name="row" value="3" />
<input type="text" name="row" value="4" />
<input type="text" name="row" value="5" />
<input type="text" name="row" value="6" />
<input type="submit" />
</form>
all that you need to do is declare this inside your Controller
public ActionResult YourAction(int[] row)
{
//put your code here
}
and you will have all the values inside the row array
You may take a look at the following blog post which explains how to achieve exactly that. It uses a custom helper (Html.BeginCollectionItem) which allows to use non sequential as collection indexes instead of numbers which makes adding/deleting new items much easier.

Binding arrays with missing elements in asp.net mvc

I am trying to bind a dynamic array of elements to a view model where there might be missing indexes in the html
e.g. with the view model
class FooViewModel
{
public List<BarViewModel> Bars { get; set; }
}
class BarViewModel
{
public string Something { get; set; }
}
and the html
<input type="text" name="Bars[1].Something" value="a" />
<input type="text" name="Bars[3].Something" value="b" />
<input type="text" name="Bars[6].Something" value="c" />
at the moment, bars will just be null. how could I get the model binder to ignore any missing elements? i.e. the above would bind to:
FooViewModel
{
Bars
{
BarViewModel { Something = "a" },
BarViewModel { Something = "b" },
BarViewModel { Something = "c" }
}
}
Add the .Index as your first hidden input to deal with out of sequence elements as explained in this Phil Haacked blog post:
<input type="text" name="Bars.Index" value="" />
<input type="text" name="Bars[1].Something" value="a" />
<input type="text" name="Bars[3].Something" value="b" />
<input type="text" name="Bars[6].Something" value="c" />
A possible workaround could be to instantiate the ViewModel and the collection to the correct size (assuming it's known), then update it with TryUpdateModel... something like:
[HttpPost]
public ActionResult SomePostBack(FormCollection form)
{
// you could either look in the formcollection to get this, or retrieve it from the users' settings etc.
int collectionSize = 6;
FooViewModel bars = new FooViewModel();
bars.Bars = new List<BarViewModel>(collectionSize);
TryUpdateModel(bars, form.ToValueProvider());
return View(bars);
}H
MVC is able to populate list itself.
public ActionResult Index(FooViewModel model)
{
...
So no matter if anything is missing mvc will create new List<BarViewModel> and
for each found index - [1],[3],[6] it will create new BarViewModel and add it to List. So you will get FooViewModel with populated Bars.
i didnt know even that worked!
bearing that in mind, id have done something like:
<input type="text" name="Bars.Something" value="a" />
<input type="hidden" name="Bars.Something" value="" />
<input type="text" name="Bars.Something" value="b" />
<input type="hidden" name="Bars.Something" value="" />
<input type="hidden" name="Bars.Something" value="" />
<input type="text" name="Bars.Something" value="c" />
which would hopefully post
a,,b,,,c
but I suspect that will bind in the same way as you describe
Youre probably going to have write a custom model binder that looks for the max index, makes a list of that size then puts the elements in the correct place.
Saying all that, wait for someone else to post a really simple attribute you can put on your property that makes it just work ;D

Resources