Solarion
Honorary Master
- Joined
- Nov 14, 2012
- Messages
- 28,080
- Reaction score
- 17,849
Hi everyone. I have a form with a dropdown which is Employee Type. Below that a label which should display the remuneration rate depending on what employee type was selected.
This is what I have so far. The controller action fires off fine. However the text field is not getting populated. My Ajax knowledge is next to nothing so really just feeling my way in the dark. What have I done wrong?
NOTE: If I hardcode a value into the jquery script then upon changing the dropdown then yes the text box gets populated with that hardcoded value so that is function fine too. It seems that this part here is not picking up the value: $("#RemunerationRate").val(vdata[0].RenumerationRate);
View
Controller
What is being returned from usertypes service
This is what I have so far. The controller action fires off fine. However the text field is not getting populated. My Ajax knowledge is next to nothing so really just feeling my way in the dark. What have I done wrong?
NOTE: If I hardcode a value into the jquery script then upon changing the dropdown then yes the text box gets populated with that hardcoded value so that is function fine too. It seems that this part here is not picking up the value: $("#RemunerationRate").val(vdata[0].RenumerationRate);
View
Code:
<div class="form-group row">
<label asp-for="UserTypeId" class="col-sm-2 col-form-label"></label>
<div class="col-sm-5">
<select id="remunerationrate" asp-for="UserTypeId" class="form-control" asp-items="ViewBag.UserTypes" onchange="Action(this.value);">
<option value="">-- Select User Type --</option>
</select>
<span asp-validation-for="UserTypeId" class="text-danger"></span>
</div>
</div>
Code:
<div class="form-group row">
<label asp-for="RemunerationRate" class="col-sm-2 col-form-label"></label>
<div class="col-sm-5">
@Html.TextBox("RemunerationRate", null, new { @class = "form-control" })
</div>
</div>
Code:
<script type="text/javascript">
function Action(code) {
$.ajax({
url: '@Url.Action("Action", "User")',
type: "POST",
data: { "code": code },
"success": function (data) {
if (data != null) {
var vdata = data;
$("#RemunerationRate").val(vdata[0].RemunerationRate);
}
}
});
}
</script>
Controller
Code:
//Action Function
[HttpPost]
public async Task<ActionResult> Action(string code)
{
var userTypes = await _usertypeService.Get();
var type = userTypes.Where(x => x.Id == Convert.ToInt32(code));
return Json(type);
}
What is being returned from usertypes service
Last edited: