选择DropDownList值时自动填充文本框 [英] AutoPopulate Textbox on selecting DropDownList value

查看:71
本文介绍了选择DropDownList值时自动填充文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:有两种模式注册和信息,我需要的是,在信息视图中有dropdownListPatient,其中包含来自注册表(模型)的RegistrationNo。当选择registrationNo时,它应该在文本框中自动填充值LastName 这是在信息视图中从表格注册

下面我提到屏幕短控制器(信息),查看(信息)

控制器:

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:

<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:


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>

推荐答案

#Patient)。change( function (){

var ServiceUrl = / Info / GetTextBoxValue? Id = + Id;
var content = ' ';
("#Patient").change(function () { var ServiceUrl = "/Info/GetTextBoxValue?Id="+ Id; var content = '';


.support.cors = true ;
.support.cors = true;


.ajax({
type:' GET'
url:ServiceUrl,
async: true
cache: false
crossDomain: true
contentType: application / json; charset = utf-8
dataType:' json'
错误: function (xhr,err){
},
成功: function (查询,状态){
.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) {


这篇关于选择DropDownList值时自动填充文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆