ASP.NET - Partial View inside tab does not invoke initializer method

0

I have a Razor Page with different tabs. And on each tab, I invoke a partial view in order to create a multi-step form. However, I noticed that the Index method (which is in its respective controller) that is usually invoked when a view is initialized is not invoked. How can I get around this obstacle?

By now, this is the code relative to the main page:

@{
    Layout = null;
    if(Session["userID"]==null)
    {
        Response.Redirect("~/Login/Index");
    }
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
    <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
    <link rel="stylesheet" href="/resources/demos/style.css">
    <script src="https://code.jquery.com/jquery-1.12.4.js"></script><scriptsrc="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
    <script>
      $( function() {
        $( "#tabs" ).tabs();
      } );
    </script>
</head>
<body>
    <div>
        <a class="glyphicon glyphicon-log-out" href="@Url.Action("LogOut", "Login")"></a>
    </div>

    <div id="tabs">
        <ul>
            <li><a href="#tabs-1">Dados Pessoais</a></li>
            <li><a href="#tabs-2">Inquérito</a></li>
            <li><a href="#tabs-3">Candidatura</a></li>
            <li><a href="#tabs-4">Documentos</a></li>
            <li><a href="#tabs-5">Declaração</a></li>
        </ul>
        <div id="tabs-1">
            @Html.Partial("~/Views/DadosPessoais/AddOrEdit.cshtml");
        </div>
        <div id="tabs-2">
            @Html.Partial("~/Views/Inquerito/AddOrEdit.cshtml");
        </div>
        <div id="tabs-3">

        </div>
        <div id="tabs-4">

        </div>
        <div id="tabs-5">

        </div>
    </div>


</body>
</html>

And associated with the "Personal Data / AddOrEdit.cshtml" view, there is a driver called "PersonalDataController.cs" whose code is as follows:

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Candidaturas.Models;

namespace Candidaturas.Controllers
{
    public class DadosPessoaisController : Controller
    {
        // GET: DadosPessoais
        public ActionResult Index()
        {
            LoginDataBaseEntities1 db = new LoginDataBaseEntities1();

            IEnumerable<SelectListItem> generos = db.Generoes.Select(c => new SelectListItem
            {
                Value = c.ID.ToString(),
                Text = c.Nome

            });

            ViewBag.GeneroIDs = generos.ToList();

            return View();
        }

        [HttpGet]
        // GET: User
        public ActionResult AddOrEdit(int id = 0)
        {
            DadosPessoai userModel = new DadosPessoai();
            return View(userModel);
        }

        [HttpPost]
        public ActionResult AddOrEdit(DadosPessoai dadosPessoaisModel)
        {

            using (LoginDataBaseEntities1 dbModel = new LoginDataBaseEntities1())
            {

                try
                {
                    dadosPessoaisModel.UserId = Convert.ToInt32(Session["userID"].ToString());
                    dbModel.DadosPessoais.Add(dadosPessoaisModel);
                    dbModel.SaveChanges();
                }
                catch (System.Data.Entity.Validation.DbEntityValidationException dbEx)
                {
                    Exception raise = dbEx;
                    foreach (var validationErrors in dbEx.EntityValidationErrors)
                    {
                        foreach (var validationError in validationErrors.ValidationErrors)
                        {
                            string message = string.Format("{0}:{1}", validationErrors.Entry.Entity.ToString(), validationError.ErrorMessage);
                            raise = new InvalidOperationException(message, raise);
                        }
                    }
                    throw raise;
                }
            }
            ModelState.Clear();

            return View("~View/Home/Index.cshtml");
        }
    }
}
    
asked by anonymous 02.08.2018 / 16:33

1 answer

0

I was able to solve this problem though: instead of using the @Html.Partial, I had to use the @Html.Action to invoke the Index action of the intended partial view. And then in the index of this view, I would return the view at the end.

    
03.08.2018 / 15:40