0

Recently , I'm researching that how to generate create view with nested objects(list of objects) in MVC 5 ?

My objects are like that:

 public class MrSurvey
{
    public Guid Id { get; set; }
    [Required]
    [StringLength(128)]
    public string UserId { get; set; }
    [Required]
    [StringLength(75)]
    public string Title { get; set; }
    public DateTime CreatedDate { get; set; }
    public List<MrSurveyQuestion> Questions { get; set; }
}

public class MrSurveyQuestion
{      
    public Guid Id { get; set; }
    public Guid SurveyId { get; set; }
    [Required]
    [StringLength(250)]
    public string QuestionName { get; set; }
    public bool IsMultipleChoice { get; set; }
    public bool IsSelectOneMoreThan { get; set; }
    public List<MrSurveyQption> Options { get; set; }

}
public class MrSurveyQption
{
    public Guid Id { get; set; }
    public Guid QuestionId { get; set; }
    [Required]
    [StringLength(140)]
    public string OptionName { get; set; }
}

I try to generate create view with my nested objects for days. But I didn't do it. Does anyone suggest that how to create view with these objects? or Are there any examples like that?

Thank you for your kind interest.

  • typically the nested objects would be partials views that you are rendering. – MPavlak Jul 01 '16 at 15:50
  • you can make a viewmodel and pass that to your view – Collateral.dmg Jul 01 '16 at 16:02
  • 1
    however, it is unclear exactly what you're trying to do.... maybe post your view to show us what you've tried?? – Collateral.dmg Jul 01 '16 at 16:03
  • Refer te answers [here](http://stackoverflow.com/questions/29161481/post-a-form-array-without-successful/29161796#29161796) and [here](http://stackoverflow.com/questions/28019793/submit-same-partial-view-called-multiple-times-data-to-controller/28081308#28081308) –  Jul 01 '16 at 22:50

1 Answers1

0
  1. You can create a new class which contains all the classes that you want to have on the view. Then use it to pass through the view.

    public class YourNewClass{
        public MrSurvey mrSurvey {get;set;}
        public MrSurveyQuestion mrSurveyQuestion {get;set;}
        public MrSurveyQption mrSurveyQption {get;set;} }
    
  2. You can do like @MPavlak comment: use partial view for each of your objects by using @Html.Action to get partial views with different objects.

On your view

    @Html.Action("mrsurvey_method")
    @Html.Action("mrsurveyquestion_method")
    @Html.Action("mrsurveyqption_method")

On the controller

    public ActionResult mrsurvey_method(){
        //do anything you want
        return PartialView("_partialview1", new MrSurvey());
    }
    public ActionResult mrsurvey_method(){
        //do anything you want
        return PartialView("_partialview2", new MrSurveyQuestion());
    }
    public ActionResult mrsurvey_method(){
        //do anything you want
        return PartialView("_partialview3", new MrSurveyQption());
    }

Reference:How can I use Html.Action?

Community
  • 1
  • 1
TranQ
  • 139
  • 1
  • 7