我在视图中有一个iframe,我需要从我的控制器填充src这是必须从中调用它的控制器的一部分。
else
{
//TODO: add after mobile detection is added back in
//if (Request.Browser.IsMobileDevice)
// return RedirectToAction("Index", "Mobile", new { Area = "" });
//else
return RedirectToAction("Index", "Home", new { Area = "" });
}
我知道如果它不在MVC中你会怎么做,所以我怎么能从控制器调用我的iframe?
ifr1.Attributes["src"] = "http://localhost:8000/index.php/login/?u=" + Session["username_s"] + "&p=" + Session["password_s"];
enter code here
答案 0 :(得分:2)
您可以将ViewData
或ViewBag
用于此目的。
在您的控制器中,您应该具备以下条件:
else
{
//TODO: add after mobile detection is added back in
//if (Request.Browser.IsMobileDevice)
// return RedirectToAction("Index", "Mobile", new { Area = "" });
//else
ViewBag.IframeUrl = "http://localhost:8000/index.php/login/?u=" + Session["username_s"] + "&p=" + Session["password_s"];
return RedirectToAction("Index", "Home", new { Area = "" });
}
在您查看中,您可以使用以下内容:
<iframe src="@(ViewBag.IframeUrl)"></iframe>
您也可以将ViewData
替换为ViewBag.IframeUrl
ViewData["IframeUrl"]
答案 1 :(得分:1)
您应该在目标操作中使用ViewBag
,并传递URL,例如:
1)您的目标重定向操作应如下所示:
public ActionResult Index()
{
//Other code from the action here
ViewBag.IFrameSrc = "http://localhost:8000/index.php/login/?u=" + Session["username_s"] + "&p=" + Session["password_s"];
return View();
}
2)Index
视图应如下所示:
<iframe src="@ViewBag.IFrameSrc">
</iframe>