Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. It's as if it just isn't being found in the EditorTemplates folder:

My ViewModel is:

    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.

My Create.cshtml code is:

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

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

I have also tried adding a breakpoint in Offers.cshtml, but VS doesn't stop within it, implying it isn't being hit.

Is there anything wrong with my setup, or the placement of the Offers.cshtml file?

Thank you,

Mark

like image 872
Mark Avatar asked Jul 03 '13 14:07

Mark


People also ask

What is ASP.NET C?

ASP.NET is a web application framework developed and marketed by Microsoft to allow programmers to build dynamic web sites. It allows you to use a full featured programming language such as C# or VB.NET to build web applications easily.

Can I use .NET with C?

. NET Framework is an object oriented programming framework meant to be used with languages that it provides bindings for. Since C is not an object oriented language it wouldn't make sense to use it with the framework.

Is ASP.NET like C#?

ASP.NET is a web application development framework used to develop web applications using different back-end programming languages like C# where C# is used as an object-oriented programming language to develop web applications along with ASP.NET.

Is .NET and C same?

In summary, C# is a programming language, while . NET is a developer platform. After comparing C# vs . NET, it is clear that both are essential for application development.


1 Answers

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. So here, your template not being used because its name Offers did not match the viewmodel type of OfferVM. 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.

like image 188
anaximander Avatar answered Sep 27 '22 23:09

anaximander