Best Practice for passing object from view to controller in MVC Core

Corey Fleig 615 Reputation points
2026-12-01T17:57:40.8933333+00:00

I usually pass data from view to controller by using session, or by passing an Id and fetching from a db. In this case, I would like to show data in a view, and then pass the object to another view for editing, long before persisting. The snippet below doesn't work, but am I forgetting something? There's no <input> here, because I want the next view to handle editing.

            @model MyModel

			<form asp-controller="Home" asp-action="Update_DB">
                <a class="btn btn-primary" asp-action="Manual">Manual</a>
                <button type="submit" class="btn btn-warning">Update EZ-CAP</button>
            </form>
Developer technologies | .NET | Entity Framework Core
0 comments No comments

1 answer

Sort by: Most helpful
  1. AI answer

    2026-12-01T17:58:39.1266667+00:00

    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^.

    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:

    1. 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.
    2. 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^.
    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

    1. Model Binding in ASP.NET Core
    2. Tag Helpers in forms in ASP.NET Core
    3. Session and state management in ASP.NET Core
    AI-generated content may be incorrect. Read our transparency notes for more information.

    Was this answer helpful?

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.