I’m having a very strange issue when rendering a partial view. My page has a table whose rows are rendered using a partial view. Here’s a snippet
<td>
<img src="@Url.Content("~/Content/Images/edit_icon.gif")" class="editLine" />
<img src="@Url.Content("~/Content/Images/delete_icon.gif")" class="deleteLine" />
@Html.HiddenFor(model => model.LineId)
</td>
This renders fine when the page first loads. The input tags have the value set to the id. However, when I do an ajax call and return the PartialView from the controller, this value is always 0.
Controller code:
line.LineId = 12; //whatever the actual value is
return PartialView("Line", line);
And the jquery code is
$.ajax({
data: {
Id: id
},
success: function (data, textStatus) {
$('#lines').append(data);
}
When I debug through and look I see data has come back as
<input name="lineId" id="lineId" value="0" />
I debugged through EVERYTHING and the lineId is not 0. I then debugged throuh the view itself and whenever I did Html.Hidden(“lineId”, anyvalueHere) it rendered as 0. When I changed it to be Html.Hidden(“lineId_” + actualId, actualId) it worked.
Is this known/expected behavior? I can imagine it not jiving with duplicate id’s. However, it renders just fine on the page load where I use the exact same partial view. Is there magical juju for client side stuff? I would imagine not since it’s just a controller action and it doesn’t know the difference. Has anyone seen this. Thanks
If you are changing the value when you post, that is the issue. Use:
instead of the Html.HiddenFor.