0

MVC/ASP.NET/C#/html/javascript newbie question:

I'm trying to move some legacy software into an MVC solution. I have an MVC controller ViewResult method that makes an API call to the legacy system and returns a string which is a fully formed HTML page (including the HTML start and end tags). Some time in the future, I'll rewrite the logic as an MVC view, but for right now I need to just display that page (preferably in a new tab).

I've tried this in the controller:

return View((object)calendar);  

(where "calendar" is the string containing the HTML document)

In my view I have

  @model string  
  @{ Layout = null; }  
  @Model  

But that didn't work.
Any ideas?

GusV
  • 15
  • 5

3 Answers3

1

Model binding is binding the object of your model class.

For example, ([Solution].[Models].[Model class]),

@model PassDatainMVC.Models.Record

To pass the data from controller to view,

Approach 1: ViewBag

Controller:

string data = "testing";
ViewBag.see = data;
return View();

View:

@using PassDatainMVC.Models
@ViewBag.see

Or:

Approach 2: Model binding

Controller (Class):

public string recordProperty;

View:

@model PassDatainMVC.Models.Record
@Model.recordProperty

While you have to set the property under the model class in the data field for the second approach.

Ref. https://www.c-sharpcorner.com/article/asp-net-mvc-passing-data-from-controller-to-view/

Wing Kui Tsoi
  • 474
  • 1
  • 6
  • 16
  • Thank you Wing, but both you and Reha are missing the point. I'm not a _total_ newbie to MVC. I have successfully built a view views and used both ViewBag and models to pass data into the .cshtml file. It all works fine when the data is just a simple string. But when, as in my case, the data is a complete HTML page specification (ie, the string starts with an html open tag and ends with an html close tag, with an entire html specification (over 5000 characters) inbetween) then everything I try just ends up displaying the html **code** instead of the html page itself. – GusV Nov 23 '18 at 16:56
  • This may be helpful: https://stackoverflow.com/questions/22781548/pass-html-string-from-controller-to-view-asp-net-mvc – Wing Kui Tsoi Nov 23 '18 at 17:11
  • Thank you Wing. Your reference to https://stackoverflow.com/questions/22781548/pass-html-string-from-controller-to-view-asp-net-mvc solved the problem. I now use Html.Raw in my view. The entire view now consists of just two lines of code: `@{Layout = null;}` `@Html.Raw(ViewBag.calendar ) ;` – GusV Nov 23 '18 at 21:47
0

If you want to just one data you can use a ViewBag. This is simple.

Also you want to send with model. You should use this code.

Class

 public class Calendar
    {
        public string CalendarName { get; set; }
    }

Controller

Calendar newModel = new Calendar();
newModel.CalendarName = "test name...";
return View(newModel);  

View

 @model ModelNamespace.Calendar

<h1> @Model.CalendarName </h1>
0

Thanks Reha! But unfortunately neither of your suggestions did the trick.
For your first suggestion I used ViewBag. In the controller I replaced

return View((object)calendar);

to

ViewBag.calendar = calendar;
return View();

And replaced the view with just

@{ Layout = null; }
@ViewBag.calendar

The result was that the user is left looking at the actual HTML code instead of what the HTML code is supposed to render.

For your 2nd suggestion, I did exactly as you suggested (except I changed Model.CalendarName = "test name..."; to Model.CalendarName = calendar; The result is the same, the user is left looking at the HTML code.

GusV
  • 15
  • 5