reZach
reZach

Reputation: 9429

Using a custom editor template for list property

I've almost got it working, but I am missing a tiny bit of knowledge to make this work, it is a quick fix I know it.

I am putting two ViewModels into a single view:

public class MajorViewModel
{
    public ICollection<Major> Majors { get; set; }
}
public class MinorViewModel
{
    public ICollection<Minor> Minors { get; set; }
}

Into a view like so:

<snip>
@Html.Partial("_MajorPartial")
@Html.Partial("_MinorPartial")
<snip>

Whose views look like:

// _MajorPartial
@using Microsoft.AspNet.Identity
@model ClassPlannerMVC.ViewModels.MajorViewModel

@using (Html.BeginForm())
{
     //@Html.EditorFor(m => m.Majors) ?? I'm not able to use this here
}

// _MinorPartial
looks the same except "Major" is changed to "Minor"

And then I have two views in Views/Shared/EditorTemplates/Major.cshtml and Minor.cshtml

// Major.csthml
@model ClassPlannerMVC.Models.Major

@Html.HiddenFor(m => m.ID)

<div class="form-group">
    @Html.LabelFor(m => m.Name, new { @class = "col-md-2 control-label" })
    <div class="col-md-10">
        @Html.EditorFor(m => m.Name, new { @class = "form-control" })
    </div>
</div>

// Minor.cshtml
looks the same except "Major" is changed to "Minor"

My two models are:

public class Major
{
    public int ID { get; set; }
    public string Name { get; set; }

    public virtual ICollection<UserHasMajor> UserHasMajors { get; set; }
}

public class Minor
{
    public int ID { get; set; }
    public string Name { get; set; }

    public virtual ICollection<UserHasMinor> UserHasMinors { get; set; }
}

Currently the view doesn't show my view models (MajorViewModel, MinorViewModel), which will display editors for Major and Minor respectively. I know I might be passing something wrong into the _MajorPartial and _MinorPartial views, and that is why I cannot add a @Html.EditorFor(x => x.Majors) which will display my custom editor template. There is something very minor missing in my code, do you spot the error I am having?

I'll upload more code if it is necessary.

Upvotes: 0

Views: 157

Answers (1)

user3559349
user3559349

Reputation:

You don't appear to be passing anything to your @Partial() methods (i.e.the model is null). I suggest that you create another view model to hold both and pass that the the main view, something like

public class CombinedViewModel
{
  public MajorViewModel MajorVM { get; set; }
  public MinorViewModel MinorVM { get; set; }
}

In you [HttpGet] method, populate the view model and pass to the main view

public ActionResult Details()
{
  CombinedViewModel model = new CombinedViewModel();
  // set properties
  model.MajorVM = new MajorVM();
  model.MajorVM.Majors = // assign your collection
  ..
  return View(model);
}

Then in the main view

@model YourAssembly.CombinedViewModel
...
@Html.Partial("_MajorPartial", Model.MajorVM)
@Html.Partial("_MinorPartial", Model.MinorVM)

Aternatively, just have one view model with properties public ICollection<Major> Majors { get; set; } and public ICollection<Minor> Minors { get; set; } (not sure if you had omitted some other properties for brevity and therefore you needed to have separate view models)

Upvotes: 1

Related Questions