Question

New to MVC and I've not been able to understand answers to similar questions asked, so apologies if this is a repeat for people.

I have a details view using a character model:

@model Attacker.Models.Character

@{
    ViewBag.Title = "Details";
}

<h2>Details</h2>

<div>
    <h4>Character</h4>
    <hr />
    <dl class="dl-horizontal">
        <dt>
            @Html.DisplayNameFor(model => model.Name)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.Name)
        </dd>

        <dt>
            @Html.DisplayNameFor(model => model.Str)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.Str)
        </dd>

        <dt>
            @Html.DisplayNameFor(model => model.Con)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.Con)
        </dd>

        <dt>
            @Html.DisplayNameFor(model => model.Dex)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.Dex)
        </dd>

        <dt>
            @Html.DisplayNameFor(model => model.Int)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.Int)
        </dd>

        <dt>
            @Html.DisplayNameFor(model => model.Wis)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.Wis)
        </dd>

        <dt>
            @Html.DisplayNameFor(model => model.Cha)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.Cha)
        </dd>

        <dt>
            @Html.DisplayNameFor(model => model.BaseAttack)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.BaseAttack)
        </dd>
    </dl>
</div>
   @Html.Partial("~/Views/Shared/_Weapons.cshtml")
<p>
    @Html.ActionLink("Edit", "Edit", new { id = Model.CharacterID }) |
    @Html.ActionLink("Back to List", "Index")
</p>

My Partial view at the end is essentially another details view but from a weapon model. My aim is to show a character and the weapons he owns on one view but I'm getting an error due to the conflicting model types.

Is there a way around this?

Was it helpful?

Solution

Apart from using Editor/Display templates, you can do it in two different ways:

  1. include in your Attacker.Models.Character a list of model for weapons, and render them in a loop like this:

    @foreach(weaponModel in Model.Weapons) { Html.RenderPartial("", weapon); }

  2. create an action to display a weapon by id (or whatever key you want to use). Include the weapon keys in your model, and render the action for each weapon (this example assumes that ShowWeapon is an action in the same controller, but you can, of course, use another controller by specifying it):

    @foreach(weaponId in Model.WeaponIds) { Html.RenderAction("ShowWeapon", new {id = weaponId}); }

Using editor or display templates makes things easier, but is less flexible. It depends on what you need to achieve, or what you have already available.

Edit: about "flexibility"

To be more precise, an Editor or Display template is similar to a render partial, with the differece that the view template is automatically selected based upon the model type.

For example, if you define a type like 'Duration' which includes hours and minutes properties, you can create

  • an Editor template to show it as "hh:mm"
  • a Display template, that allows to input the value as "hh:mm" in a textbox.

This is great because, whenever you want to edit this kind of value you can rely in Html.EditorFor or Html.DisplayFor, to get this template, without any extra work. And this is really useful because you can have a model with several Duration properties, and they'll get "automagically" bound in the correct way.

However, if you have a more complex model, with many properties inside it, it's quite probable that you want to show them in different ways. For example,

  • depending on the permissions of the user logged in or on some business logic, Besides it can be neccesary to show an editor with some read only controls, or display only part of the values
  • depending on the place where you are displaying it (in a full page, inside a jQuery UI dialog, as a detail in another page...) you may need different layouts.

Obviously, in this cases an editor / display template will be to rigid.

Definitely, if you will always show or edit a model exactly in the same way, use editor/display templates. If not, RenderPartial or RenderAction will give the required flexibility.

(NOTE: it's possible to customize the Display and Editor templates passing extra data to them, but this is much harder to implement that a normal view / action).

OTHER TIPS

Yes, don't use Partials for this. Use Editor or Display Templates instead. I don't have the time to write a full response right now, others are free to if they want... These links should help for now.

http://blogs.msdn.com/b/nunos/archive/2010/02/08/quick-tips-about-asp-net-mvc-editor-templates.aspx http://www.growingwiththeweb.com/2012/12/aspnet-mvc-display-and-editor-templates.html http://www.codeproject.com/Articles/672591/Exploring-Display-and-Editor-Templates-in-ASP-NET

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top