Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
4.33/5 (2 votes)
See more:
hello,
I used the following code and I want get value of hdn1(hidden field)
C#
var grid = new WebGrid(Model, rowsPerPage: 15, ajaxUpdateContainerId: "myGrid");
  @grid.GetHtml(tableStyle: "clrPreview", columns: grid.Columns(
  grid.Column("CityName", "City", canSort:false),
                                              grid.Column("HotelName", "Hotel", canSort: false),
                                              grid.Column("HotelCategoryName", "Category", canSort: false),
                                              grid.Column("GitRangeStartFrom", "Git Range Start From", canSort: false),
                                       grid.Column(columnName: "Action", canSort: false,
                                          format: @<text><input type="hidden" id="hdn1" value='@item.HotelId'  />
  @Html.ActionLink("Edit", "Edit", new { id = item.HotelId }, new { rowid = item.HotelId })
  @* |  @Html.ActionLink("Delete", "Delete", new { id = item.HotelId })*@
  </text>)

I want to get hidden field value by jquery
Posted
Updated 1-May-13 0:33am
v3

Your problem is that you can not have multiple elements with the same ID. But with the pattern you use for that column you will generate inputs with the same ID. That won't work, since besides the fact that you violate the html standard, jquery can not grab it.
You have could use more approaches:
- just add a class to that input element, and no ID. And if you have already the row, you can grab it's child with that class - and that will be the input you want. Or if you have any other element in that row you can use the jquery dom navigation tools to get that. If it is the only hidden element input in the row you will probably don't need even the class.
- use the id from the model (@item.HotelId) to generate ID
If you need the id on the other columns of the row (and forget the hidden input):
- use the Style property of WebGridColumn to add a special class that holds the model id
- use the Format property as you used but add id as custom html property to the element you generate

All these can be used, an probably more, but you have to choose based on what you want to do with that value.
 
Share this answer
 
JavaScript
var hv = $('#hdn1').val();


Should do the trick.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900