1

I have a webpage that displays a a table of data pulled back from a sql server view via entity framework and WCF Services.

On my sql server view one of the columns is a Date Field. I am having some difficulty getting it to display without the time part on my page (the time part if not there on the DB but from reading it seems as it is a DateTime property when I generate my entity view the time part will be added.

So basically on my model I have the following (obv the names are just mocked here):

Model

public List<MySqlView> MySqlView { get; set; }

Controller

model.MySqlView = MyWCFService.GetMeAlltheData(
                      //pass in some values start date, end date
                  );

So on this I can see everything I want returning fine and then I pass the model to my view.

To build up my table then in the View I am doing the following:

cshtml

<tbody>
            @foreach (var item in Model.MySqlView)
            {
                <tr>
                    <td class="table-data">
                        @Html.DisplayFor(modelItem => item.Date)
                    </td>
                    //More stuff in table close loop

I have tried item.Date.ToShortDateString() but that throws exception

Templates can be used only with field access, property access, single-dimension array index, or single-parameter custom indexer expressions.

4

2 回答 2

0

Having a ViewModel class separate the model from the view solves virtually all interface issues. The more I work with MVC, the more I realize a better approach is MVVmC. This is a good example of why. Instead of strongly typing to the model itself, create a ViewModel that provides to the view a string property with the correctly formatted date value using MyDateTimeItem.ToString("MMMM dd, yyyy").

于 2013-02-14T05:03:13.733 回答
0

您可以在 Views/Shared/DisplayTemplates 中使用以下内容创建 DateTime.cshtml

@model DateTime

@Html.Label("", Model.ToShortDateString())
于 2013-02-14T05:07:39.927 回答