Home > Enterprise >  Open new tab html mvc
Open new tab html mvc

Time:03-16

I am trying to open a new tab as long as certain conditions are met. To do this, I understand that target="_blank" is used in the html form, the problem with this is that they will open regardless of whether or not the condition is met. How can I make it happen only when the condition is met?

This is my ActionResult with which I control the conditions:

public ActionResult ChAzul(double? titulo)
{
    ConexionSQL cn = new ConexionSQL();
    var suscriptor = cn.cargarDatos(Convert.ToDouble(titulo));
    var caracteres = Convert.ToString(titulo).Length;
    string uname = string.Empty;
    if (Session["uname"] != null)
    {
        uname = Convert.ToString(Session["uname"]);
    }
    var usuario = cn.datosCob(uname);
    if (uname == string.Empty)
    {
        return RedirectToAction("Index", "Home");
    }
    else if (usuario[0].conectado == false)
    {
        return RedirectToAction("Index", "Home");
    }
    else if (caracteres <= 3 || caracteres > 6)
    {
        ViewBag.Alert = "La cantidad de caracteres no puede ser menor a 4 (cuatro) ni mayor a 6 (seis).";
        return View("Cuotas", usuario);
    }
    else if (suscriptor.Count <= 0)
    {
        ViewBag.Alert = "Lo sentimos, este título no existe.";
        return View("Cuotas", usuario);
    }
    else
    {
        return View("ChAzul", suscriptor);
    }
}

Only in this line return View("ChAzul", suscriptor); is when the new tab should open, how can I achieve it?

This is my view for what it's worth:

    <form id="frmCU" method="post" action="@Url.Action("ChAzul", "Home")">
        <label for="titulo">Título: </label>
        <input type="number" id="titulo" oninput="javascript: if (this.value.length > this.maxLength) this.value = this.value.slice(0, this.maxLength);" name="titulo" maxlength="6" placeholder="Ingrese su título..." required
               title="Sólo letras y números. Cantidad mínima de caracteres: 4. Cantidad máxima de caracteres: 5"
               onkeypress="return soloNumeros(event)" autofocus>
        <input type="submit" value="Buscar"/>
        @if (ViewBag.Alert != null)
        {
            <div >
                <span >&times;</span>
                <strong>Providus informa: </strong>
                <p id="textoAlerta">@ViewBag.Alert</p>
            </div>
        }
    </form>

CodePudding user response:

What you are asking cannot be done from within the Controller itself.

You need it do this on from your View. For this you can apply the target attribute on your form while posting to your Controller.

Now you have already mentioned that you do not want this to happen but rather you want it to happen on a condition inside your Controller:

<form id="frmCU" method="post" action="@Url.Action("ChAzul", "Home")" target="_blank">

So in order to do this, you can use AJAX to get a response from from your Controller method and based on that, you can redirect to your ChAzul view in a new tab . In simplest terms, your call would something like:

$.ajax({
    url: 'Home/ChAzul',
    type: 'GET',
    dataType: 'json',
    data: {
        titulo: 1
    },
    success: function(data) {
      if(data.status=="true")
      {
        var urlToRedirect= '@Url.Action("ChAzul","Home")';
        window.location.href = urlToRedirect; //Redirect here
      }
    },
    error: function() {
        alert('fail');
        window.location.reload();
    }
});

And your Controller will look like this:

[HttpGet]
public JsonResult ChAzul(double? titulo)
{
    //Your logic here

    return Json(new {status="true", msg= "Successful authentication"}, JsonRequestBehavior.AllowGet);
}
  • Related