Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strongly Typed view vs Normal View vs Partial View in Asp.NET MVC

I am Newer in ASP.NET MVC. I can not Clearly Understand the difference among

Strongly Typed view vs Normal View vs Partial View vs Dynamic-type View

in Asp.NET MVC. Anyone describe me about this terms.

Thanks in advance!!!

like image 231
Osman Avatar asked May 07 '16 13:05

Osman


People also ask

What is the difference between partial view and normal view in MVC?

Views are the general result of a page that results in a display. It's the highest level container except the masterpage. While a partial view is for a small piece of content that may be reused on different pages, or multiple times in a page.

What is strongly typed partial view in MVC?

Strongly Typed Partial View - When creating partial view, if we select or enter a type for the Model Class option then it will create strongly typed partial view. Now partial view file is created and it only contains the @model tag to specify the view model type.

What is strongly typed view in ASP.NET MVC?

Strongly typed views are used for rendering specific types of model objects, instead of using the general ViewData structure. By specifying the type of data, you get access to IntelliSense for the model class.


1 Answers

Strongly Typed view

A view which is bound to a view model. For example if you have the following view model:

public class MyViewModel
{
    public string SomeProperty { get; set; }
}

that is passed to the view by the controller action:

public ActionResult Index()
{
    var model = new MyViewModel();
    model.SomeProperty = "some property value";
    return View(model);
}

the strongly typed view will have the @model directive at the top pointing to this view model:

@model MyViewModel
...
<div>@Model.SomeProperty</div>

Partial View

The difference between a view and a partial view is that a partial view only contains some small HTML fragments that can be reused in multiple parts of a normal view. For example you could define the following partial view:

@model AddressViewModel
<div>Street: @Model.Street</div>
<div>Country: @Model.Country</div>

and then render this partial view at multiple places in your main view to avoid repetition of the same code over and over:

@model MainViewModel
...
<h3>Personal address</h3>
<div>@Html.Partial("_Address.cshtml", Model.PersonalAddress)</div>

...
<h3>Business address</h3>
<div>@Html.Partial("_Address.cshtml", Model.BusinessAddress)</div>

Dynamic-type View

A view which doesn't have a model or one that uses weakly typed structures such as ViewBag. For example you could have a controller action which sets some property in the ViewBag:

public ActionResult Index()
{
    ViewBag["SomeProperty"] = "some property value";
    return View();
}

and the corresponding view you could access this property by using the same key in the ViewBag:

<div>@ViewBag["SomeProperty"]</div>
like image 88
Darin Dimitrov Avatar answered Sep 19 '22 01:09

Darin Dimitrov