ASP 核心 MVC:将 Html 字符串从控制器传递到带有变量的视图
ASP Core MVC: Pass Html String from Controller into View with variables
我想将变量传递到 HTML 屏幕并放置在屏幕上。目前使用网络核心。
此问题仅适用于常规 .Net,似乎不起作用。
Pass Html String from Controller to View ASP.Net MVC
测试控制器:
_cardtitle = "Green Tree Title";
_cardtext = "Oak tree with leaves";
_imageurl = "https://images.unsplash.com/reserve/bOvf94dPRxWu0u3QsPjF_tree.jpg?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=500&q=60"
public IActionResult Test(string _cardtitle, string _cardtext)
{
ViewBag.CardTitle = _cardtitle;
ViewBag.CardText = _cardtext;
ViewBag.ImageURL= _imageurl;
ViewBag.Cardstring = @"<div class=""card"" style=""width: 18rem;"" >
<img class=""card -img-top"" src =""https://images.unsplash.com/reserve/bOvf94dPRxWu0u3QsPjF_tree.jpg?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=500&q=60"" alt =""Card image cap"" >
<div class=""card -body"" >
<h5 class=""card -title"" > @ViewBag.CardTitle</h5>
<p class=""card -text"">@ViewBag.CardText</p>
</div>
</div>";
return View();
}
Test.cshtml
@Html.Raw(ViewBag.Cardstring)
现在我只看到@&符号变量,
您显示的 ViewBag 有误。
将Html.Raw(ViewBag.Cardtest)
更改为Html.Raw(ViewBag.Cardstring)
编辑:
根据我的评论,这应该对你有用。您需要将 @ViewBag.CardTitle
和 @ViewBag.CardText
作为属性而不是字符串的一部分传递。
ViewBag.Cardstring = "<div class=\"card\" style=\"width: 180rem;\"><img class=\"card -img-top\" src =\"https://images.unsplash.com/reserve/bOvf94dPRxWu0u3QsPjF_tree.jpg?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=500&q=60\" alt =\"Card image cap\"><div class=\"card -body\" ><h5 class=\"card -title\" >" + @ViewBag.CardTitle +"</h5><p class=\"card -text\">" + @ViewBag.CardText + "</p></div></div>";
我想将变量传递到 HTML 屏幕并放置在屏幕上。目前使用网络核心。
此问题仅适用于常规 .Net,似乎不起作用。 Pass Html String from Controller to View ASP.Net MVC
测试控制器:
_cardtitle = "Green Tree Title";
_cardtext = "Oak tree with leaves";
_imageurl = "https://images.unsplash.com/reserve/bOvf94dPRxWu0u3QsPjF_tree.jpg?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=500&q=60"
public IActionResult Test(string _cardtitle, string _cardtext)
{
ViewBag.CardTitle = _cardtitle;
ViewBag.CardText = _cardtext;
ViewBag.ImageURL= _imageurl;
ViewBag.Cardstring = @"<div class=""card"" style=""width: 18rem;"" >
<img class=""card -img-top"" src =""https://images.unsplash.com/reserve/bOvf94dPRxWu0u3QsPjF_tree.jpg?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=500&q=60"" alt =""Card image cap"" >
<div class=""card -body"" >
<h5 class=""card -title"" > @ViewBag.CardTitle</h5>
<p class=""card -text"">@ViewBag.CardText</p>
</div>
</div>";
return View();
}
Test.cshtml
@Html.Raw(ViewBag.Cardstring)
现在我只看到@&符号变量,
您显示的 ViewBag 有误。
将Html.Raw(ViewBag.Cardtest)
更改为Html.Raw(ViewBag.Cardstring)
编辑:
根据我的评论,这应该对你有用。您需要将 @ViewBag.CardTitle
和 @ViewBag.CardText
作为属性而不是字符串的一部分传递。
ViewBag.Cardstring = "<div class=\"card\" style=\"width: 180rem;\"><img class=\"card -img-top\" src =\"https://images.unsplash.com/reserve/bOvf94dPRxWu0u3QsPjF_tree.jpg?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=500&q=60\" alt =\"Card image cap\"><div class=\"card -body\" ><h5 class=\"card -title\" >" + @ViewBag.CardTitle +"</h5><p class=\"card -text\">" + @ViewBag.CardText + "</p></div></div>";