aspnet / Mvc

[Archived] ASP.NET Core MVC is a model view controller framework for building dynamic web sites with clean separation of concerns, including the merged MVC, Web API, and Web Pages w/ Razor. Project moved to https://github.com/aspnet/AspNetCore
Apache License 2.0
5.62k stars 2.14k forks source link

ViewComponent inside form and Model Biding #8625

Closed Eilon closed 5 years ago

Eilon commented 5 years ago

From @ciacco85 on Friday, 19 October 2018 07:32:34

Using a Model like

class A
{
B b {get; set;}
}
class B
{
string testString{get;set;}
}

and passing the b property to the viewcomponent

<form aspAction='testMethod'>
<vc:myModel model="@(Model.b)"></vc:myModel>
</form>

when posting the form, the model property testString is alwayse null. I've found a workaround, after a long experience with MVC, inside the viewcomponent, adding the "b" inside name

<input asp-for="testString" name="b.testString" class="form-control" />

but in this situation, a ViewComponent should know something about the class in wich the model is placed...two object coupled with no relationship is not that good Should I use the editor template o change the approch?? ViewComponent are so powerful. Thanks for your great work!!!

Copied from original issue: aspnet/AspNetCore#3673

mkArtakMSFT commented 5 years ago

Thanks for contacting us, @ciacco85. @dougbu, can you please look into this? Thanks!

mkArtakMSFT commented 5 years ago

Thank you for filing this issue. In order for us to investigate this issue, please provide a minimalistic repro project that illustrates the problem.

dougbu commented 5 years ago

@ciacco85 thank-you for your feedback. We're closing this issue as no updates have been provided in a timely manner and we have been unable to reproduce the issue. If you have more details and are encountering this issue, please add a new reply and re-open the issue.

In particular,

to the viewcomponent

Please provide the view component source, both C# and the .cshtml file (if any)

the model property testString is alwayse null.

Where?

Should I use the editor template o change the approch??

That's a good approach if I'm understanding the issue (guessing at the moment). View component invocations pass values but do not change the TemplateInfo.HtmlFieldPrefix. That's by design.