Forums - C# Corner

Forum guidelines
athira k

athira k

  • 1.4k
  • 72
  • 4.2k

how to fill textbox on selecting item from dropdown list mvc

May 8 2017 4:44 AM
I have a bit confusion in a session where that how to fill the textbox on selection of an item from drop down list  for that I have written the code that 
controller
 
public async Task<ActionResult> Index()
{
ViewBag.studentName = new SelectList(db.Students, "ID", "FirstName");
return View(db.Details.ToList());
}
 
view
 
<script src="~/Scripts/jquery-1.8.2.min.js"></script>
<script>
$(document).ready(function () {
$('#StudentName').change(function () {
$('#Fee').val($(this).val());
alert(Fee);
});
});
</script>
 
@using (@Html.BeginForm("Edit", "StudentFee", FormMethod.Post))
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<hr />
<div class="form-group">
<label class="col-sm-2 control-label">
Agent Name
</label>
<div class="col-md-3">
@Html.DropDownList("StudentName", (IEnumerable<SelectListItem>)@ViewBag.StudentName, "select value")
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">
Fee
</label>
<div class="col-md-3">
@Html.Editor("Fee", new { @class = "form-control" })
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label"></label>
<div class="col-md-3">
<input type="button" value="New" id="Create"/>
</div>
</div>
</div>
}
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.StudentId)
</th>
<th>
@Html.DisplayNameFor(model => model.Date)
</th>
<th>
@Html.DisplayNameFor(model => model.Fee)
</th>
<th></th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.StudentId)
</td>
<td>
@Html.DisplayFor(modelItem => item.Date)
</td>
<td>
@Html.DisplayFor(modelItem => item.Fee)
</td>
<td>
@Html.ActionLink("Collect", "Edit", new { id = item.Id }) |
@Html.ActionLink("Details", "Details", new { id=item.Id }) |
@Html.ActionLink("Delete", "Delete", new { id=item.Id })
</td>
</tr>
}
</table>
 
 
 This is my view page , here Now I get the Id of the item selected to the textbox but What is need is I need to get the Fee of the selected student instead of the Id and when clicking on the new button the action to edit edit page is not working or it is not redirect to the edit page . how can I do this can anyone please help me to find a solution for this ??

Answers (9)