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. 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:
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. 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; }
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.