I have tried various Google searches but am not getting specific results.
The problem is simple: I have an Edit form for records in a table. On the same view, there is also a dropdown list of all the records in that table. I want to use that dropdown list to reload the form with the selected record from the dropdown list.
This is the "Get" action for Edit:
public async Task<IActionResult> Edit(int? id)
{
if (id == null)
{
return NotFound();
}
var myObj = await _context.MyObjs.FindAsync(id);
if (myObj == null)
{
return NotFound();
}
VMEdit model = new VMEdit
{
// populate view model
};
var objList = _context.MyObjs.Select(c => new
{
Value = c.Id,
Text = c.Tag,
}).OrderBy(c => c.Text);
ViewData["ObjList"] = new SelectList(objList, "Value", "Text", id);
return View(model);
}
This is the view:
@model Audit.Models.VMEdit
@{
ViewData["Title"] = "Edit";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<form name="jumpForm" asp-action="Edit" asp-controller="MyObj" method="get">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group form-row">
<label asp-for="Id" class="col-md-3">Jump to: </label>
<div class="col-md-6">
<select asp-for="Id" class="form-control" asp-items="ViewBag.ObjList"></select>
</div>
<div class="col-md-3">
<input type="submit" value="Go" />
</div>
</div>
</form>
<form asp-action="Edit">
// edit form controls here
</form>
This form is behaving like this:
Suppose I am at /MyObj/Edit/3
Suppose I select the record with Id=5 from the dropdown and press Go
The page that gets loaded is /MyObj/Edit/3?Id=5
The page I intend to get loaded is /MyObj/Edit/5
I am guessing that naming the select field "Id" is causing it to get bound to the loaded Id (3). However, I do not know what to do. I want the Get request generated by the form to have Id paramter with value 5.
Any help will be appreciated. Specially simplest solution that does not involve javascript. Thanks.