How do we render @model? Throwing Exception In Plugin

May 13, 2014 at 3:38 AM
This code works great when you are doing simple renders.

However I need to bind my view to a model and when I try to render the view I get a inner exception of:
"c:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\root\45c71923\56a49e6f\App_Web_views.MyData2.index.cshtml.7f6456fa.xex6wv1f.0.cs(45): error CS0103: The name 'model' does not exist in the current context"}

This is my index.cshtml for the plugin model
@inherits System.Web.Mvc.WebViewPage<MyCompanyNameSpace.MyData>
@model MyCompanyNameSpace.MyData
@using (Html.BeginForm()) {
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)

    <fieldset>
        <legend>MyData</legend>

        <div class="editor-label">
            @Html.LabelFor(model => model.Name)
        </div>
    </fieldset>
}
I'm still somewhat new at MVC. So what am I missing?
Coordinator
May 13, 2014 at 4:22 PM
Edited May 13, 2014 at 4:25 PM
Right now, views embedded in dlls are formatted a bit different:

@using System.Web.Mvc
@using System.Web.Mvc.Ajax
@using System.Web.Mvc.Html
@inherits System.Web.Mvc.WebViewPage<Core.MapFlyout>


You don't need the @model because the @inherits already uses a template parameter that is the model.

Also, the example above has three @using statements before the @inherits which are used by the C# in the view. You can add as many @using statements for the various namespaces as needed.

Note that @using used in that way is very different than the @using shown in your example. Both are valid uses and mean different things.
Coordinator
May 13, 2014 at 4:24 PM
You can do anything in a cshtml embedded in plugin you can do in a cshtml in the main site project. Here is real example of a view I use in a production project:
@using System.Web.Mvc
@using System.Web.Mvc.Ajax
@using System.Web.Mvc.Html
@using ScoutingPlugin.Models
@inherits System.Web.Mvc.WebViewPage<ScoutingObservation>

@{
    string baseMediaServerUrl = Url.Action("Image", "Image");
}

@using (Html.BeginForm("SaveNoteDialog", "Scouting", FormMethod.Post))
{    
<div class="dialog" id="dlgScoutNote" title="" style="overflow:hidden;" data-dialog-width="630" data-dialog-height="400" data-dialog-max-height="500px" data-dialog-modal="false" data-dialog-resizable="true">
    @Html.ValidationSummary()
    @Html.HiddenFor(m => m.Key)
    @Html.HiddenFor(m=>m.ParentKey)
    <div class="sct-main-fields">
        <p>
            <label class="sct-note-label">Date Taken:</label>@Html.TextBoxFor(m => m.Date, Model.Date.HasValue?new { @Value = Model.Date.Value.ToString("MMM dd, yyyy h:mm tt") }:null)
        </p>
        <p>
            <label class="sct-note-label">Title:</label>@Html.TextBoxFor(m => m.Title)
        </p>
        <p>
            <label class="sct-note-label" style="vertical-align:top;margin-right:2px;">Main Note:</label>
            @Html.TextAreaFor(m => m.NoteText)
        </p>
    </div>
    <fieldset>
        <legend>Notes</legend>
        <div class="mps-toolbar">
            <div class="mps-toolbar-zone">
                <button id="sctAddNewNoteBtn" class="mps-toolbar-button" type="button" title="Add new note to observation"></button>
                <button id="sctDeleteNoteBtn" class="mps-toolbar-button disabled" type="button" title="Delete selected note"></button>
                <button id="sctAddPolygonBtn" class="mps-toolbar-button disabled" type="button" title="Draw polygon for selected note"></button>
                <button id="sctAddCircleBtn" class="mps-toolbar-button disabled" type="button" title="Draw circle for selected note"></button>
                <button id="sctAddPointBtn" class="mps-toolbar-button disabled" type="button" title="Draw single point for selected note"></button>
                <button id="sctAddLineBtn" class="mps-toolbar-button disabled" type="button" title="Draw line for selected note"></button>
            </div>
        </div>
        <div class="sct-note-imgarea" style="height:250px;overflow:auto;">
            <table class="sct-notes">
                @if (Model.Notes != null)
                {
                    for (int i = 0; i < Model.Notes.Count; i++)
                    {
                        string imgUrl = baseMediaServerUrl + "/" + Model.Notes[i].Key + ".jpg?size=";
                        string hasGeo = !string.IsNullOrWhiteSpace(Model.Notes[i].Geometry) ? "" : "disabled";
                        <tr class="sct-note sct-note-fr sctNoteRow@(Html.Raw(i))">
                            <td width="80px" rowspan="2">
                                <div class='sct-note-imgdisp sct-note-data'>
                                    @if (Model.Notes[i].Key.HasValue)
                                    {
                                        <a class='sct-note-imgbox sct-note-data' href='@(imgUrl)Large' data-fancybox-group="scouting" title="@Model.Notes[i].Note">
                                            <img src='@(imgUrl)Thumbnail' data-name='' @*data-index='@i'*@ />
                                        </a>
                                    }
                                    else
                                    {
                                        <img src='@Url.Content("~/Content/images/blank.gif")' data-name='' data-index='@i' />
                                    }
                                </div>
                            </td>
                            <td><label for="Notes[@i].Label">Label:</label><input type="text" maxlength="15" id="Notes[@i].Label" name="Notes[@i].Label" class="sct-note-lbl sct-note-data" value="@Model.Notes[i].Label" /></td>
                            <td><label>Geo:</label><div class="sct-note-has-geo mps-toolbar-button @hasGeo" title='Indicates if the note has geometry associated with it'></div></td>
                        </tr>
                        <tr class="sct-note sct-note-sr sctNoteRow@(Html.Raw(i))">
                            <td colspan="2"><textarea id='Notes[@i].Note' name='Notes[@i].Note' style="width:96%" class='sct-note-txt sct-note-data'>@Model.Notes[i].Note</textarea></td>
                        </tr>
                    }
                }
            </table>
        </div>
    </fieldset>
    @if (Model.Notes != null)
    {
        for (int i = 0; i < Model.Notes.Count; i++)
        {
            string imgName = Model.Notes[i].Key + ".jpg";
            if (!Model.Notes[i].Key.HasValue)
            {
                imgName = "";
            }
            <input type='hidden' class='sct-note-img sct-note-data' name='Notes[@i].MimeType' id='Notes[@i].MimeType' value='@imgName' />
            @Html.HiddenFor(m => Model.Notes[i].Geometry, new { id = "Notes[" + i + "].Geometry", @class = "sct-note-geo sct-note-data" });
        }
    }


    <div style="clear:both"></div>

        <div class="buttonbar clear right">
            <input type="submit" id="btnDialogOk" value="OK" class="button fancy silver medium"
                   style="width: 100px;" />
            <input type="button" id="btnDialogCancel" value="Cancel" class="button fancy silver medium"
                   style="width: 100px;" />
        </div>
</div>
}
Coordinator
May 15, 2014 at 8:45 PM
Edited May 15, 2014 at 8:45 PM
You can also check out the SharePoint 2013 plugin, which is hosted as a separate project from this. https://sharepointplugin.codeplex.com/

It allows a variety of SharePoint 2013 content to be displayed in an MVC site without having to code directly to SharePoint. It has some interesting views too.