Isn't that possible to use "Ajax.BeginForm" in a collection model? Form repeats itself, Html elements are rendered correctly, values are ok. However, all parameters going through action are being null.
I've realized that, it posts like that:
ProductList[0].Id 501
ProductList[0].quantity 1
However it should be like
Id 501
quantity 1
Any Ideas?
You should use prefix in UpdateModel function:
UpdateModel(MyModel, "MyModel")
I solved by using pure html, thanks anyway.
<input type="hidden" value="#Model.Id" name="Id"/>
<input type="text" value="" name="quantity"/>
Related
Controller Coding
List<Map<String,Object>> rows=globalDao.list("select * from hosp_conf");
System.out.println(rows.get(0).get("hosp_id"));
model.addObject("list",rows);
return model;
View File
<input type="text" name="name" value="${rows.get(0).get("hosp_name")}" required>
I want to access my list element in spring form using specific index.
Above Sysout stament print excat value in console
How can I access it?
In jsp you can access a list index using the syntax -
${list[index]}.
In your jsp[, you need to use model attribute name that you have added at your controller.So instead of use rows you need to use list.
I don't know about the object in your list. But you can try like this -
<input type="text" name="name" value="${list[0]["hosp_name"]}" required>
Lets say I have an element like this:
<input id="options_14_text" type="text" onchange="something()" name="options[14]" value="">
How could I change its id to options_15_text for example using Prototype?
Thanks!
You can select the element and then change its id property:
$("options_14_text").id = "options_15_text";
Or you could use writeAttribute:
$("options_14_text").writeAttribute("id", "options_15_text");
Here's a working example. Inspect the DOM to see that the id value has changed.
I have a form with the autocomplete attribute set to off.
Inside this form, there is a hidden input element (generated using ASP.NET MVC's Html.HiddenFor() method, but, that should be irrelevant):
<input type="hidden" value="0" name="Status" id="Status" data-val-required="The Status field is required." data-val-number="The field Status must be a number." data-val="true">
When the form is submitted, this value is incremented by one and the model is returned to the view. If I write the status value to the page, I can see that the value was correctly incremented.
However, this hidden input field is always cached. It's never getting the correct value. I tried setting the autocomplete attribute directly on the input element, but without success.
How can I get this hidden field to get the correct value? I'd prefer not to use any Javascript.
Edit: Supplying more code...
Controller
//This code is being executed, and the status is being incremented.
shippingOrder.Status = (shippingOrder.Status != (int)OrderStatus.Closed) ? shippingOrder.Status + 1 : shippingOrder.Status;
View
#using (Html.BeginForm("Number", "Order", FormMethod.Post, new { id = "orderSummary", autocomplete = "off" })) {
...
#Html.HiddenFor(m => m.Status)
}
According to this post here html helpers such as HiddenFor will always first use the posted value and after that the value in the model.
As you said, when writing the value to the page you can see it incremented, yet the helper is using the previously posted value, which is the intended behaviour.
The link does suggest the use of ModelState.Remove("Status") or ModelState.Clear() before assigning the new value.
It also suggest that another option could be not using a HiddenFor helper but instead to build the hidden field yourself. Similar to this:
<input type="hidden" name="Status" value="#Model.Status" />
Either way it looks like your problem is based on similar circumstances.
As you can see here, I'm allowing a user to dynamically create a table of data, and storing the ids of the table in a hidden field (in the example it's a text area so you can see it, and the final solution will be Guid rather than integers).
My question is simply this: What data type should I use on the server/MVC action to take the data held in the textarea/hidden field?
At the moment I have a string, and am contemplating doing a load of .split()'ing and whatnot, but it doesn't feel right!
Ultimately I need some sort of IEnumerable<Guid, Guid> thing?!?! so I can do a foreach and get each pair of Ids.
I'm sure the answer will be simple, but I can't think of what to do.
Any help appreciated.
If your UI has multiple, like-named form fields, they will be submitted to your action method and bound properly to an array. We could use string[] for this case.
<form action="">
<input type="text" name="guids"/>
<input type="text" name="guids"/>
<input type="text" name="guids"/>
<input type="text" name="guids"/>
<input type="submit" value="Submit"/>
</form>
Then your controller could handle them like so:
public ActionResult MyAction(string[] guids)
{
guids.Count == 4 // if all four fields were filled in.
}
Note that if there is just a single guids value sent by the form, the string[] guids will still work - it will contain just a single item.
Finally, note that if no values are entered, the array value will be null, not an empty array.
You can actually bind to a list from your model, take a look at this post
http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx
i'm dynamically generating fields (from a hashmap):
<s:iterator value="app.fields" status="field">
<s:set name="findex" value="%{'app.fields[' + #field.index + '].id'}"/>
<s:fielderror value="%{#findex}"/>
<s:textfield name="%{#findex}" />
</s:iterator>
This method sets up the textfield ok:
<input type="text" id="saveapp_app_fields_1__id" value="[DE]Enter an ID" name="app.fields[1].id">
but not the fielderror.
I add the fielderrors manually in the validate method. but all field errors get displayed n times for each fielderror tag. wich implies that what is actually happening is that the #findex seems to evaluate to null and i'm adding n <fielderror/> tags.
I could extract the field errors manulally in the jsp but was hoping for a more elegant solution.
Thanks in advance. Michael.
I've never seen a fielderror declared in that way. Perhaps try:
<s:fielderror>
<s:param value="%{#findex}" />
</s:fielderror>