locked
Redirecting child object to parent page RRS feed

  • Question

  • User-893417126 posted

    Hi, I am currently working on an asp.net core project with EF. I had to create a popup dialog in order to add an object to a specific parent. but after I add the object I get the view of all the children from all different parents in one view. the hierarchy here is (Parent, child-parent, child) and thus projects have surveys and surveys have questions. I had the same problem with projects(Parent) and surveys(Child) but I could solve it. but this issue appears again with surveys(Parent) and questions(Child), while adding a question it can be done with a pop up dialog and then it suppose to post it in a view only for the parent,here is the code for it

    showInPopup = (url, title) => {
        $.ajax({
            type: "GET",
            url: url,
            success: function (res) {
                $("#form-modal .modal-body").html(res);
                $("#form-modal .modal-title").html(title);
                $("#form-modal").modal('show');
            }
        })
    }
    
    jQueryAjaxPost = form => {
        try {
            $.ajax({
                type: 'POST',
                url: form.action,
                data: new FormData(form),
                contentType: false,
                processData: false,
                success: function (res) {
                    if (res.isValid) {
                        $("#view-all").html(res.html);
                        $("#form-modal .modal-body").html('');
                        $("#form-modal .modal-title").html('');
                        $("#form-modal").modal('hide');
                    }
                    else 
                        $("#form-modal .modal-body").html(res.html);  
                },
                error: function (err) {
                    console.log(err);
                }
            })
    
        } catch (e) {
            console.log(e);
        }
    and here is the Post create action method from the child controller:
            // POST: Questions/AddOrEdit
            // To protect from overposting attacks, enable the specific properties you want to bind to, for 
            // more details, see http://go.microsoft.com/fwlink/?LinkId=317598.
            [HttpPost]
            [ValidateAntiForgeryToken]
            public async Task<IActionResult> AddOrEdit(decimal id, [Bind("SurveyId,Page,Ranking,ParentId,Itemtype,Controltype,Displaytype,VariableDescription,VariableName,VariableLabel,ValueLabelId,MissingValueId,IsRequired,DataType,VariableLength,NrOfDecimals,Comment,CommentType,ShowAllForStudy,AnswerFont,IsDeleteQuestion,IsStopQuestion,RoutingType,RoutingValue,Show,QuestionId")] Question question)
            {
                if (ModelState.IsValid)
                {
                    if (id == 0)
                    {
                        _context.Add(question);
                        await _context.SaveChangesAsync();
                        //return RedirectToAction(nameof(Index), new { id = question.SurveyId });
                    }
                    else
                    {
                    try
                    {
                        _context.Update(question);
                        await _context.SaveChangesAsync();
                    }
                    catch (DbUpdateConcurrencyException)
                    {
                        if (!QuestionExists(question.QuestionId))
                        {
                            return NotFound();
                        }
                        else
                        {
                            throw;
                        }
                    }
                    }
    
                    //return RedirectToAction(nameof(Index), new{id=question.SurveyId});
                    return Json(new { isValid = true, html = Helper.RenderRazorViewToString(this, "_ViewAll", _context.Question.ToList()) });
                }
                ViewData["SurveyId"] = new SelectList(_context.Survey, "SurveyId", "SurveyId", question.SurveyId);
                //return View(question);
                return Json(new { isValid = false, html = Helper.RenderRazorViewToString(this, "AddOrEdit", question) });
                }
    

    i want to get the view of the children that belongs to the selected parent before adding the child because now i get the view of all children of all parents.

    Thursday, November 12, 2020 12:14 PM

Answers

  • User1535942433 posted

    Hi ziad-zeus,

     // POST: Questions/AddOrEdit
            // To protect from overposting attacks, enable the specific properties you want to bind to, for 
            // more details, see http://go.microsoft.com/fwlink/?LinkId=317598.
            [HttpPost]
            [ValidateAntiForgeryToken]
            public async Task<IActionResult> AddOrEdit(decimal id, [Bind("SurveyId,Page,Ranking,ParentId,Itemtype,Controltype,Displaytype,VariableDescription,VariableName,VariableLabel,ValueLabelId,MissingValueId,IsRequired,DataType,VariableLength,NrOfDecimals,Comment,CommentType,ShowAllForStudy,AnswerFont,IsDeleteQuestion,IsStopQuestion,RoutingType,RoutingValue,Show,QuestionId")] Question question)
            {
                if (ModelState.IsValid)
                {
                    if (id == 0)
                    {
                        _context.Add(question);
                        await _context.SaveChangesAsync();
                        //return RedirectToAction(nameof(Index), new { id = question.SurveyId });
                    }
                    else
                    {
                    try
                    {
                        _context.Update(question);
                        await _context.SaveChangesAsync();
                    }
                    catch (DbUpdateConcurrencyException)
                    {
                        if (!QuestionExists(question.QuestionId))
                        {
                            return NotFound();
                        }
                        else
                        {
                            throw;
                        }
                    }
                    }
    
                    //return RedirectToAction(nameof(Index), new{id=question.SurveyId});
                    return Json(new { isValid = true, html = Helper.RenderRazorViewToString(this, "_ViewAll", _context.Question.ToList()) });
                }
                ViewData["SurveyId"] = new SelectList(_context.Survey, "SurveyId", "SurveyId", question.SurveyId);
                //return View(question);
                return Json(new { isValid = false, html = Helper.RenderRazorViewToString(this, "AddOrEdit", question) });
                }

    As far as I think,when you return the json, you need to filter the result instead of the Question list.

    Just like this:

     // POST: Questions/AddOrEdit
            // To protect from overposting attacks, enable the specific properties you want to bind to, for 
            // more details, see http://go.microsoft.com/fwlink/?LinkId=317598.
            [HttpPost]
            [ValidateAntiForgeryToken]
            public async Task<IActionResult> AddOrEdit(decimal id, [Bind("SurveyId,Page,Ranking,ParentId,Itemtype,Controltype,Displaytype,VariableDescription,VariableName,VariableLabel,ValueLabelId,MissingValueId,IsRequired,DataType,VariableLength,NrOfDecimals,Comment,CommentType,ShowAllForStudy,AnswerFont,IsDeleteQuestion,IsStopQuestion,RoutingType,RoutingValue,Show,QuestionId")] Question question)
            {
                if (ModelState.IsValid)
                {
                    if (id == 0)
                    {
                        _context.Add(question);
                        await _context.SaveChangesAsync();
                        //return RedirectToAction(nameof(Index), new { id = question.SurveyId });
                    }
                    else
                    {
                    try
                    {
                        _context.Update(question);
                        await _context.SaveChangesAsync();
                    }
                    catch (DbUpdateConcurrencyException)
                    {
                        if (!QuestionExists(question.QuestionId))
                        {
                            return NotFound();
                        }
                        else
                        {
                            throw;
                        }
                    }
                    }
                  ViewData["SurveyId"] = new SelectList(_context.Survey, "SurveyId", "SurveyId", question.SurveyId);
                    //return RedirectToAction(nameof(Index), new{id=question.SurveyId});
                    return Json(new { isValid = true, html = Helper.RenderRazorViewToString(this, "_ViewAll", _context.Question.where(m=> m.SurveyId == question.SurveyId)).toList() });
                }
               
                //return View(question);
                return Json(new { isValid = false, html = Helper.RenderRazorViewToString(this, "AddOrEdit", question) });
                }

    Best regards,

    Yijing Sun

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, November 13, 2020 8:08 AM

All replies

  • User1535942433 posted

    Hi ziad-zeus,

     // POST: Questions/AddOrEdit
            // To protect from overposting attacks, enable the specific properties you want to bind to, for 
            // more details, see http://go.microsoft.com/fwlink/?LinkId=317598.
            [HttpPost]
            [ValidateAntiForgeryToken]
            public async Task<IActionResult> AddOrEdit(decimal id, [Bind("SurveyId,Page,Ranking,ParentId,Itemtype,Controltype,Displaytype,VariableDescription,VariableName,VariableLabel,ValueLabelId,MissingValueId,IsRequired,DataType,VariableLength,NrOfDecimals,Comment,CommentType,ShowAllForStudy,AnswerFont,IsDeleteQuestion,IsStopQuestion,RoutingType,RoutingValue,Show,QuestionId")] Question question)
            {
                if (ModelState.IsValid)
                {
                    if (id == 0)
                    {
                        _context.Add(question);
                        await _context.SaveChangesAsync();
                        //return RedirectToAction(nameof(Index), new { id = question.SurveyId });
                    }
                    else
                    {
                    try
                    {
                        _context.Update(question);
                        await _context.SaveChangesAsync();
                    }
                    catch (DbUpdateConcurrencyException)
                    {
                        if (!QuestionExists(question.QuestionId))
                        {
                            return NotFound();
                        }
                        else
                        {
                            throw;
                        }
                    }
                    }
    
                    //return RedirectToAction(nameof(Index), new{id=question.SurveyId});
                    return Json(new { isValid = true, html = Helper.RenderRazorViewToString(this, "_ViewAll", _context.Question.ToList()) });
                }
                ViewData["SurveyId"] = new SelectList(_context.Survey, "SurveyId", "SurveyId", question.SurveyId);
                //return View(question);
                return Json(new { isValid = false, html = Helper.RenderRazorViewToString(this, "AddOrEdit", question) });
                }

    As far as I think,when you return the json, you need to filter the result instead of the Question list.

    Just like this:

     // POST: Questions/AddOrEdit
            // To protect from overposting attacks, enable the specific properties you want to bind to, for 
            // more details, see http://go.microsoft.com/fwlink/?LinkId=317598.
            [HttpPost]
            [ValidateAntiForgeryToken]
            public async Task<IActionResult> AddOrEdit(decimal id, [Bind("SurveyId,Page,Ranking,ParentId,Itemtype,Controltype,Displaytype,VariableDescription,VariableName,VariableLabel,ValueLabelId,MissingValueId,IsRequired,DataType,VariableLength,NrOfDecimals,Comment,CommentType,ShowAllForStudy,AnswerFont,IsDeleteQuestion,IsStopQuestion,RoutingType,RoutingValue,Show,QuestionId")] Question question)
            {
                if (ModelState.IsValid)
                {
                    if (id == 0)
                    {
                        _context.Add(question);
                        await _context.SaveChangesAsync();
                        //return RedirectToAction(nameof(Index), new { id = question.SurveyId });
                    }
                    else
                    {
                    try
                    {
                        _context.Update(question);
                        await _context.SaveChangesAsync();
                    }
                    catch (DbUpdateConcurrencyException)
                    {
                        if (!QuestionExists(question.QuestionId))
                        {
                            return NotFound();
                        }
                        else
                        {
                            throw;
                        }
                    }
                    }
                  ViewData["SurveyId"] = new SelectList(_context.Survey, "SurveyId", "SurveyId", question.SurveyId);
                    //return RedirectToAction(nameof(Index), new{id=question.SurveyId});
                    return Json(new { isValid = true, html = Helper.RenderRazorViewToString(this, "_ViewAll", _context.Question.where(m=> m.SurveyId == question.SurveyId)).toList() });
                }
               
                //return View(question);
                return Json(new { isValid = false, html = Helper.RenderRazorViewToString(this, "AddOrEdit", question) });
                }

    Best regards,

    Yijing Sun

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, November 13, 2020 8:08 AM
  • User-893417126 posted

    Thanks its working now i appreciate it!

    Thursday, November 19, 2020 10:45 AM