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.

6
  • You can pass a model to a partial, so it's no different than how you did it in your view. Commented Sep 8, 2014 at 14:53
  • Where do I propagate that model though, has no controller to propagate the data from the db Commented Sep 8, 2014 at 14:55
  • From the controller that created the model for your view. Commented Sep 8, 2014 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? Commented Sep 8, 2014 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. Commented Sep 8, 2014 at 15:03

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

So you're doing the propagation of the model data from the db, in the model itself without using any controller?
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.

7 Comments

Yes, I'm using razor, but I don't have an App_Code folder, I do however, see an App_Data folder that is currently empty, would this be it?
Create an App_Code folder then.
Hmm, ok. So this example you provided is a partial view or something else?
It's a helper. You can put HTML in it. You're not limited to C#.
I'm assuming a helper is not the same as a partial view and what I am asking really can't be done in partial view and must be done with a helper?
|
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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.