【问题标题】:MVC3: How to return table structure as EditorForMVC3:如何将表结构作为 EditorFor 返回
【发布时间】:2011-11-23 16:32:50
【问题描述】:

我想为作为属性存在于我的 ViewModel 类中的模型列表创建一个编辑器。

ViewModel 类:

public class FooManagementDetailViewModel : ViewModelBase
{
    public List<FooPermissionModel> FooPermissions { get; set; }

模型类:

public class FooPermissionModel
{
    public string Name { get; set; }
    public string Reason { get; set; }
    public bool Selected { get; set; }
}

编辑器模板:

@model FooPermissionModel
<table>
    <thead>
        <tr>
            <th>
                Heading
            </th>
            <th>
                Heading
            </th>
            <th>
                Heading
            </th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>
                @Html.TextBoxFor(x => x.Name, new { @readonly = "readonly" })
            </td>
            <td>
                @Html.CheckBoxFor(x => x.Selected)
            </td>
            <td>
                @Html.TextBoxFor(x => x.Reason)
            </td>
        </tr>
    </tbody>
</table>

查看:

   <fieldset>
      <legend>FooBarTitle</legend>
         <div>
             @Html.EditorFor(x => x.FooPermissions)
         </div>
   </fieldset>

我返回的只是名称的单个 div。根本没有结构。 我错过了什么?

谢谢!

【问题讨论】:

  • 我在您的模型中没有看到 CompanyName,这是拼写错误吗?
  • 你有什么看法?
  • 使用EditorForModel不是最好吗?并根据模型来做。不是列表。

标签: c# asp.net-mvc-3 razor


【解决方案1】:

我感觉您可能没有正确指定模型名称,或者 MVC 找不到您的 EditorTemplate。

请注意,此示例的模板位置是:~/Views/Shared/EditorTemplates/FooPermissionModel.cshtml

以下显示正确呈现 EditorTemplate:

型号:

public class FooPermissionModel
{
    public string Name { get; set; }
    public string Reason { get; set; }
    public bool Selected { get; set; }
}

视图模型:

public class FooManagementDetailViewModel
{
    public List<FooPermissionModel> FooPermissions { get; set; }
}

控制器:

public ActionResult Index()
{
    var fakePerms = new List<FooPermissionModel> () 
    { 
        new FooPermissionModel { Name = "Foo1", Reason = "Boo", Selected=true }, 
        new FooPermissionModel { Name = "Bazz", Reason = "Tootsie", Selected=false }
    };

    var model = new FooManagementDetailViewModel();
    model.FooPermissions = fakePerms;

    return View(model);
}

查看:

@model StackExamples.Models.FooManagementDetailViewModel
<fieldset>
    <legend>FooBarTitle</legend>
        <div>
            @Html.EditorFor(x => x.FooPermissions)
        </div>
</fieldset>

编辑器模板:

@model StackExamples.Models.FooPermissionModel
<table>
    <thead>
        <tr>
            <th>
                Heading
            </th>
            <th>
                Heading
            </th>
            <th>
                Heading
            </th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>
                @Html.TextBoxFor(x => x.Name, new { @readonly = "readonly" })
            </td>
            <td>
                @Html.CheckBoxFor(x => x.Selected)
            </td>
            <td>
                @Html.TextBoxFor(x => x.Reason)
            </td>
        </tr>
    </tbody>
</table>

【讨论】:

  • 这正是我实施的设置。我也有一种感觉 MVC 没有找到模板,但它在您指定的位置。 ~Views/Shared/EditorTemplates/... .cshtml 有什么办法可以手动设置这个位置吗?或者检查它是否找到模板?
  • 存在命名约定问题,现已修复。如此微不足道的错误。唉,谢谢你的帮助!
【解决方案2】:

你的@model 应该是 FooManagementDetailViewModel 然后你可以 foreach FooPermissions.Items 这将是 FooPermissions 对象

【讨论】:

  • 但是局部视图编辑器模板是用于 FooPermission 模型的。这些是我希望显示的字段。如果我将其更改为 FooManagementDetailViewModel 那么我将有权访问该属性但不能访问 FooPermissionModel 字段?它确实意味着一个 foreach,因为我为 List 中的每个条目返回一组结果
  • 抱歉 - 我没有看到 EditorTemplate 位,我以为是视图。我很抱歉。
  • 啊,不用担心。以为你当时可能正在做某事!
  • @MattTolliday 我不认为 EditorFor 可以处理列表,HTML.Partial('_yourPartial',Model.FooPermissions) 然后你的部分中的 foreach 不会完成这项工作吗?
【解决方案3】:

尝试使用EditorForModel()

查看:

@model FooPermissionModel


@using (Html.BeginForm("bar", "foo"))
{
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>Edit</legend>

        @Html.EditorForModel()

      <p>
        <input type="submit" value="Save" /> 
       </p>
     </fieldset>
 }

控制器:

    public ActionResult Edit()
    {
        //get instance of model
        // <CODE GOES HERE>


        // then pass it in the return
        return View(mymodelinstance);
    }

使用正确的模板,它应该会为您完成剩下的工作并将其放入表格中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-01-01
    • 2012-03-20
    • 2012-06-07
    • 2014-04-22
    • 1970-01-01
    • 2021-12-01
    • 1970-01-01
    相关资源
    最近更新 更多