Hi @jewel
When inserting data it works correctly but when editing it doesn't show item in selectlist.
public IActionResult Edit(int IDs)
{
persondata model = new persondata();
if (IDs > 0)
{
persondata emp = _context.persondatas.Where(x => x.ID == IDs).SingleOrDefault();
model.ID = emp.ID;
model.Name = emp.Name;
model.CountryID = emp.CountryID;
model.StateID = emp.StateID;
}
var data = _context.persondatas.Where(x => x.ID == IDs).SingleOrDefault();
return PartialView("~/Views/Dropdown/_partialview.cshtml", model);
}
The issue relates the Edit
method and the JavaScript scripts.
From your code, we can see you just return a persondata model to the partial view, without return the DropDownList options to the partial view. And in your JavaScript scripts, you will populate the DropDownList in the insert part using the Country()
function, this action is executed before the Edit partial view loading, and in the Edit(Std)
function, after loading the edit partial view, you also didn't find the DropDownList in the Edit partial view and populate it. So, the DropDownList in the edit partial view is empty.
To solve this issue, you can use ViewBag to transfer the countries and states from Edit action to the partial view, then populate the DropDownList using ViewBag. Code like this:
public IActionResult Edit(int IDs)
{
persondata model = new persondata();
if (IDs > 0)
{
persondata emp = _context.persondatas.Where(x => x.ID == IDs).SingleOrDefault();
model.ID = emp.ID;
model.Name = emp.Name;
model.CountryID = emp.CountryID;
model.StateID = emp.StateID;
}
var data = _context.persondatas.Where(x => x.ID == IDs).SingleOrDefault();
//used to populate the dropdownlist.
ViewBag.countries = new SelectList(_context.countries.ToList(), "CountryID", "Name");
ViewBag.state = new SelectList(_context.states.ToList(), "StateID", "sName");
return PartialView("~/Views/Dropdown/_partialview.cshtml", model);
}
And in the partial view, you can use asp-items
to bind the DropDownList:
@model aspCoreProject.Models.persondata
<form>
<div>
<label>Person Name</label>
<input asp-for="Name" id="name" />
</div>
<div>
<label>Country Name</label>
<select asp-for="CountryID" id="Country" asp-items="ViewBag.countries">
<option>---Select Country Name----</option>
</select>
</div>
<div>
<label>State Name</label>
<select asp-for="StateID" id="State" asp-items="ViewBag.state">
<option>---Select State Name----</option>
</select>
</div>
<div>
<button class="btn btn-primary" onclick="Update()">Update</button>
</div>
</form>
Then, the result as below: after clicking the Edit button, the DropDownList is populated and has the default selected item.
Besides, you can also populate the edit partial view's DropDownList in the Edit(Std)
function, after loading the partial view, use jquery to find the dropdownlist and then use jquery ajax to call the country action and populate the dropdownlist.
function Edit(Std) {
var url = "/Dropdown/Edit?IDs=" + Std;
$("#myModalBodyDiv1").load(url, function (data) {
$("#myModal1").modal("show");
//use JQuery selector to find the DorpDownList in the partial view,
//then use JQuery ajax to call the /Dropdown/Country
$('#heading').text('Insert Or Updae Record');
})
}
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
Best regards,
Dillion