Create a partial view with dynamic content

I understand that when I create a view, I should not put any code there besides html and data from the model / controller, which I have done so far.

But let's say that there is a shortened dynamically generated html that can be used in several views, I assume that it will be a partial view, which is included in the shared folder in the project. But since this is a partial view that does not have an absolute controller for processing its distribution of dynamic data (from db), how would I call, and where would I encode the distribution of data from db into a view (or model?), If partial view is allowed for dynamically display content for table.id=netc.

I'm pretty new and working on a tutorial in .net trying to figure out how to do this. Does anyone know how to do this? Hope the question makes sense.

+4
source share
3 answers

You can always define a model for partial.

And you can display a partial from the container view passing through a dynamically populated instance of your 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 partial _FileUploader.cshtmlfrom index.cshtmlusing a method @Html.Partial(), passing an instance of a new model that indicates the value of UploaderClassName.

Edit

this.Model.UploaderClassName . , db .

MSDN .

+1

, , .cshtml App_Code .

:

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

( , Utility.cshtml)

@Utility.FormatDate(Patient.DOB)

, , .

0

I recently posted a nuget package for this. It is called Dynamic MVC.

http://dynamicmvc.com

You can see the source code on codeplex.

https://dynamicmvc.codeplex.com

The way I did this was to use the ModelMetadata mechanism built into MVC to allow me to get the value for any property in a weakly typed form. The ModelMetadata engine was originally derived from ASP.net dynamic data and ported to MVC in MVC2. It works great for this kind of situation.

0
source

All Articles