Changing the id of an element using Prototype? - prototypejs

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.

Related

List element access in spring form using index

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>

Ajax.BeginForm with Collection

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"/>

Firefox is caching hidden inputs even with autocomplete turned off

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.

Posting an array of Guid pairs to an Action

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

MVC3 get Select Box Option Name as Well as Value

<select id="selectedSchool" size="3" multiple="multiple" name="selectedSchool">
#foreach (var item in ViewBag.pt)
{
<OPTION VALUE="#item.entityID">#item.name</OPTION>
}
</select>
on my page the user selects a school from the list of schools, each school has a unique ID which is needed and passed via the VALUE attribute, I also would like to pick up the name
(item.name) that was selected how can I resolve this info from my control Action Result?
public ActionResult ResultsSchoolsAttended(List<int> selectedSchool)
i iterate through the list of results, but I would aslo like selectedSchool.name to enter back in the db as 'display text'
how can i do this?
Well, MVC can only give you what is in the HTTP POST. And if you look at that, you will see that the <option> inner HTML isn't there. So you have only two choices:
Include it in the value in your markup, or
Look it up in your ResultsSchoolsAttended action based on the submitted value.
I'd pick the second, personally.

Resources