A lightweight, extensible, open-source, and cross-platform version of the Entity Framework data access technology.
ASP.NET Core MVC cannot post the Razor Model automatically; navigate to an edit action with an identifier, then have that action reconstruct or retrieve the draft and supply a strongly typed edit view model.
Model binding reads HTTP request values—primarily named form controls, route values, and query-string values—not the server-side object originally used to render the view. Because the form contains no controls with name attributes, Update_DB(MyModel model) receives an empty/default model ^1^.
Recommended flow: pass an identifier
If the object can be regenerated or is already held in a server-side draft store, pass only its ID or draft token:
@model MyModel
<a class="btn btn-primary"
asp-controller="Home"
asp-action="Manual"
asp-route-id="@Model.Id">
Manual edit
</a>
The controller retrieves the object and maps it to an action-specific edit model:
[HttpGet]
public async Task<IActionResult> Manual(int id)
{
var item = await _repository.GetDraftAsync(id);
if (item is null)
return NotFound();
return View(new ManualEditVM
{
Id = item.Id,
Name = item.Name,
Description = item.Description
});
}
The editing view then contains inputs for the properties the user may change:
@model ManualEditVM
<form asp-controller="Home" asp-action="Manual" method="post">
<input asp-for="Id" type="hidden" />
<input asp-for="Name" />
<textarea asp-for="Description"></textarea>
<button type="submit">Continue</button>
</form>
The Input Tag Helper generates the name attributes that allow MVC to bind the submitted values ^2^.
Receive the edit model, retrieve the authoritative draft again, and copy only permitted values:
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Manual(ManualEditVM input)
{
if (!ModelState.IsValid)
return View(input);
var draft = await _repository.GetDraftAsync(input.Id);
if (draft is null)
return NotFound();
draft.Name = input.Name;
draft.Description = input.Description;
await _repository.SaveDraftAsync(draft);
return RedirectToAction(nameof(Review), new { id = draft.Id });
}
Using a dedicated input/view model limits overposting; Microsoft recommends view models rather than binding a broad entity type during edits ^1^.
If the object has not been persisted anywhere
A browser request cannot carry an object that is neither represented by form fields nor stored server-side. Choose one of these designs:
- Persist it as a draft in a database or another server-side store, and pass an opaque draft ID between actions. This is the most robust choice for a workflow that may last a long time.
- Keep it in session if the workflow is short-lived and tied to one browser session. Complex session values must be serialized, such as with JSON; Microsoft cautions against storing live serialized objects in session ^3^.
- Post hidden fields if the object is small and all values can safely round-trip through the client. Hidden values can be modified by the user, so revalidate them and never treat them as authoritative ^3^.
TempData is intended for data needed across a redirect and is normally better suited to small messages than a long-lived editable object ^3^.
Also note that the existing Manual anchor is navigation independent of the surrounding form; it does not submit that form. The submit button posts to Update_DB, but with no model-bound controls it sends no MyModel property values.
References