Click here to Skip to main content
15,949,741 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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);
}

// GET: /Info/Create

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:
HTML
<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:
HTML
<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>
Posted
Updated 10-Mar-15 9:58am
v2

1 solution

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