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.
Related
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];
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.
my model -
public class Model
{
public String SelectedValue;
}
action method -
public ActionResult Index()
{
Model model = new Model();
model.SelectedValue = "a";
return View("Index1", model);
}
view -
<input type="radio" name="Group1" value="a" data-bind="attr: { checked: SelectedValue }" />
<input type="radio" name="Group1" value="b" data-bind="attr: { checked: SelectedValue }" />
<input type="radio" name="Group1" value="c" data-bind="attr: { checked: SelectedValue }" />
<script type="text/javascript">
var data = #Html.Raw(new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(Model));
BindData();
</script>
My javascript -
function BindData() {
viewModelData = ko.mapping.fromJS(data);
ko.applyBindings(viewModelData);
}
When my view is rendered, it renders all three radio buttons with the one with value = "c" selected. But from action method i am selecting radio button with value "a". why is it so?
Can anyone please help me understanding whether i have any issue with my code or how does knockout handle radio button selections internally. Thanks!
Use the checked binding instead of the attr binding which is directly created for this scenario:
The checked binding links a checkable form control — i.e., a checkbox (<input type='checkbox'>) or a radio button (<input type='radio'>) — with a property on your view model.
So in your case you need to write:
<input type="radio" name="Group1" value="a" data-bind="checked: SelectedValue" />
<input type="radio" name="Group1" value="b" data-bind="checked: SelectedValue" />
<input type="radio" name="Group1" value="c" data-bind="checked: SelectedValue" />
Demo JSFiddle.
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]);
}
}
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