简体   繁体   English

实体框架对象具有datetime字段,我希望在传递给视图之前将其转换为短日期字符串(ASP.NET MVC4)

[英]Entity Framework object has datetime field that I want to convert to short date string before I pass to view (ASP.NET MVC4)

I thought it was a pretty simple issue, and I might be overthinking it, but I can't figure out how to pass just a date string to my view. 我认为这是一个非常简单的问题,可能会考虑得太多,但是我无法弄清楚如何仅将日期字符串传递给视图。 Here is the entity framework code first class: 这是实体框架代码的第一类:

public class Onus
{
public virtual int Id { get; set; }
public virtual string Title { get; set; }
[MaxLength(140)]
public virtual string Description { get; set; }
public virtual string Details { get; set; }
public virtual DateTime? DueDate { get; set; }
public virtual string Status { get; set; }
}

And here is the part of the controller where I want to change it: 这是控制器中我要更改的部分:

public Onus GetOnus(int id)
{
var onus = db.Onuses.Find(id);

if (onus == null)
{
    throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));
}
return onus;
}

In the view I am using some ajax and javscript templating to show the data. 在视图中,我使用一些ajax和javscript模板来显示数据。 If you need that code to, I will add it. 如果您需要该代码,我将添加它。

***EDIT ***编辑

Sorry for not being more clear, I don't really have a controller manipulating this, it is all done with ajax: 抱歉,我不清楚,我实际上没有控制器来操纵它,这一切都是用ajax完成的:

var getOnus = function (id) {
            return $.ajax(onusApiUrl + "/" + id)
        };

var viewOnus = function () {
            var id = getId(this);
            onusServer.getOnus(id).done(viewOnusDetails);
        };

var viewOnusDetails = function (onus) {
            var output = templates.onusDetails(onus);
            $("#onusDetailsOutput").html(output);
        };

THere is a lot more javascript than that, but I believe that is all that you should need to see. 这比JavaScript多得多,但我相信这就是您应该看到的全部内容。 Oh and the markup: 哦,还有标记:

<div id="Details">
<div id="detailsLeft">
    <input type="hidden" value="{{Id}}" id="detailsId" />
    <header>
        <p><span class="onusTitleDetails">{{Title}}</span><span id="close">X</span></p>
    </header>
    {{#if DueDate}}
    <p class="detailsLabel">Due Date</p>
    <p class="onusDueDate">{{DueDate}}</p>
    {{/if}}
    <p class="detailsLabel">Description</p>
    <p class="onusDescriptionDetails">{{Description}}</p>
    <p class="detailsLabel">Details</p>
    <p class="onusDetails">{{Details}}</p>
</div>
<div id="detailAction">
    <div class="editOnus hover">Edit</div>
    <div class="deleteOnus hover">Delete</div>
</div>
</div>
<div class="onusStatusDetails">{{Status}}</div>

It would be helpful to see the controller that is invoking the view, but I'll assume it's something like this: 看到正在调用视图的控制器会很有帮助,但是我假设它是这样的:

public ActionResult MyController(int id)
{
    Onus onus = GetOnus(int id);
    return View(onus.DueDate.HasValue ? 
                onus.DueDate.Value.ToShortDateString() : 
                "(null)");
}

That will pass just the short date string to your model, which in turn should declare 这将仅将短日期字符串传递给您的模型,然后该模型应声明

@model string

What about adding another property to your entity, and then binding to that? 将另一个属性添加到您的实体,然后绑定到该实体,该怎么办?

 public virtual DateTime? DueDate { get; set; }

 public string DueDateString
        {
            get { return DueDate != null ? DueDate.ToString() : string.Empty; }
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将输入/文本框字段传递给控制器​​? ASP.NET MVC和实体框架 - How can I pass an input/Textbox Field to Controller? ASP.NET MVC and Entity Framework ASP.Net Framework MVC Entity 6 中的日期时间字段 - DateTime Field in ASP.Net Framework MVC Entity 6 如何将短日期字符串转换回DateTime对象? - How do I convert a short date string back to a DateTime object? ASP.NET MVC4 和实体框架上的超时问题 - Timeout issue on ASP.NET MVC4 and Entity Framework 使用实体框架对ASP.NET MVC4进行单元测试 - Unit testing asp.net mvc4 with Entity Framework 想要将多个模型传递给视图ASP .NET MVC4 - Want to pass multiple models to a view ASP .NET MVC4 如何将DateTime参数从View传递到ASP.net MVC5中的Controller? - How do I pass DateTime parameters from View to Controller in ASP.net MVC5? 如何在ASP.Net MVC实体框架的帐户控制器中传递来自登录方法的ID? - How can I pass the Id from Login method in Account Controller in ASP.Net MVC Entity Framework? 从apicontroller传递值到asp.net mvc4中查看 - pass value from apicontroller to view in asp.net mvc4 asp.net mvc4 razor将视图模型传递给帮助器 - asp.net mvc4 razor pass view model to helper
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM