[英]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.