Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use asp.net mvc EditorTemplate

I read that EditorTemplates are loaded automatically, but from asp.net mvc 2 and now 3 with razor, I still cant get this to work.

My model looks like this:

public class RoleViewModel
{
    public int RoleId { get; set; }
    public bool InRole { get; set; }
    public string RoleName { get; set; }
}

public class UserViewModel
{
    public User User { get; set; }
    public IEnumerable<RoleViewModel> Roles { get; set; }
}

My view looks like this:

~/Views/Roles/Edit.cshtml

@model Project.Web.ViewModel.UserViewModel
@using (Html.BeginForm()) {
   @Html.EditorFor(model => model.Roles)
   <!-- Other stuff here -->
}

~/Views/Roles/EditorTemplates/RoleViewModel.cshtml

@model Project.Web.ViewModel.RoleViewModel
@foreach (var i in Model)
{
    <div>
    @i.RoleName
    @Html.HiddenFor(model => i.RoleId)
    @Html.CheckBoxFor(model => i.InRole)
    </div>
}

If i move the content from the EditorTemplate to the actual page, then it works, it shows the checkbox, etc. But with this current setup, all that shows up is the count of the number of roles.

What am I doing wrong?

like image 506
Shawn Mclean Avatar asked Jan 12 '11 16:01

Shawn Mclean


People also ask

What is MVC editor template?

An EditorTemplate is a Razor file placed in the EditorTemplates folder: For Razor Pages apps, in the Pages/Shared/EditorTemplates folder. For MVC apps, in the Views/Shared/EditorTemplates folder or the Views/ControllerName/EditorTemplates folder.

What is templated HTML helpers in MVC?

The Html. EditorFor templated helper also renders validation logic that enforces constraints that are built into the data model or that you can add as System. ComponentModel. DataAnnotations attributes to a partial class that is associated with the data model.

What is HTML DisplayFor?

DisplayFor() The DisplayFor() helper method is a strongly typed extension method. It generates a html string for the model object property specified using a lambda expression.

What is HTML EditorFor?

The Html. Editor() or Html. EditorFor() extension methods generate HTML elements based on the data type of the model object's property. The following table list the data types and releted HTML elements: DataType.


1 Answers

~/Views/Roles/EditorTemplates/RoleViewModel.cshtml

@model MvcApplication16.Controllers.RoleViewModel
<div>
    @Model.RoleName
    @Html.HiddenFor(m => m.RoleId)
    @Html.CheckBoxFor(m => m.InRole)
</div>

~/Views/Roles/Edit.cshtml

@model MvcApplication16.Controllers.UserViewModel
@using (Html.BeginForm()) {
   @Html.EditorFor(m => m.Roles)
   <!-- Other stuff here -->
}

Models

public class UserViewModel {
    public User User { get; set; }
    public IEnumerable<RoleViewModel> Roles { get; set; }
}

public class RoleViewModel {
    public int RoleId { get; set; }
    public bool InRole { get; set; }
    public string RoleName { get; set; }
}

public class User {
    public string Name { get; set; }
}

Controller

public ActionResult Edit() {
    return View(
        new UserViewModel() {
            User = new User() { Name = "Test" },
            Roles = new List<RoleViewModel>() { 
                new RoleViewModel() { 
                    RoleId = 1, 
                    InRole = true, 
                    RoleName = "Test Role" }}
        });
}

The above code works just fine. Compare it with yours and see if you see anything amiss :)

like image 52
Buildstarted Avatar answered Dec 09 '22 17:12

Buildstarted