打开新标签页 html mvc

Open new tab html mvc

只要满足某些条件,我就会尝试打开一个新标签页。为此,我了解到 target="_blank" 在 html form 中使用,问题是无论是否满足条件,它们都会打开。我怎样才能使它仅在满足条件?

这是我的ActionResult我控制条件:

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);
    }
}

只有这一行 return View("ChAzul", suscriptor); 是新标签页应该打开的时间,我该如何实现?

这是我对它的价值的看法:

    <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 class="alert">
                <span class="closebtn">&times;</span>
                <strong>Providus informa: </strong>
                <p id="textoAlerta">@ViewBag.Alert</p>
            </div>
        }
    </form>

无法从 Controller 内部完成您所要求的内容。

您需要它从您的 View 上执行此操作。为此,您可以在 form 上应用 target 属性,同时发布到 Controller.

现在您已经提到您不希望这种情况发生,而是希望它在您的 Controller:

内部条件下发生
<form id="frmCU" method="post" action="@Url.Action("ChAzul", "Home")" target="_blank">

因此,为了做到这一点,您可以使用 AJAX 从您的 Controller 方法中获取响应,并基于此,您可以重定向到您的 ChAzul 视图一个新标签。用最简单的话来说,你的电话会是这样的:

$.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();
    }
});

而您的 Controller 将如下所示:

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

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