Search code examples
asp.netasp.net-mvc-3editortemplates

Custom Filename for @Html.EditorForModel()?


so I have the following code:

@model IEnumerable<My_School_Book.Models.Mark>
@{
    ViewBag.Title = "Marks";
}

<div class="row-fluid">
    <h1>Assignment Marks:</h1>
    @using (Html.BeginForm("Marks", "Assignment", FormMethod.Post, new { @class = "form-horizontal" }))
    {
        <table class="table table-bordered">
            <thead>
                <tr>
                    <th>Student</th>
                    <th style="width: 50px;">Mark
                    </th>
                    <th style="width: 50px;">Total
                    </th>
                    <th style="width: 50px;">Weight</th>
                </tr>
            </thead>
            <tbody>
                @Html.EditorForModel()
            </tbody>
        </table>
        <div class="form-actions">
            <button type="submit" class="btn btn-primary"><i class="icon-checkmark"></i>&nbsp;Save</button>
            <a href="@Url.Action("Index", "Assignment")" class="btn btn-primary"><i class="icon-back"></i>&nbsp;Cancel</a>
        </div>
    }
</div>

As you can see my Model being passed into the view is an IEnumerable of the Model Mark.

Now I have a EditorTemplate under the Views/Shared folder called Mark:

@model My_School_Book.Models.Mark
<tr>
    <td>@Model.Student.FullName</td>
    <td>
        @Html.TextBoxFor(model => Model.Grade, new { @class = "span12", @style = "min-height: 20px;", @maxlength = "5" })
    </td>
    <td>
        @Model.Assignment.Total
    </td>
    <td>@Model.Assignment.Weight</td>
</tr>

My question is, can I rename my EditorTemplate file from Mark to AssignmentMarks? I'd like it to be more specific instead of a generic name called Marks.

Edit:

Inside my MarkRepository.cs

    public IEnumerable<Mark> GetAllByAssignmentID(Guid id)
    {
        return context.Marks.Where(m => m.AssignmentID == id);
    }

Inside my AssignmentController.cs

    public ActionResult Marks(Guid id)
    {
        IEnumerable<Mark> Marks = markRepository.GetAllByAssignmentID(id).ToList();
        return View(Marks);
    }

    [HttpPost]
    public ActionResult Marks(IEnumerable<Mark> viewModel)
    {
        if (ModelState.IsValid)
        {

        }
        return View(viewModel);
    }

Inside my Marks.cshtml

   @Html.EditorForModel("Mark", "AssignmentMark")

Inside my AssignmentMark.cshtml

@model My_School_Book.Models.Mark
<tr>
    <td>@Html.DisplayTextFor(model => model.Student.FullName)</td>
    <td>@Html.TextBoxFor(model => model.Grade, new { @class = "span12" })</td>
    <td>@Html.DisplayTextFor(model => model.Assignment.Total)</td>
    <td>@Html.DisplayTextFor(model => model.Assignment.Weight)</td>
</tr>

The error I receive on my View is now:

System.Data.Entity.DynamicProxies.Assignment_7EDE7AD8477AB363802A036747CD8A8B259C6CD72DCEF587A2B0FBEDC7B2DCE1System.Data.Entity.DynamicProxies.Assignment_7EDE7AD8477AB363802A036747CD8A8B259C6CD72DCEF587A2B0FBEDC7B2DCE1System.Data.Entity.DynamicProxies.Assignment_7EDE7AD8477AB363802A036747CD8A8B259C6CD72DCEF587A2B0FBEDC7B2DCE1System.Data.Entity.DynamicProxies.Assignment_7EDE7AD8477AB363802A036747CD8A8B259C6CD72DCEF587A2B0FBEDC7B2DCE1

Solution

    1. Rename your model class to be AssignmentMarks
    2. Instead of @EditorForModel(), run the following:

      @foreach(var mark in Model)
      {
        @EditorFor(m => m.Mark, "AssignmentMark")
      }
      

    Hope this will help you.