0

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
Wesley
  • 309
  • 7
  • 21
  • Yes you can, look at my answer here: http://stackoverflow.com/questions/12050109/mvc3-editor-template-css-clas-maxlength-and-size/12052784#12052784. You just call it explicitly, like @EditorFor(m =>m.Mark, "AssignmentMarks") – MiBu Aug 23 '12 at 22:00
  • Well you see, with calling @Html.EditorFor(Model, "AssignmentMarks") I get a model mismatch (It's expecting a single entity of Mark, instead I'm passing in an IEnumerable. AND to top it all off, if I go ahead and change my EditorTemplate file to accept a IEnumerable when I pass back my viewModel to the Controller on POST I get a return type of NULL – Wesley Aug 23 '12 at 22:07
  • You can fall foreach, and then fo each item call editor for. For posting each of items in list must have different ID for properties (ie Mark.Student.FullName[index]...), you can see http://stackoverflow.com/questions/11267354/how-to-produce-non-sequential-prefix-collection-indices-with-mvc-html-editor-tem/11267659#11267659 for that scenario. – MiBu Aug 23 '12 at 22:13
  • @Html.EditorFor(model => model.marks) is now returning this: System.Data.Entity.DynamicProxies.Assignment_7EDE7AD8477AB363802A036747CD8A8B259C6CD72DCEF587A2B0FBEDC7B2DCE1System.Data.Entity.DynamicProxies.Assignment_7EDE7AD8477AB363802A036747CD8A8B259C6CD72DCEF587A2B0FBEDC7B2DCE1System.Data.Entity.DynamicProxies.Assignment_7EDE7AD8477AB363802A036747CD8A8B259C6CD72DCEF587A2B0FBEDC7B2DCE1 Why? – Wesley Aug 23 '12 at 23:07
  • Did you serialize your list before hitting View? Your EF context is not alive on page, so every object should be serialized before. btw. it's good to use POCO classes and DbContext. – MiBu Aug 24 '12 at 07:36
  • @MiBu I am using DbContext, some of my classes are POCO. And I am serializing my viewModel before passing it. I'll update my question with my DbContext code. – Wesley Aug 24 '12 at 12:31

1 Answers1

0
  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.

Display Name
  • 4,672
  • 1
  • 33
  • 43