Reputation:
I have an @html.editorfor
and an @html.hiddenfor
element in my.cshtml
view, and on a button click event I'm changing both of the elements' name attributes so that I can post them in a separate list.
The value of the editorfor
element is supposed to be a string in the model, but the value of the hiddenfor
element is supposed to be an int.
The problem is that the new list is returning with the right editor value but the hidden value is always returning "0" even though it should be something else. Is there a different way of posting an int value back in a list?
by the way, when I'm posting the values of the elements back without changing their name attributes, everything is posting back correctly.
heres the script that I'm using to change the name attributes:
//inside for loop where i is the iteration index
@Html.HiddenFor(m => m[i].EmployeeTimeSeriesDataID, new { htmlAttributes = new { @id = "tsdata_hidden_" + i } })
<script>
$('somebutton').click(function() {
var hiddenTsID = "#tsdata_hidden_" + aNumber; //number to match the i from the hidden for
var nameOfHiddenToDeleteWithIndex = "deleteList[" + deleteIndex + "].EmployeeTimeSeriesDataID";
//deleteindex starts at 0 and is incremented at the end of every button click, so the new list
//can be correctly created starting with index[0]
$(hiddenTsID).attr('name', nameOfHiddenToDeleteWithIndex);
});
</script>
controller action:
public ActionResult TimeSeriesData(/*among other params*/EmployeeTimeSeriesData[] deleteList) {
//working update functionality
//working add functionality
foreach (EmployeeTimeSeriesData tsData in deleteList)
{
EmployeeTimeSeriesData y = db.EmployeeTimeSeriesData.Find(tsData.EmployeeTimeSeriesDataID);
db.EmployeeTimeSeriesData.Remove(y);
}
db.SaveChanges();
}
Upvotes: 1
Views: 306
Reputation:
apparently theres something wrong with the html hiddenfor helper. When I was trying to retrieve the value it was returning blank values, for example:
confirm($('tsdata_hidden_0').attr('value'))
this was opening up a confirmation box with an empty message, the 'value'
wasn't showing up. Same thing happened when I tried the same thing but with a different attribute (instead of 'value'
I tried 'name'
, 'id'
and they all were blank.)
So then what I did was make a regular html hidden input
<input type="hidden" id="someID" name="someName" value="someValue"/>
and now when I did
confirm($('someID').attr('value'))
a confirmation box came up with "someValue"
as the message, no longer blank. Thank you again, Microsoft, for making life difficult
Upvotes: 1