简体   繁体   English

ASP.Net C#MVC EditorFor模板没有从主剃刀视图调用

[英]ASP.Net C# MVC EditorFor Template not being called from main razor view

I'm trying to setup an EditorTemplate for a repeating class (Offer) - however, I'm having trouble getting the EditorTemplate to show. 我正在尝试为重复的类(Offer)设置EditorTemplate - 但是,我无法让EditorTemplate显示。 It's as if it just isn't being found in the EditorTemplates folder: 就好像它在EditorTemplates文件夹中找不到一样:

My ViewModel is: 我的ViewModel是:

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

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; }
    }

My Controller Get code is: 我的控制器获取代码是:

    //
    // GET: /Offer/Create

    public ActionResult Create()
    {

        var roomtypes = db.RoomTypes.Include(r => r.Property).ToList();
        var vm = new CreateViewModel();
        vm.Offers = Mapper.Map<IList<RoomType>, IList<OfferVM>>(roomtypes);
       return View(vm);
    }

The controller works fine - I can see it populated in VS. 控制器工作正常 - 我可以看到它填充在VS.

My Create.cshtml code is: 我的Create.cshtml代码是:

@model FGBS.ViewModels.CreateViewModel
@{
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(x => x.Offers) 

    </table>

Then in my Views/Shared/EditorTemplates folder I have my Offers.cshtml file: 然后在我的Views / Shared / EditorTemplates文件夹中,我有我的Offers.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>

SS

However, when I run the code, I get no error, but my template doesn't display. 但是,当我运行代码时,我没有收到任何错误,但我的模板没有显示。

I have tried changing the template to just have: 我已经尝试将模板更改为:

<tr><td>test</td></tr>

But it still doesn't display - which suggests the EditorFor is not "finding" the Offers.cshtml file in the editortemplates folder. 但它仍然没有显示 - 这表明EditorFor没有“查找”editortemplates文件夹中的Offers.cshtml文件。

I have also tried adding a breakpoint in Offers.cshtml, but VS doesn't stop within it, implying it isn't being hit. 我也尝试在Offers.cshtml中添加一个断点,但是VS并没有在其中停止,这意味着它没有被击中。

Is there anything wrong with my setup, or the placement of the Offers.cshtml file? 我的设置或Offers.cshtml文件的位置有什么问题吗?

Thank you, 谢谢,

Mark 标记

To provide an answer for future visitors: 为未来的访客提供答案:

As noted in this other question , by default, the EditorFor helper uses the template whose name matches the name of the type being edited. 其他问题所述 ,默认情况下, EditorFor帮助程序使用名称与正在编辑的类型名称匹配的模板。 So here, your template not being used because its name Offers did not match the viewmodel type of OfferVM . 所以在这里,您的模板没有被使用,因为它的名称OffersOfferVM的viewmodel类型不匹配。 An alternative is to mark the property you want an EditorFor for with the [UIHint("TemplateName")] attribute, where TemplateName is the name of your editor template, which in this case would be Offers . 另一种方法是使用[UIHint("TemplateName")]属性标记您想要EditorFor for的属性,其中TemplateName是编辑器模板的名称,在本例中为Offers

I litle bit late but I this would help. 我迟到了,但我会帮忙。

you could include the template name like this. 你可以包含这样的模板名称。 I used this: 我用过这个:

@Html.EditorFor(model => model.DateOfBirth, "DateTime", new { htmlAttributes = new { @class = "form-control" } }) @ Html.EditorFor(model => model.DateOfBirth,“DateTime”,new {htmlAttributes = new {@class =“form-control”}})

you write in quotes your template. 你在引号中写下你的模板。

In your case would be something like this 在你的情况下会是这样的

@Html.EditorFor(model => model.RoomTypeName, "Offer"). @ Html.EditorFor(model => model.RoomTypeName,“Offer”)。

Make sure Offer template expect the type (@model type) you are sending within the EditorFor 确保Offer模板需要您在EditorFor中发送的类型(@model类型)

Hope this help 希望这有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Asp.Net MVC EditorFor带有模板的阵列 - Asp.Net MVC EditorFor Array with template 在 C# 和 ASP.NET MVC 剃刀视图中使用数据库中的值填充 css 属性 - Populate css property with value from database in C# & ASP.NET MVC razor view 在ASP.NET MVC Razor View中将class属性设置为Html.EditorFor - Set the class attribute to Html.EditorFor in ASP.NET MVC Razor View 在Foreach或For循环中使用EditorFor(ASP.NET MVC + RAZOR) - Using EditorFor in Foreach or For loop (ASP.NET MVC + RAZOR) 如何使用asp.net mvc和razor模板引擎从c#中获取路由名称的URL? - How can I get the URL from route's name in c# using asp.net mvc and the razor template engine? 将数据从内部网格视图传递到主网格视图(gridview 模板中的网格视图)ASP.net C# - Pass Data from inner grid View to main grid view (gridview in gridview template)ASP.net C# 如何使用Asp.Net C#MVC 4 Razor Devexpress在视图中呈现gridview。 - How to render a gridview in the view using Asp.Net C# MVC 4 Razor Devexpress. 如何像在c#中的html解析器一样解析asp.net mvc剃刀视图(cshtml) - how parse asp.net mvc razor view (cshtml) like html parser in c# 我们如何将javascript var初始化为c#var Asp.net MVC Razor View - how we initialize javascript var to c# var Asp.net MVC Razor View 为什么 Html.Hidden 在 C# ASP.NET MVC 剃刀视图中不起作用? - Why Html.Hidden is not working in C# ASP.NET MVC razor view?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM