4

I understand when I create a view, I shouldn't be putting any code in there besides html and the data from the model/controller, which is what I've done so far.

But lets say there is a snipped of dynamically generated html that can be used in multiple views, I'm guessing this would be a partial view that goes in the Shared folder in the project. But since it's a partial view, that has no absolute controller to handle it's propagation of dynamic data (from db), how would I call, and where would I code the propagation of data from the db into the view (or model?), if lets say the partial view was to dynamically render content for table.id=n, etc.

I'm fairly to new and working off a tutorial in .net, trying to figure out how to do this. Anyone know how it's done? Hope the question makes sense.

Control Freak
  • 12,965
  • 30
  • 94
  • 145
  • You can pass a model to a partial, so it's no different than how you did it in your view. – MikeSmithDev Sep 08 '14 at 14:53
  • Where do I propagate that model though, has no controller to propagate the data from the db – Control Freak Sep 08 '14 at 14:55
  • From the controller that created the model for your view. – MikeSmithDev Sep 08 '14 at 14:59
  • Ok So lets say there are 10 different controller/models that use their own views, but also use this partial view... I would then go into each of the 10 controllers and propagate the model which will be used in the partial view, that makes sense, but doesn't that become redundant coding, ie: rewriting a propagation code in all 10 controllers? – Control Freak Sep 08 '14 at 15:01
  • Each Model can have a property that is a model that controls your partial view. So you are just making one new model. Then make one function to populate that model. Yes you'll need to populate that model in every controller but that is just one function call and you have to populate the model anyway. – MikeSmithDev Sep 08 '14 at 15:03
  • I would probably use a `section` for this.. it sounds like a candidate. – Simon Whitehead Sep 08 '14 at 23:41

3 Answers3

1

You can always define a model for the partial.

And you can render the partial from the container view passing a dinamically populated instance of its model:

<!-- index.cshtml -->
<h1>Feed Upload</h1>
<div id="uploader">
        @Html.Partial("~/Views/Shared/Controls/_FileUploader.cshtml", new FileUploaderModel() { UploaderClassName = this.Model.UploaderClassName })
</div>

In this simple example I call the partial _FileUploader.cshtml from the index.cshtml using the @Html.Partial() method, passing a new model instance that specifies the UploaderClassName value.

Edit

The this.Model.UploaderClassName refers to the container's model and it is initialized inside the container's controller business. Of course the container's controller can run any data access logic to grab dynamic data from the db and pass them to the partial's model.

Have a look at MSDN, and at this article.

Community
  • 1
  • 1
Alberto De Caro
  • 5,147
  • 9
  • 47
  • 73
0

Assuming you are using the razor view engine, you can put an .cshtml file in the App_Code folder with helper functions.

The syntax is like this:

@helper FormatDate(DateTime date)
{
    @date.ToShortDateString()
}

You call it like this (assuming the file is Utility.cshtml)

@Utility.FormatDate(Patient.DOB)

Because you can pass parameters to a helper, you can pass any type you need, including complex objects.

David Crowell
  • 3,711
  • 21
  • 28
0

I recently published a nuget package to do this very thing. It's called Dynamic MVC.

http://dynamicmvc.com

You can look at the source code on codeplex.

https://dynamicmvc.codeplex.com

The way I did this was to use the ModelMetadata engine built into MVC to allow me get the value for any property in a weakly typed fashion. The ModelMetadata engine originally came from ASP.net Dynamic Data and was ported over to MVC in MVC2. It works great for this kind of situation.

Chris Perry
  • 121
  • 5