如何在ASP.net MVC中的组合框所选项目上填充明细字段更改 [英] How to populate details fields on combo box selected item change in ASP.net MVC

查看:73
本文介绍了如何在ASP.net MVC中的组合框所选项目上填充明细字段更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下控制器类方法,用于获取和发布使用WCF服务在MVC应用程序中更新人员记录的操作。我正在使用组合框选择ID。
这是我的控制器类方法,用于获取人员详细信息。

I have following controller class methods for get and post actions of updating a person record in my MVC application using a WCF service. There I'm using a combobox to select the ID. Here is my controller class methods for getting the person details.

public ActionResult updatePerson()
        {
            Service1Client SCOBJ = new Service1Client();
            List<Person> PeLi = SCOBJ.GetPersons().ToList();
            ViewBag.List = PeLi.Select(x => new SelectListItem
            {
                Value = (Convert.ToString(x.Id)),
                Text = x.FName + " " + x.LName
            });
           return View();
        }

        [HttpPost]
        public ActionResult updatePerson(Person personobj)
        {
            Service1Client SCOBJ = new Service1Client();
            SCOBJ.UpdatePerson(personobj);
            List<Person> PeLi = SCOBJ.GetPersons().ToList();
            ViewBag.List = PeLi.Select(x => new SelectListItem
            {
                Value = (Convert.ToString(x.Id)),
                Text = x.FName + " " + x.LName
            });
            return View();
        }

这是我的观点。

<form method="post" action="@Url.Action("updatePerson")">

    ID:@Html.DropDownList("Id", new SelectList(ViewBag.List, "Value", "Text"))
    @*<input type="text" name="Id" />*@ 
    <br />
    First Name: <input type="text" name="FName" />
    <br />
    Middle Name: <input type="text" name="MName" />
    <br />
    Last Name: <input type="text" name="LName" />
    <br />
    Date of Birth:<input type="date" id="start" name="DOB" value="2018-07-22" min="1900-01-01" max="2000-12-31" />
    <br />
    NIC:<input type="text" name="NIC" />
    <br />
    Address:<input type="text" name="Adddress" />
    <br />
    <input type="submit" value="Insert" />

</form>

我想将详细信息加载到名字中间名姓氏地址和NIC文本字段以及DOB日期当我更改ID的选定值时输入时间。换句话说,当我从ID的选择列表中选择一个ID时,我想将详细记录加载到所有字段。
我该如何实现?

I want to achieve loading details to First Name Middle Name Last Name Address and NIC text fields and DOB date time input field when I change the selected value of the ID. In other word I want to load detail record to all field when i select an ID from select list for ID. How can I achieve this?

推荐答案

假设您的控制器中有此

public JsonResult GetPersonDetails(int Id){
    var person = db.Person.Where(m => m.Id == Id); //this should be accessed from the db
    return Json(person);
}

然后您认为

<form method="post" action="@Url.Action("updatePerson")">

    ID:@Html.DropDownList("Id", new SelectList(ViewBag.List, "Value", "Text"), null,  new { @id= "Id"})
    <br />
    First Name: <input type="text" name="FName" id="FName" />
    <br />
    Middle Name: <input type="text" name="MName" id="MName" />
    <br />
    Last Name: <input type="text" name="LName" id="LName" />
    <br />
    Date of Birth:<input type="date" id="start" name="DOB" value="2018-07-22" min="1900-01-01" max="2000-12-31" />
    <br />
    NIC:<input type="text" name="NIC" />
    <br />
    Address:<input type="text" name="Adddress" />
    <br />
    <input type="submit" value="Insert" />

</form>

确保提供所有输入ID变量

make sure to give all inputs Id variable

然后您将有一个Javascript函数来调用控制器

Then you will have a Javascript function to call the controller

$("#Id").change(function(){
   var  value = $("#Id").val();
   //$.get(URL,data,function(data,status,xhr),dataType)
   $.get(
        "@Url.Action("GetPersonDetails")", 
        {id:value},
        function (response) {    
            $("#FName").val(response.FName);
            //assign all other variables here
        },
        "json"
   );
});

这篇关于如何在ASP.net MVC中的组合框所选项目上填充明细字段更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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