开发者

Best Place To Populate ViewModels For Partials?

开发者 https://www.devze.com 2023-03-31 07:15 出处:网络
Currently I am using a ViewModelFactory hanging off HtmlHelper in an extension method: public static IViewModelFactory ViewModels(this HtmlHelper helper)

Currently I am using a ViewModelFactory hanging off HtmlHelper in an extension method:

public static IViewModelFactory ViewModels(this HtmlHelper helper)
{
   var factory = DependencyResolver.Current.GetService<IViewModelFactory>();
   return factory;
}

A开发者_开发问答nd then an example view with partial:

@model WidgetViewModel

<fieldset>
    @using (Html.BeginForm())
    {
        @Html.Partial("_Form.cshtml", Html.ViewModels().EventForm() )
    }
</fieldset>

Is this a bad idea? It feels dirty. If so, where/how is the testable best practice to populate/build ViewModels for my Partials?


This totally breaks the pattern.

ViewModels should be supplied via the controller, not the view.

0

精彩评论

暂无评论...
验证码 换一张
取 消