Kerem Zaman
Kerem Zaman

Reputation: 539

Strange Behaviour of Response.Write()

I am working on an ASP.NET MVC Project. In my controller I check if ModelState is valid and according to the result I assign a value to IsSucceed:

if (ModelState.IsValid)
{
    ModelTutucu.modelim.IsSucceed = true; 
}

return View("YaziEkle",ModelTutucu.modelim);

In my view I have a div with a "success" class. I have some text written by Response.Write() to the value of IsSucceed:

<div class="success">
    @if(Model.IsSucceed) {
        string success = "It is successfully saved.";
        Response.Write(success);
    }
</div>

The problem is that success doesn't appear in the the div. It appears at top of the page. When I do something like this:

<div class="success">Foo</div>

"Foo" appears in the div, but I cant get the same result with Response.Write, it shows strange behaviour. What is the reason of this problem? How can I solve it? Thanks in advance.

Upvotes: 1

Views: 50

Answers (1)

schei1
schei1

Reputation: 2487

You should use the @ instead of Response.Write(). Response.Write() is writing before the razor is rendered and written, so that's why it comes out on top, rather than in the middle or where it should be. Using @ makes sure that the string is rendered where it should be according to the html markup.

Like:

@if (Model.IsSucceed)
{
    string success = "It is successfully saved.";
    @success
}

Upvotes: 2

Related Questions