How can I pass HTML code stored in a string variable to a view in MVC 5?

I have an action which is defined like this one:

    public ActionResult TempOutput(string model)
    {
        return View((object)model);
    }

I also have its view, which is defined like this:

@model String

@{
    ViewBag.Title = "TempOutput";
}

<h2>TempOutput</h2>

@Model

And, at one point in my code, I call the action method and the view with this line of code:

return RedirectToAction("TempOutput", "SEO", new { model = tmpOutput });

The point is that, it works all fine when I have a short string, but in my case tmpOutput is a string variable which holds a long HTML code. The thing is that I want to pass that HTML code inside my view, but I don't want it to be displayed as a normal text, but I want it to be parsed as a HTML code and change the view. Currently, when I run the code, I get an error message saying:

The request filtering module is configured to deny a request where the query string is too long.

How can I solve the problem?


The problem in your case is that redirecttoaction makes a new (302) http request and your 'model' which you are sending will be send with querystring and querystring has a limit then instead try using TempData as:

TempData["mydata"] = tmpOutput ;

and then retrieve TempData value in TempOutput action or directly use it in View as shown.

View :

<div>@Html.Raw(TempData["mydata"])</div> 
链接地址: http://www.djcxy.com/p/42116.html

上一篇: 无法处理长的JSON字符串

下一篇: 如何将存储在字符串变量中的HTML代码传递给MVC 5中的视图?