Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nested Partial view passing sub object issue

I am trying to pass sub object in a partial view to another, and I always get the error. Can anyone help me to solve this? T.T

"The model item passed into the dictionary is of type 'Application.Models.PetModel', but this dictionary requires a model item of type 'Application.Models.Calendar'"

Main model

public class PetModel
{

    public string Name { get; set; }
    public long SpeciesID { get; set; }
    public long BreedID { get; set; }
    public Calendar DOB { get; set; }

}

Sub Model

   public class Calendar
    {
        public int Day { get; set; }
        public int Month { get; set; }
        public int Year { get; set; }

        public DateTime DateObj
        {
            get
            {
                if (Day != 0 && Month != 0 && Year != 0)
                {
                    return new DateTime(Year, Month, Day);
                }

                return DateTime.Now;
            }

            set
            {
                if (value != null)
                {
                    Day = value.Day;
                    Month = value.Month;
                    Year = value.Year;
                }
            }
        }

    }

Main View

@model Application.Models.PetModel
@using (Html.BeginForm("CatchPetContent", "Quote",Model))
{
    @Html.Partial("PetDetailsContent", Model)
    <input type="submit" value="submit" />
}

PetDetailsContent Partial View

@model Application.Models.PetModel
@Html.TextBoxFor(x => x.Name)
@Html.DropDownListFor(x => x.SpeciesID, (IEnumerable<SelectListItem>)ViewData["TypeList"], "--Please Select--")
 @Html.DropDownListFor(x => x.BreedID, (IEnumerable<SelectListItem>)ViewData["BreedList"], "--Please Select--")
@Html.RenderPartial("UserControl/Calendar", Model.DOB)

Calendar Partial view

@model Application.Models.Calendar
 @Html.TextBoxFor(x => x.Day)
 @Html.TextBoxFor(x => x.Month)
 @Html.TextBoxFor(x => x.Year)
like image 532
user2376512 Avatar asked May 14 '13 06:05

user2376512


People also ask

Can we call partial view inside view?

Partial function. In order to add Partial View, you will need to Right Click inside the Controller class and click on the Add View option in order to create a View for the Controller.

Can we use multiple partial view in MVC?

You can only return one value from a function so you can't return multiple partials from one action method.

Can we call partial view inside partial view in MVC?

Yes you can do that.


2 Answers

I have got the same problem. In my case whenever the sub-model is null, framework is passing the main model to the partial view.

As a workaround I check if the sub-model is null before passing it to the partial view. If its null then I either dont show the partial view at all or create an instance of the sub-model. (Again, its a workaround until I find the proper solution for this issue. If there is one.)

like image 130
Oxon Avatar answered Nov 15 '22 06:11

Oxon


Change This,

@Html.RenderPartial("UserControl/Calendar", Model.DOB)

you have DOB property of type Calender in your PetModel model.

like image 36
Shivkumar Avatar answered Nov 15 '22 06:11

Shivkumar