My dropdown list items populated from the database table and working fine and want to save the selected dropdown list item into another sql database table but saving only value (id) instead text. Somebody helps me to solve these problems.
I have tried below codes but save only id instead save text
Model:
public class BiodataSingle
{
public BiodataSingle()
{
this.MDA = new List<SelectListItem>();
this.ZoneDept = new List<SelectListItem>();
}
public List<SelectListItem> MDA { get; set; }
public List<SelectListItem> ZoneDept { get; set; }
public int MDAId { get; set;
public int ZoDepId { get; set; }
}
Controller:
[HttpPost]
public JsonResult AjaxMethod(string type, int value)
{
BiodataSingle model = new BiodataSingle();
switch (type)
{
case "ddlmdaName":
model.ZoneDept = (from z in this._context.ZoneDept
where z.MDAId == value
select new SelectListItem
{
Value = z.ZoDepId.ToString(),
Text = z.ZoneDeptname
}).ToList();
break;
}
return Json(model);
}
[HttpPost]
public async Task<IActionResult> SaveBiodata(BiodataSingle biodataSingle)
{
if (ModelState.IsValid)
{
var newbiodata = new BiodataViewModel()
{
MDA = biodataSingle.MDAId,
Department = biodataSingle.ZoDepId,
};
await _context.BiodataA.AddAsync(newbiodata);
await _context.SaveChangesAsync();
}
return View(biodataSingle);
}
View:
<select id="ddlmdaName" name="MDAId" asp-for="MDAId" asp-items="Model.MDA" >
<option value="">--Please select--</option>
</select>
<select id="ddlzoneDept" name="ZoDepId" asp-for="ZoDepId" asp-items="Model.ZoneDept" >
<option value="">--Please select--</option>
</select>
CodePudding user response:
If I don't misunderstand your question, When you select an option and submit the form, In controller you can only get the value(Id), But you want to get the text, right? I hope this simple demo can help you.
When your dropdown list items populated from the database, You can use TempData["xx"]
to pass data between actions.
//.....other code.....
BiodataSingle model = new BiodataSingle();
//populate from database
model.MDA = xxxx;
model.ZoneDept = xxxx;
//then use TempData["xx"] to save the value
TempData["MDA"] = JsonSerializer.Serialize(model.MDA);
TempData["ZoneDept"] = JsonSerializer.Serialize(model.ZoneDept);
//....other code.....
Then in SaveBiodata
action, You can select text by value in TempData["xxxx"]
[HttpPost]
public async Task<IActionResult> SaveBiodata(BiodataSingle biodataSingle)
{
if (ModelState.IsValid)
{
List<SelectListItem>? MDA= JsonSerializer.Deserialize<List<SelectListItem>>(TempData["MDA"].ToString());
List<SelectListItem>? ZoneDept= JsonSerializer.Deserialize<List<SelectListItem>>(TempData["ZoneDept"].ToString());
//Then you can select the corresponding text by value(Id)
var MDAName= MDA.Where(x => x.Value == biodataSingle.MDAId).Select(y => y.Text).FirstOrDefault();
var ZoneDeptName= ZoneDept.Where(x => x.Value == biodataSingle.ZoDepId).Select(y => y.Text).FirstOrDefault();
//Now you have got the name
//.....other code......
}
return View(biodataSingle);
}
Or, If you don't need the ID to do any action, You can just:
select new SelectListItem
{
Value = z.ZoneDeptname,
Text = z.ZoneDeptname
}
Set both Value and b Text to name, Then you can get name directly in the backhand.
CodePudding user response:
Change your dropdown markup to be like:
<select id="ddlzoneDept" name="ZoDepId" asp-for="ZoDepId" asp-items="@(new SelectList(Model.ZoneDept , "ZoDepId", "ZoneDeptname"))" >
<option value="">--Please select--</option>
</select>
I am using this way, and it is working.