Problems: There are two models Registration and Info, What I need is ,there is dropdownList “Patient” in Info view which contain RegistrationNo from Registration table(model).When a registrationNo is selected its should autopopulate value in textbox “LastName” which is in Info View from table Registration
Below I have mention the screen short of controller(Info),view(Info)
Controller:
public JsonResult GetTextBoxValue(int Id)
{
var query = from reg in db.RegistrationMaster
where reg.Id == Id
select new { Id = reg.Id, FirstName = reg.LastName };
return Json(new SelectList(query.ToArray(), "Id", "LastName"), JsonRequestBehavior.AllowGet);
}
public ActionResult Create()
{
List<Registration> listObj = new List<Registration>();
var query = (from reg in db.RegistrationMaster
select new { Id = reg.Id, RegistNo = reg.RegistNo }).ToList();
ViewBag.PatientRegNo = new SelectList(query, "Id", "RegistNo");
return View();
}
View:
<fieldset style="border:solid 1px">
<legend>OpdBillingMst</legend>
<fieldset style="border:solid 1px;margin:15px">
<table>
<tr>
<td>
@Html.LabelFor(model => model.Patient, "Patient")
</td>
<td>
@Html.DropDownListFor(model => model.Patient, ViewBag.PatientRegNo as SelectList, "Select ", new { id = "Patient" })
@Html.ValidationMessageFor(model => model.Patient)
</td>
</tr>
<tr>
<td>
@Html.LabelFor(model => model.PatientRegNo)
</td>
<td>
@Html.EditorFor(model => model.PatientRegNo)
@* @Html.ValidationMessageFor(model => model.PatientRegNo)*@
</td>
<td>
@Html.LabelFor(model => model.PatientType)
</td>
<td>
@Html.EditorFor(model => model.PatientType, new { id = "PTypeId" })
@Html.ValidationMessageFor(model => model.PatientType)
</td>
</tr>
<tr>
<td>
@Html.LabelFor(model => model.PatientFirstName)
</td>
<td>
<select id="query" name="query"></select>
@* @Html.EditorFor(model => model.PatientFirstName, new { id = "FNameId" })*@
@Html.ValidationMessageFor(model => model.PatientFirstName)
</td>
<td>
@Html.LabelFor(model => model.PatientLastName)
</td>
<td>
@Html.EditorFor(model => model.PatientLastName, new { id = "LNameIdText"})
@Html.ValidationMessageFor(model => model.PatientLastName)
</td>
</tr>
<tr>
<td>
@Html.LabelFor(model => model.EmpId)
</td>
<td>
@Html.EditorFor(model => model.EmpId, new { id = "EId" })
@Html.ValidationMessageFor(model => model.EmpId)
</td>
<td>
@Html.LabelFor(model => model.EmailId)
</td>
<td>
@Html.EditorFor(model => model.EmailId , new {id ="email"})
@Html.ValidationMessageFor(model => model.EmailId)
</td>
</tr>
<tr>
<td>
@Html.LabelFor(model => model.ToVisitDoctor)
</td>
<td>
@Html.CheckBoxFor(model => model.ToVisitDoctor, new { id = "chkb" })
@Html.ValidationMessageFor(model => model.ToVisitDoctor)
</td>
</tr>
</table>
</fieldset>
<fieldset style="border:solid 1px;margin:15px" disabled="disabled" id="fieldId">
<table>
<tr>
<td>
@Html.LabelFor(model => model.DoctorName)
</td>
<td>
@Html.EditorFor(model => model.DoctorName)
@Html.ValidationMessageFor(model => model.DoctorName)
</td>
<td>
@Html.LabelFor(model => model.VisitType)
</td>
<td>
@Html.EditorFor(model => model.VisitType)
@Html.ValidationMessageFor(model => model.VisitType)
</td>
</tr>
<tr>
<td>
@Html.LabelFor(model => model.DoctorRate)
</td>
<td>
@Html.EditorFor(model => model.DoctorRate)
@Html.ValidationMessageFor(model => model.DoctorRate)
</td>
</tr>
</table>
</fieldset>
JavaScript:
<script type="text/jscript">
jQuery(document).ready(function () {
$("#Patient").change(function () {
var ServiceUrl = "/Info/GetTextBoxValue?Id="+ Id;
var content = '';
$.support.cors = true;
$.ajax({
type: 'GET',
url: ServiceUrl,
async: true,
cache: false,
crossDomain: true,
contentType: "application/json; charset=utf-8",
dataType: 'json',
error: function (xhr, err) {
},
success: function (query, status) {
$('#LNameIdText').val(query.Text);
debugger;
}
});
});
});
</script>