Rob
Rob

Reputation: 426

@Html.DropDownListFor fails in partial view but not in Full Page View

I am having an issue where my PartialView DropDownListFor gets the error:

The ViewData item that has the key PlanId is of type System.int32 but must be of type   
IEnumerable<SelectListItem>

@Html.DropDownListFor(model => model.PlanId, (SelectList)ViewBar.PlanNameSelectList, new {@class = "short" })  

This error does not pop up when I go to the page that originally held this code. What I have done is gutted the core part of the code which has worked previously with another partialView, as long as I took out the DropDownListFor elements in the code. I did not need them for that partialView, but now that I need them the problem has come full circle.

I would greatly appreciate any help that can be given to me to help solve this problem. Other resources like calls to the partial are below

@Html.Partial("location", new MAO.Models.ViewModels.CreateTemplateModel{})

This is the model

public class CreateTemplateModel {
    [Required(ErrorMessage = "{0} is required.")]
    [RegularExpression("^[0-9]+$", ErrorMessage="Template Id can only contain numbers")]
    [Display(Name = "Template ID")]
    public string TNumber { get; set; }

    [Required(ErrorMessage = "{0} is required.")]
    [RegularExpression("^.[0-9]{4}(-[0-9]{3})?$", ErrorMessage = "H# Must follow either @XXXX or @XXXX-XXX pattern")]
    [Display(Name = "HNumber")]
    public string HNumber { get; set; }

    [RequiredIfOtherIsEmpty("NewPlanName", ErrorMessage = "Please enter a Plan Name")]
    [Display(Name = "Select Existing Plan Name")]
    public int PlanId { get; set; }

    [MaxLength(500, ErrorMessage="{0} can't be longer than 500 characters")]
    [Display(Name = "Enter New Plan Name")]
    public string NewPlanName { get; set; }

    [RequiredIfOtherIsEmpty("NewParentOrganization", ErrorMessage = "Please enter a Parent Organization")]
    [Display(Name = "Select Existing Parent Organization")]
    public string ParentOrganization { get; set; }

    [MaxLength(500, ErrorMessage = "{0} can't be longer than 500 characters")]
    [Display(Name = "Enter New Parent Organization")]
    public string NewParentOrganization { get; set; }

    [Required(ErrorMessage = "{0} is required.")]
    public int TemplateTypeId { get; set; }
}

There is a controller that is pretty long so I am not going to post that. If there are parts of the controller that would be helpful I can post those parts as well as anything else that I might have forgotten to include

Upvotes: 0

Views: 288

Answers (1)

David L
David L

Reputation: 33863

Based on your comments, I'm suspecting that you're never rebinding your drop down list when you are returning your partial view. Your controller action for the partial should be building your dropdown list in an identical manner to the controller action that renders the full view. Compare the two and make sure that they match.

UPDATE: Your partial view action should look something like the following:

public ActionResult Location()
{
    ViewBag.PlanNameSelectList = new SelectList(plans.Distinct(), "Id", "Name", plans.FirstOrDefault(plan => plan.Name == selectedPlan));
    attachSelectLists(ViewBag); 
    return PartialView("Location");
}

What you are currently doing with

@Html.Partial("location", new MAO.Models.ViewModels.CreateTemplateModel{})

Is rendering the partial view "location" using a NEW CreateTemplateModel object, not an existing one. Instead, a better way to do it is to duplicate your controller actions. Create a new one specifically for your partial view (this is a simpler use case for now).

public ActonResult TestPartialView()

Instead of using @Html.Partial which renders a partial, try calling your new controller action instead, which will build your drop down list for you.

@Html.RenderAction("TestPartialView").

This will call your new controller action and render the partial on the page, preserving the controller logic. If you use @Html.Partial, it simply renders the partial view passing in whatever object you give it which, in this case, is a new, empty CreateTemplateModel.

Upvotes: 2

Related Questions