I’m trying to use jquery in asp.net mvc3. I’m using EditorTemplates for the list. My model is as
public class staffinfo
{
public int staffid { get; set; }
public double extgrade { get; set; }
public string lvlid { get; set; }
public IList<SelectListItem> level { get; set; }
public string grdid { get; set; }
public IList<SelectListItem> grade { get; set; }
public double pf { get; set; }
public double wages { get; set; }
public List<hrfacility> facility { get; set; }
}
public class hrfacility
{
public int id { get; set; }
public string name { get; set; }
public double price { get; set; }
public int staffid { get; set; }
public string staffname { get; set; }
public double amount { get; set; }
public bool selected { get; set; }
}
View:
...
@Html.EditorFor(m => m.wages)
@Html.EditorFor(m=>m.facility)
EditorTemplate for hrfacility
@model Mysol.Models.hrfacility
...
@Html.DisplayFor(m=>m.name)
@Html.CheckBoxFor(m=>m.selected)
@Html.EditorFor(m => m.staffname)
@Html.EditorFor(m => m.amount)
<script type="text/javascript">
$('#staffname').attr('readonly', true);
</script>
I’ve used the above procedure without the list part in editortemplates and the jquery works fine but for this particular situation it is not working!!
Is it because there might be more than one staffname??
How can i make jquery work for the above situation (make staffname readonly)??
P.S. readonly is just for the example purpose. I need to do other things as well with jquery like assigning the value to the textbox. If i can get this work, i think i can do other things as well.
Thanks
It does not work because the id of the input for
staffnameshould be something likefacility_0_staffname,facility_1_staffnameand so on.You better add a class to the
staffnameinput and access it with that class.You may check the id with Firebug (on Firefox)