问题
I'm having a requirement where the number of partial views could grow tomorrow and they could be a composition of any number of values and of any type. Well, yes, I can do this using partial views itself but the time I will add a new partial add, I will be required to recompile the application that I want to avoid. It would be very like a CMS where you just specify the fields and the form is generated on the fly based on fields and their type you specify.
Edit 1
Let's say for example you're building a survey application where you have multiple types of question and have associated partial views for every type. Now, if tomorrow you need to add one or more question types- how would you create a partial view dynamically on the fly for the new question type? This is where the idea come from to store view definitions in an XML file or in Database so that the you can just add an entry for new partial view and you're good to display a new view for new question type without re-compiling > restarting your server.
Can we do something like that in ASP.NET MVC 5 using a data store (Any DB: SQL Server / MySQL or XML File / Flat File)? Any thoughts, pointers, tips are highly appreciated!
please correct if I'm incorrect.
回答1:
Yes you can use a objectContainer that has multiple values:
Model of partial views:
public List<DynamicQuestion> dynamicQuestionList { get; set; }
public class DynamicQuestion
{
public string question{ get; set; }
public string ask{ get; set; }
}
wth that that you can get a List of DynamicQuestion so it's ok for you
in Db, you should have a table "question" that contains
id, question
that host all questions
and a table "ask" that has
id, idQuestion, response
that save all ask
来源:https://stackoverflow.com/questions/27353965/load-partial-views-from-database