I am modifying an existing asp.net mvc application that creates a list of checkboxes from a list on the model, with property name "MyModelProperty" and additionally generates one more input element for "Select All" which has the following html:
<input name="MyModelProperty_SelectAll" type="checkbox" CHECKED="checked" value=""/>
What is the property declaration in the model that would create a boolean property that would bind to this existing element in the view?
I tried, 'public bool MyModelProperty_SelectAll {get;set;}' but it was returning null. Is that because the value is an empty string in the html input control?
Change your Model property for string:
public string MyModelProperty_SelectAll { get; set; }
Set some value for the checkbox, then in the server, if its checked, the value will be the given value, else you will see null.
<input name="MyModelProperty_SelectAll" type="checkbox" value="all"/>
EDIT:
If you want to bind this to a bool, you must provide a value="true" and a hidden field:
<input class="input-validation-error" id="MyModelProperty_SelectAll" name="MyModelProperty_SelectAll" type="checkbox" value="true">
<input name="MyModelProperty_SelectAll" type="hidden" value="false">
This example code above was generated using the Html.CheckBox helper.
If you don't use MVC htmlhelpers for generating checkboxes you must add an additional hidden element for your checkbox:
<input name="MyModelProperty_SelectAll" type="checkbox" value="true"/>
<input name="MyModelProperty_SelectAll" type="hidden" value="true"/>
Related
Is there a way to populate user input fields with default value in thymeleaf?
I understand that th:field replaces the value="" tag, but I need to populate the user inputs with default number so, that if the user does not provide input, number 0 will be passed as the input.
I cannot do this in controller as my input type needs to be number, and my model attribute is String[] arraylist.
<input type="number" id = "a2s" name="a2" class="newMatch" value="0" min="0" max="11" th:field="*{player1score}">
<input type="number" id = "b2s" name="b2" class="newMatch" value="0" min="0" max="11" th:field="*{player2score}" >
Try this way:
<input th:value="*{player1score != '' ? player1score : 0}" //...other attr />
th:field will override value, name and id attributes. To populate the field you will have to use the tags separately, like so:
<input type="number" id = "a2s" name="a2" class="newMatch" value="0" min="0" max="11" th:name="*{player1score}" th:id="*${playerscore}">
Solved this by using name and id html fields to replace the need for th:field="*{myVar}" . Like so:
<input type="number" class="newMatch" value="0" min="0" max="25" name="player1score" id="player1score" >
<input type="number" class="newMatch" value="0" min="0" max="25" name="player2score" id="player2score" >
The th:field tag replaces name , id and value fields. So one way to do this is to just use html tags instead.
How can I post a model list with gaps from a MVC webpage, and have it preserve those gaps when accessing it in server code?
For example, I want to post this:
<input type="hidden" name="ImageUrl.Index" value="0" />
<input type="url" name="ImageUrl[0]" value="example0.png" />
<!---->
<input type="hidden" name="ImageUrl.Index" value="2" />
<input type="url" name="ImageUrl[2]" value="example2.png" />
And receive this in the controller:
public ActionResult Save(Gallery model)
{
model.ImageUrl[0]; // "example0.png"
model.ImageUrl[1]; // null
model.ImageUrl[2]; // "example2.png"
}
I already noticed the "Index" hidden value helps send gaps correctly, but currently I'm receiving a list with 2 items, not 3. I would rather avoid declaring a dummy hidden value.
Given the following viewmodel:
public class FooViewModel
{
public bool IsBoolValue { get; set; }
}
and this view:
<input type="hidden" id="Whatever" data-something="#Model.IsBoolValue" value="#Model.IsBoolValue" />
The output of the hidden input field is this:
<input type="hidden" id="Whatever" data-something="True" value="value">
How come the value attribute is not set toTrue, but the data-something attribute is?
Is there a change in MVC 5 that would cause this, since in my MVC 4 apps this problem does not occur.
I think I've figured it out.
I believe the Razor viewengine is adhering to the HTML 5 way of setting boolean attributes, as described here:
What does it mean in HTML 5 when an attribute is a boolean attribute?
In HTML 5, a bool attribute is set like this:
<input readonly />
or
<input readonly="readonly" />
So the Razor viewengine takes your model's bool value and will render (in my case) the value attribute if Model.IsBoolValue is true. Otherwise, if it's false then the value attribute is not rendered at all.
EDIT:
As mentioned Zabavsky in the comments, to force the value of True or False to appear in the value attrbiute, simple use ToString():
<input type="hidden" value="#Model.BoolProperty.ToString()" />
<div class="field_warp_hidden">
<input type="checkbox" asp-for="ShowGoogleCaptcha" checked="#Model.ShowGoogleCaptcha" value="#Model.ShowGoogleCaptcha"/>
</div>
can set field_wrap_hidden is display:none;
checked and value must be set
I was using the hidden field in a partial view and I got an error when I used .ToString()
alternative option was to specify the value property explicitly even after specifying asp-for
<input type="hidden" value="#Model.TargetmarketsToChangeAvailability[i].Available" asp-for="TargetmarketsToChangeAvailability[i].Available" />
I have a class MyClass as below
public class MyClass {
private int fieldOne; //with getter, setter
private String fieldTwo; //with getter, setter
}
I have following two fields in my form bean
private MyClass myObject; //with getter, setter
private ArraList<MyClass>myList; //with getter, setter
Using struts1 I want to submit object and list of objects from web-page form.
In struts2 we can get object and list in action as below (considering fields are not in action class instead of form bean). When following fields are submitted, struts2 initiates myObject and myList for me with submitted values. (Parameters Interceptor is the magician behind the scene in struts2.)
<!-- Object -->
<input type="text" name="myObject.fieldOne" value="1" />
<input type="text" name="myObject.fieldTwo" value="Two" />
...
<!-- List -->
<input type="text" name="myList[0].fieldOne" value="1" />
<input type="text" name="myList[0].fieldTwo" value="Two" />
<input type="text" name="myList[1].fieldOne" value="10" />
<input type="text" name="myList[1].fieldTwo" value="Twenty" />
Is there any way to perform such magic in struts1?
AFAIR, the same would work in Struts1, provided the list in your form bean contains a list which already has the right size. I.e. if the last input text has the name myList[7].fieldOne, the list should be of size 8 at least, and thus be prepopulated with 8 MyClass instances. STruts will only do formBean.getMyList().get(7).setFieldOne(10).
See http://struts.apache.org/development/1.x/struts-taglib/indexedprops.html for more details.
I have some problems with ASP.NET MVC’s default model binder. The View contains HTML like this:
<input name="SubDTO[0].Id" value="1" type="checkbox">
<input name="SubDTO[1].Id" value="2" type="checkbox">
This is my simplified ‘model’:
public class SubDTO
{
public virtual string Id { get; set; }
}
public class DTO
{
public List<SubDTO> SubDTOs { get; set; }
public DTO()
{
SubDTOs = new List< SubDTO>();
}
}
All this works fine if the user selects at least the first checkbox (SubDTO[0].Id). The controller ‘receives’ a nicely initialised/bound DTO. However, if the first check box is not selected but only, for example, SubDTO[1].Id the object SubDTOs is null. Can someone please explain this ‘strange’ behaviour and how to overcome it? Thanks.
Best wishes,
Christian
PS:
The controller looks like this:
[Transaction]
[AcceptVerbs(HttpVerbs.Post)]
public RedirectToRouteResult Create(DTO DTO)
{
...
}
PPS:
My problem is that if I select checkbox SubDTO[0].Id, SubDTO[1].Id, SubDTO[2].Id SubDTOs is initialised. But if I just select checkbox SubDTO[1].Id, SubDTO[2].Id (NOT the first one!!!) SubDTOs remains null. I inspected the posted values (using firebug) and they are posted!!! This must be a bug in the default model binder or might be missing something.
This behavior is "by design" in html. If a check-box is checked its value is sent to the server, if it is not checked nothing is sent. That's why you get null in your action and you'll not find value in the posted form either. The way to workaround this is to add a hidden field with the same name and some value AFTER the check-box like this:
<input name="SubDTO[0].Id" value="true" type="checkbox">
<input name="SubDTO[0].Id" value="false" type="hidden">
<input name="SubDTO[1].Id" value="true" type="checkbox">
<input name="SubDTO[1].Id" value="false" type="hidden">
In this way if you check the check-box both values will be sent but the model binder will take only the first. If the check-box is not checked only the hidden field value will be sent and you\ll get it in the action instead of null.
I think this post on Scott Hanselman's blog will explain why. The relevant line is:
The index must be zero-based and unbroken. In the above example, because there was no people[2], we stop after Abraham Lincoln and don’t continue to Thomas Jefferson.
So, in your case because the first element is not returned (as explained by others as the default behaviour for checkboxes) the entire collection is not being initialized.
Change the markup as follows:
<input name="SubDTOs" value="<%= SubDTO[0].Id %>" type="checkbox">
<input name="SubDTOs" value="<%= SubDTO[1].Id %>" type="checkbox">
What's being returned by your original markup is an unrelated set of parameters, i.e. like calling RedirectToRouteResult Create(SubDTO[0].id, SubDTO[1].id, ..., SubDTO[n].id) which is clearly not what you want, you want an array returned into your DTO object so by giving all the checkboxes the same name the return value to your function will be an array of ids.
EDIT
Try this:
<input name="SubDTO[0].Id" value="<%= SubDTO[0].Id %>" type="checkbox">
<input name="SubDTO[0].Id" value="false" type="hidden">
<input name="SubDTO[1].Id" value="<%= SubDTO[1].Id %>" type="checkbox">
<input name="SubDTO[1].Id" value="false" type="hidden">
You have to return something to make sure there is an element for each index, I suspect that any gap will cause a problem so I'd suggest using a 'null' ID, for example 0 or -1 and then process that out later in your code. Another answer would be a custom model binder.
There is always the alternate option of adding a property to your class that takes an array of strings and creates the SubDTO array from that.
public List<string> SubDTOIds
{
get { return SubDTO.Select(s=>s.Id).ToList(); }
set
{
SubDTOs = new List< SubDTO>();
foreach (string id in value)
{
SubDTOs.Add(new SubDTO { Id = id });
}
}
}
or something like that