问题
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();
}
This is my 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>
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?
回答1:
Lets say you have this in your controller
public JsonResult GetPersonDetails(int Id){
var person = db.Person.Where(m => m.Id == Id); //this should be accessed from the db
return Json(person);
}
Then in your view, you have
<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>
make sure to give all inputs Id variable
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"
);
});
来源:https://stackoverflow.com/questions/56846620/how-to-populate-details-fields-on-combo-box-selected-item-change-in-asp-net-mvc