1 year ago

#385094

test-img

MichaelMitchell

Html.BeginForm inside list of partial views

Is it possible to do Databinding on a form post when the form is inside a partial view - either through <partial name=... for=...> or Html.EditorFor(..., but where that partial view is inside a foreach block?

The user expects to only work with a subset of the page (it is a tabbed interface) and submit only the tab they are currently working with. Each tab is its own partial view and its own form. Each tab has an identical set of fields with a different primary key (included in the form as a hidden input).

My form submit handler is firing, but the databound object is empty.

My HTML ids are prefixed thanks to the partial for or EditorFor.

[Bind(Prefix)] on the form submit handler doesn't work for me because the prefix isn't constant, it depends on the loop iteration.

My current code:

@for (var i = 0; i < Model.TabModels.Count(); i++)
{
    @Html.EditorFor(m => Model.TabModels[i], "TabModel")
}

It does bind properly on display, just not on submit.

Is there a way to do this, or should I submit the entire Model instead of Model.TabModels[x], as in, make one giant form instead of one form per tab? Note that the forms are not nested.

asp.net-core

razor

data-binding

0 Answers

Your Answer

Accepted video resources