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

Asked

Viewed 70 times

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 the way, this is the code for the home 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>
    <script src="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 view "Dadospessoais/Addoredit.cshtml", there is a controller called "Dadospessoaiscontroller.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");
        }
    }
}
  • Has the code???

  • I put my code in the initial post.

1 answer

0

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.