简体   繁体   中英

Add XElement dynamically using loop in MVC 4?

i will like to add the Xelement in Xdocument but how can i add in dynamically using loop? I need to capture the user multiple answer and generate the Xelement dynamically. Please guide me or provide any hint. Thanks alot!

    [HttpPost]
        public ActionResult SURV_Answer_Submit(List<AnswerQuestionViewModel> viewmodel, int Survey_ID, string Language)
        {
            if (ModelState.IsValid)
            {
                  var query = from r in db.SURV_Question_Ext_Model.ToList()
                        join s in db.SURV_Question_Model
                        on r.Qext_Question_ID equals
                        s.Question_ID
                        where  s.Question_Survey_ID == Survey_ID && r.Qext_Language == Language
                        orderby s.Question_Position ascending
                        select new { r, s };

                int i = 0;

                foreach(var item in query)
                {

                        var answer = new SURV_Answer_Model();
                        answer.Answer_Qext_ID = item.r.Qext_Question_ID;
                        string value = item.s.Question_Type;

                        string str = item.r.Qext_Configuration;

                        XElement qconfig;
                        qconfig = XElement.Parse(str);

                        XElement ChoicesType =
                        (from node in qconfig.Elements("ChoicesType")
                         select node).SingleOrDefault();

                         XDocument doc = new XDocument
                         (
                            new XDeclaration("1.0", "utf-8", "yes"),
                            new XComment("Question Configuration"),
                            new XElement("AnswerData",
                             new XElement("Answer1", viewmodel[i++].Answer),                    
                            new XElement("Answer2", viewmodel[i++].Answer), 
                            new XElement("Answer3", viewmodel[i++].Answer),
                            new XElement("Answer4", viewmodel[i++].Answer),
                            How to add dynamically for new XElement Answer2,3,4 here?


    );
                 answer.Answer_Data = doc.ToString();



                        db.SURV_Answer_Model.Add(answer);
                        db.SaveChanges();               
                }

                return RedirectToAction("SURV_Main_Index", "SURV_Main");

            }

            return View(viewmodel);
        }

Try something like this:

XDocument doc = new XDocument
                 (
                       new XDeclaration("1.0", "utf-8", "yes"),
                           new XComment("Question Configuration"),
                           new XElement("AnswerData", GetAnswerData(viewmodel));

...

public List<XElement> GetAnswerData(Object viewmodel)
    {
        var result = new List<XElement>();

        for(int x =0 ; x < viewmodel[i].MultiAnswer.Count(); x++)
        {
            result.Add(new XElement("Answer",viewmodel[i++].MultiAnswer[x])));
        }
        return result;
    }

This is the complete code

[HttpPost]
public ActionResult SURV_Answer_Submit(List<AnswerQuestionViewModel> viewmodel, int Survey_ID, string Language)
{
    if (ModelState.IsValid)
    {
          var query = from r in db.SURV_Question_Ext_Model.ToList()
                join s in db.SURV_Question_Model
                on r.Qext_Question_ID equals
                s.Question_ID
                where  s.Question_Survey_ID == Survey_ID && r.Qext_Language == Language
                orderby s.Question_Position ascending
                select new { r, s };

        int i = 0;

        foreach(var item in query)
        {

                var answer = new SURV_Answer_Model();
                answer.Answer_Qext_ID = item.r.Qext_Question_ID;
                string value = item.s.Question_Type;

                string str = item.r.Qext_Configuration;

                XElement qconfig;
                qconfig = XElement.Parse(str);

                XElement ChoicesType =
                (from node in qconfig.Elements("ChoicesType")
                 select node).SingleOrDefault();

                 XDocument doc = new XDocument
                 (
                    new XDeclaration("1.0", "utf-8", "yes"),
                    new XComment("Question Configuration"),
                    new XElement("AnswerData", GetAnswerData(viewmodel, i));

                answer.Answer_Data = doc.ToString();

                db.SURV_Answer_Model.Add(answer);
                db.SaveChanges();   
                i++;
        }

        return RedirectToAction("SURV_Main_Index", "SURV_Main");

    }

    return View(viewmodel);
}

public IEnumerable<XElement> GetAnswerData(List<AnswerQuestionViewModel> viewmodel, int i)
{
    for(int x =0 ; x < viewmodel[i].MultiAnswer.Count(); x++)
    {
        yield return new XElement("Answer",viewmodel[i++].MultiAnswer[x]));
    }
}

A fancier version of the answer from Oscar Fonseca :

 XDocument doc = new XDocument
 (
       new XDeclaration("1.0", "utf-8", "yes"),
           new XComment("Question Configuration"),
           new XElement("AnswerData", GetAnswerData(viewmodel));
    //...

public IEnumerable<XElement> GetAnswerData(Object viewmodel)
{
    for(int x =0 ; x < viewmodel[i].MultiAnswer.Count(); x++)
    {
        yield return new XElement("Answer",viewmodel[i++].MultiAnswer[x]));
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM