Bojangles
Bojangles

Reputation: 467

Display the Name and Value associated with my model in my view

In my Patrol model I have

public string Visibility { get; set; }

And in my view if can display the value of Visibility:

@Model.Visibility

How can I display the word "Visibility" so I could display something like

<Visibility>Clear</Visibility>

Upvotes: 0

Views: 55

Answers (1)

ramiramilu
ramiramilu

Reputation: 17182

In your razor view, you should have something like this to get the format your wanted -

@String.Format("<{0}>{1}</{2}>", Html.DisplayNameFor(m => m.Visibility), @Html.DisplayFor(m => m.Visibility), @Html.DisplayNameFor(m => m.Visibility));

will print -

<Visibility>Clear</Visibility>

This way you can output the data in any formatted string.

UPDATE: As per @CodeCastor point, if you want to output XML to the browser, you can use following code in the controller action -

        MyModel model= new MyModel();
        model = new MyModel() { Visibility = "Clear" };

        XmlSerializer xsSubmit = new XmlSerializer(typeof(MyModel));
        StringWriter sw = new StringWriter();
        XmlWriter xw = XmlWriter.Create(sw);
        xsSubmit.Serialize(xw, model);
        var xml = sw.ToString(); 
        return Content(xml, "application/xml");

Upvotes: 1

Related Questions