1
votes

I've got a an object with an Address object as a property which is used several places in my application. I made a partial view which looks like

@model Address
<div>
    <div class="form-group">
        <label asp-for="@Model.AddressLine1"></label>
        <input asp-for="@Model.AddressLine1" />
    </div>
    ....

I use the form like

<form method="post">
    <div class="form-group">
        <input asp-for="@Model.Request.Business.Owners[0].FirstName" />
        <input asp-for="@Model.Request.Business.Owners[0].LastName" />
    </div>
    <input type="submit"/>
</form>

the data renders fine on the entire form, including the partial Address view. when I submit the form, everyone I reference the Address partial I get back as null when I submit the form, while the other data is data bound correctly. Is it possible to bind data to a partial view and have it submit with a form post correctly?

1
How are you binding the model? What's your Address object? Could you also show the controller code and the HTML that the partial generates?Anton Toshik

1 Answers

11
votes

Likely, you're not providing context to the partial, so that it can generate the names properly. As-is, the partial is going to want to generate names like AddressLine1, when what you actually need for proper binding is Request.Business.Owners[0].Address.AddressLine1. I'm assuming you're doing something like:

<partial name="_Address" model="Request.Business.Owners[0].Address" />

Instead, you need to use the for attribute:

 <partial name="_Address" for="Request.Business.Owners[0].Address" />