How do I use editortemplates in MVC3 for complex types?
Asked Answered
C

1

6

I have two classes, Vat and Product. Product has a property of IVat. I am trying to use editor templates in MVC to display a dropdown list of all the Vat objects when creating/editing a Product. For the dear life of me I cannot get this working.

I have the following code which displays the dropdown but it does not set the Vat for the Product when the form gets submitted.

Controller:

IList<IVatRate> vatRates = SqlDataRepository.VatRates.Data.GetAllResults();
ViewBag.VatRates = new SelectList(vatRates, "Id", "Description");

Add.cshtml

@Html.EditorFor(model => model.VatRate.Id, "VatSelector", (SelectList)ViewBag.VatRates)

VatSelector.cshtml

@model SelectList
@Html.DropDownList(
        String.Empty /* */,
            (SelectList)ViewBag.Suppliers, 
        Model
    )

I would be grateful if anyone can shed some light on this or even point me to a good example on the web somewhere...I have been stuck with this for quite a few days now.

Coz answered 12/6, 2011 at 19:22 Comment(0)
P
7

I would use strongly typed views and view models as it makes things so much easier rather than ViewBag.

So start with a view model:

public class VatRateViewModel
{
    public string SelectedVatRateId { get; set; }
    public IEnumerable<IVatRate> Rates { get; set; }
}

then a controller:

public class HomeController: Controller
{
    public ActionResult Index()
    {
        var model = new VatRateViewModel
        {
            Rates = SqlDataRepository.VatRates.Data.GetAllResults()
        };
        return View(model);
    }

    [HttpPost]
    public ActionResult Index(VatRateViewModel model)
    {
        // model.SelectedVatRateId will contain the selected vat rate id
        ...
    }
}

View:

@model VatRateViewModel
@using (Html.BeginForm())
{
    @Html.DropDownListFor(
        x => x.SelectedVatRateId,
        new SelectList(Model.Rates, "Id", "Description")
    )
    <input type="submit" value="OK" />
}

And if you wanted to use editor template for the VatRateViewModel you could define one in ~/Views/Shared/EditorTemplates/VatRateViewModel.cshtml:

@model VatRateViewModel
@Html.DropDownListFor(
    x => x.SelectedVatRateId,
    new SelectList(Model.Rates, "Id", "Description")
)

and then whenever somewhere you have a property of type VatRateViewModel you could simply:

@Html.EditorFor(x => x.SomePropertyOfTypeVatRateViewModel)

which would render the corresponding editor template.

Putrefy answered 12/6, 2011 at 20:27 Comment(2)
Thanks for the answer. Does the editortemplate have a separate controller? If it does not then how would my Product's values get set, and if it does, where does it go and what should it be called? I tried this before and could not get it to work.Coz
Ok, LOTS of blood, sweat and tears later I FINALLY got it sorted. I had to add all my complex types to the ViewModel, as well as my product. Thanks again!Coz

© 2022 - 2024 — McMap. All rights reserved.