MVC2 Html.HiddenFor - How to Set Value from ViewData
Having trouble here and not quite sure how to do it. I've attempted jQuery but with no luck. I have an object and on my create view, I have a parentId that I want to set (it's in the url as well). So, I'm trying to use Html.HiddenFor so it will populate my new object when returned on the post. BUT, this doesn't work. I've tried the following but it never returns the value on the post.
Html:
<%: Html.Hidden("HiddenParentId", ViewData("ParentId"))%>
<%: Html.HiddenFor(Function(model) model.ParentGroupId)%>
Javascript:
<script type="text/Javascript">
$(document).ready(function () {
alert($('#HiddenParentId').val());
$('#ParentId').val($('#HiddenParentId').val());
});
</script>
As you can see, on doc ready, I set the hidden parent id input with the value from the viewdata because the HiddenFor doesn't have a value attribute. The HiddenParentId field DOES in fact have the value that I'm setting because I'm alerting it and it's got the GUID that it should have. Now, first, I'm using VB so any help in VB would be greatly appreciated and second, if there's another method to solve this issue, please advise. All I'm trying to accomplish is to get the ID that was passed in the URL (which in the controller I'm grabbing that id and putting it in the ViewData) and set the ParentId property of 开发者_JAVA技巧my object for the post so that it will save it when the object is returned to me.
Please check the ID in HTML source for hidden field, I guess its rendering different name.
精彩评论