0

I am sending data through HttpClient (Json) from console app to web app, everything is working well but the only issue Iam facing that data sent from controller to view is not displayed. I debugged the program and i can see that data are returned to the view. Following are the codes:

Constroller:

using ( var db = new MessageDBContext())
{
    try
    {
        db.Messages.Add(model);
        db.SaveChanges();
    }
    catch (DbEntityValidationException e)
    {
        Debug.WriteLine("Saving Messages into db Error 1: " + e.EntityValidationErrors);
    }
}
return View(model);

Controller Debugg:

Debugg controller

and here is the view code:

<table class="table table-striped">
    <thead>
        <tr>
            <th>Message</th>
            <th>Time</th>
        </tr>
    </thead>
    <tbody>
        @if (Model != null)
        {
            <tr>
                <td>
                    @Html.Display(@Model.message)
                </td>
                <td>
                    @Html.Display(@Model.time)
                </td>
            </tr>
        }
    </tbody>
</table>

View Debugg:

View Debugg

So why the data are not displayed to the html view!?

23
  • Why do you need to use the Html.Display helper, why not simply write out the result <td>@Model.message</td> Commented Dec 2, 2015 at 9:43
  • @Kane its not working i tried it :( Commented Dec 2, 2015 at 9:44
  • 2
    Have you tried DisplayFor? Commented Dec 2, 2015 at 9:44
  • @SergiiZhevzhyk it wont work, i did Commented Dec 2, 2015 at 9:45
  • 3
    Are you sure you are passing the model to the correct view? Your debugger shows the table has id="treatmentTable" but your view code doesn't. Commented Dec 2, 2015 at 10:33

2 Answers 2

1

You have to use one of this case:

 @if (Model != null)
    {
        <tr>
            <td>
                @Html.DisplayFor(model => model.message)
            </td>
            <td>
                @* Or *@
                @Model.time
            </td>
        </tr>
    }
Sign up to request clarification or add additional context in comments.

Comments

1

I'll add some information to this question. The controller method which (shown in the question) is marked with HttpPost attribute and is called from the console application. Of course, the result is returned to the console and NOT to the browser. In the browser the controller returns empty model return View(); Otherwise, the DisplayFor method would work perfectly. If you want to update the display in your browser you need to write more code to establish server->client communication.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.