@ Html.EditorForModel()的自定义文件名?

时间:2012-08-23 21:57:08

标签: asp.net asp.net-mvc-3 editortemplates

所以我有以下代码:

@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>

正如您所看到的,我传递给视图的模型是模型标记的IEnumerable。

现在我在Views / Shared文件夹下有一个名为Mark:

的EditorTemplate
@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>

我的问题是,我可以将EditorTemplate文件从Mark重命名为AssignmentMarks吗?我希望它更具体,而不是名为Marks的通用名称。

编辑:

在我的MarkRepository.cs

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

在我的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);
    }

在我的Marks.cshtml内部

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

在我的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>

我在View上收到的错误现在是:

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

1 个答案:

答案 0 :(得分:0)

  1. 将您的模型类重命名为AssignmentMarks
  2. 执行以下操作,而不是@EditorForModel()

    @foreach(var mark in Model)
    {
      @EditorFor(m => m.Mark, "AssignmentMark")
    }
    
  3. 希望这会对你有所帮助。