Click here to Skip to main content
15,885,767 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
My edit view uses partial views to edit different addresses. On submit, the form keys are identical for street and are not assigned to the model.

I get:
Street[0]: Silicon Road
Street[1]: Main Avenue

I need something like:
customer.Street: Silicon Road
invoice.Street: Main Avenue

So I don't now which is the street for customer or invoice. How can I differentiate the source field?

What I have tried:

Model:
C#
public class Order
{
    public Address CustomerAdress { get; set; } //Street="Silicon Road"
    public Address InvoiceAdress { get; set; } //Street="Main Road"
}

public class Address
{
    public string Street { get; set; }
}

EditForm.cshtml

HTML
@Model EditForm
<form name="EditForm">
    @await Html.PartialAsync("AddressEditPartial.cshtml", @Model.CustomerAddress)
    @await Html.PartialAsync("AddressEditPartial.cshtml", @Model.InvoiceAddress)
    <button type="submit">Submit</button>
</form>

AdressEditPartial.cshtml

HTML
@Model Address

<input asp-for="@Model.Street" />

Form-Keys in controller after submit:

street[0]: Silicon Road
street[1]: Main Avenue
Posted
Updated 26-Jan-22 8:38am
v4

Use the <partial> tag helper instead of the PartialFor extension method:
Razor
@Model EditForm
<form name="EditForm">
    <partial name="AddressEditPartial" for="CustomerAddress" />
    <partial name="AddressEditPartial" for="InvoiceAddress" />
    <button type="submit">Submit</button>
</form>
asp.net mvc - getting the values from a nested complex object that is passed to a partial view - Stack Overflow[^]
 
Share this answer
 
Comments
Sni.DelWoods 26-Jan-22 5:59am    
Great job, thanks. This makes it very easy to change my views to <partial>.
Actually this tag helper does not execute async, maybe in a future release...
https://stackoverflow.com/questions/68834168/does-partial-tag-helper-use-renderpartialasync-or-partialasync
Richard Deeming 26-Jan-22 6:06am    
According to the other answer on that thread, and the source code[^], it does render the partial view asynchronously. :)
Sni.DelWoods 26-Jan-22 6:51am    
Shame on me for my useless reply. :-) Thanks.
You can modify your code in respectively like:
EditForm.cshtml
@await Html.PartialAsync("AddressEditPartial.cshtml", @Model.Order)

AdressEditPartial.cshtml
@Model Order
<input asp-for="@Model.CustomerAdress.Street" />
<input asp-for="@Model.InvoiceAdress.Street" />
 
Share this answer
 
Comments
Sni.DelWoods 26-Jan-22 5:29am    
Yes, but this makes my partial view obsolete. The edit form consists of many input fields. To avoid duplicates I wanted to work with partials views. If there a property in the address class changes I only need to change one single point instead of 2 lines in the EditForm.cshtml

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900