用于在窗体上重复数据的模板的编辑器

本文关键字:编辑器 数据 窗体 用于 | 更新日期: 2023-09-27 18:21:02

我正在尝试为重复类(Offer)设置EditorTemplate-但是,我很难让EditorTemplate正常工作:

我的ViewModel是:

    public class OfferVM
    {
    public int RoomTypeId { get; set; }
    public int PropertyId { get; set; }
    public string RoomTypeName { get; set; }
    public string Layout { get; set; }
    public decimal RoomRate { get; set; }
    public string Inclusions { get; set; }
    public string Property { get; set; }
    public bool IncludeInOffer { get; set; }
    }

我的控制器获取代码是:

    //
    // GET: /Offer/Create
    public ActionResult Create()
    {
        var roomtypes = db.RoomTypes.Include(r => r.Property).ToList();
        IList<OfferVM> offerVM = Mapper.Map<IList<RoomType>, IList<OfferVM>>(roomtypes);
        return View(offerVM);
    }

控制器工作正常-我可以在VS中看到它被填充。

我的Create.cshtml代码是:

@model IEnumerable<FGBS.ViewModels.OfferVM>
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
@using (Html.Bootstrap().Begin(new Form().Type(FormType.Horizontal))) {
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<fieldset>
    <legend></legend>

<table>
<tr>
    <th>
        @Html.DisplayNameFor(model => model.RoomTypeId)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.PropertyId)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.RoomTypeName)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.Layout)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.RoomRate)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.Inclusions)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.Property)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.IncludeInOffer)
    </th>
    <th></th>
    </tr>
    @Html.EditorFor(Model => Model.OfferVM)
    </table>

然后在我的Views/Home/EditorTemplates文件夹中,我有我的OfferVM.cshtml文件:

@model FGBS.ViewModels.OfferVM
@{
    Layout = null;
}
  <tr>
    <td>
        @Html.EditorFor(model => model.RoomTypeId)
        @Html.ValidationMessageFor(model => model.RoomTypeId)
    </td>

    <td>
        @Html.EditorFor(model => model.PropertyId)
        @Html.ValidationMessageFor(model => model.PropertyId)
    </td>

    <td>
        @Html.EditorFor(model => model.RoomTypeName)
        @Html.ValidationMessageFor(model => model.RoomTypeName)
    </td>

    <td>
        @Html.EditorFor(model => model.Layout)
        @Html.ValidationMessageFor(model => model.Layout)
    </td>

    <td>
        @Html.EditorFor(model => model.RoomRate)
        @Html.ValidationMessageFor(model => model.RoomRate)
    </td>

    <td>
        @Html.EditorFor(model => model.Inclusions)
        @Html.ValidationMessageFor(model => model.Inclusions)
    </td>

    <td>
        @Html.EditorFor(model => model.Property)
        @Html.ValidationMessageFor(model => model.Property)
    </td>

    <td>
        @Html.EditorFor(model => model.IncludeInOffer)
        @Html.ValidationMessageFor(model => model.IncludeInOffer)
  </td>
    </tr>

然而,当我运行代码时,我会得到错误:

"System.Collections.Generic.IEnumerable"不包含"OfferVM"的定义,也找不到接受类型为"System.Collections.cGeneric.IEnumerable"的第一个参数的扩展方法"OfferVM"(是否缺少using指令或程序集引用?)"

我肯定我错过了一些简单的东西,但任何关于如何纠正我的问题的建议都会很棒。

用于在窗体上重复数据的模板的编辑器

@Html.EditorFor(Model => Model.OfferVM)是错误的,因为Model已经是IEnumeration。你可能在重构时把ViewModel搞砸了?我建议为Create.cshtml提供一个容器ViewModel,这样就可以使用一个类似Model的CreateViewModel类来代替模型IEnumerable<FGBS.ViewModels.OfferVM>,该类然后具有属性Offers,即IList<OfferVM>

您的型号代码:

public class OfferVM
{
    ....
}
public class CreateViewModel {
    public IList<OfferVM> Offers { get; set; }
    public OfferVM Header {
        get {
            return Offers.FirstOrDefault();
        }
    }
}

您的控制器代码:

var vm = new CreateViewModel();
vm.Offers = Mapper.Map<IList<RoomType>, IList<OfferVM>>(roomtypes);
return View(vm);

您的Create.cshtml以开头

@model FGBS.ViewModels.CreateViewModel

在你的TH中,你会写一些类似的东西:

@Html.DisplayNameFor(x => x.Header.RoomTypeId)

等等。