Display a string in place of a NULL date - asp.net-mvc

when the date property is set in the model, this is executed:
if (!(rdr["JobEnded"] is DBNull)) { job.JobEnded = (DateTime)rdr["JobEnded"]; }
which results in job.JobEnded being 1/1/0001 12:00:00 AM.
What can I use in place of
#Html.DisplayFor(modelItem => item.JobEnded)
to show "In Progress" instead of "1/1/0001 12:00:00 AM".
I've tried putting an if/else in the view, but was only able to display a valid date or nothing. I'm sure I'm missing something really basic here as this is my first attempt at an APS.NET MVC view.

You're dealing with default dates here, so to test for it, you need to use:
#if (item.JobEnded == default(DateTime))
{
#:In Progress
}
else
{
#Html.DisplayFor(m => item.JobEnded)
}
Or in one-liner form:
#((item.JobEnded == default(DateTime)) ? "In Progress" : Html.DisplayFor(m => item.JobEnded).ToString())

Related

How to convert decimal field value to currency in mvc

I have an few editorfor in my view like following
#Html.EditorFor(Model => Model.CashBalance)
Now when i enter any value in to that editorfor,the value should change to currency value in textbox change event
For ex:
123 should display as 123.00
14.35 should display as 14.35
I want to do this in generic way so that I don't need to change it every where as my project has many editorfor which takes inputs from user.
As I am using an EditorTemplate for all these textboxes,i want to handle here itself.
My EditorTemplate for this is decimal.cshtml and it looks like the foll
#model decimal?
#{
string value = (Model.HasValue == false || Model.Value == 0) ? "" : string.Format("{0:0.00}", Model.Value);
}
#Html.TextBox(
"",
value,
new { #class="amountRightAlign"}
)
Will there be any textchange event i can write here so that it affects where ever there is decimal datatype?
Thanks in advance?
Html helpers are server side code used to generate the html which is sent to the client. In order to interact with user changes in the browser, you need to use javascript to handle events.
In your case you don't need an EditorTemplate. Instead, just the overload of TextBoxFor() that accepts a format string
#Html.TextBoxFor(m => m.CashBalance, "{0:0.00}", new { #class="decimalnumber" })
Then in the view, or in a separate script file
$('.decimalnumber').change(function () {
var num = new Number($(this).val());
if (isNaN(num)) {
// Its not a valid number
return;
}
$(this).val(num.toFixed(2));
})

If condition in #Html.DisplayFor

I am trying to display different messages based on data from statusid table. Let say for example if my statusid is 1120 then i would like to display "PASS" but i am struggling with the correct syntax.
If statusid=1120 then "PASS".
Here is my code for displaying statusid:
#Html.DisplayFor(modelItem => item.statusid)
Any idea?
Make an object with all the properties you need
SomeObject.statusId
SomeObject.message
#Html.DisplayFor(modelItem => item.SomeObjectInstance)
Doing it this way let's you add the code and logic outside the view, which is generally recommended.
If you only have a few values from statusid then you can do this.
#if (#Model.statusid == 1120)
{
<span>PASS</span>
}
else
{
<span>FAIL</span>
}

Kendo Timepickerfor formatting not working

I have an MVC Kendo Timepicker for that I am using. It works fine except that I can't format the time to Military time. After I add the formatting for Military time, once I select the time the validation doesn't pass and it tells me it must be a date. Is there a way to format the TimePickerFor to allow military time?
#using Kendo.Mvc.UI
#model DateTime?
#(Html.Kendo().TimePickerFor(m=>m)
.Value(#Model)
.Min("05:00")
.Max("00:00")
.Format("{0:HHmm}")
)
Update: This doesn't work with format being changed to .Format("HHmm")
Ok, so thanks to the Kendo people, I found the answer. The script may need some work depending on the situation. My TimePickerFor is in an Editor Template which sits in a grid with other timepickers and numeric text boxes. Only thing with this way of working is that once the script is fired, the numeric boxes used this script also to validate (hence the return $.isNumeric(input.val()) line. Hope this helps someone else out.
TimePickerFor Control:
#using Kendo.Mvc.UI
#model DateTime?
#(Html.Kendo().TimePickerFor(m=>m)
.Value(#Model)
.Format("HHmm")
.HtmlAttributes(new{data_format="HHmm"})
.ParseFormats(new[]{"HHmm"})
)
<script>
var originDate = kendo.ui.validator.rules.mvcdate;
kendo.ui.validator.rules.mvcdate = function(input) {
var format = input.attr("data-format");
if (input.val() == "") {
return kendo.parseDate("0000", format);
}
if (format) {
return kendo.parseDate(input.val(), format);
} else {
return $.isNumeric(input.val());
}
};
</script>
I think you have to remove the curly braces and make sure that is a valid format type. I also don't think the 0 is necessary.
Here's some formating documentation
http://docs.telerik.com/kendo-ui/getting-started/framework/globalization/dateformatting
#(Html.Kendo().TimePickerFor(m=>m)
.Value(#Model)
.Min("05:00")
.Max("00:00")
.Format("yyyy/MM/dd hh:mm tt")
)
Edit:
Is your max and min values correct? I don't see how that is logically correct.

List<>.AddRange - where clause

Hi I am working on application to get some details from Sharepoint lists.
I am able to get the results as shown below:
sample.AddRange(queryResults.GetItemRows().Select(listItemRow => new CalendarEvents
{
ItemId = listItemRow.ItemId,
Title = listItemRow.AttributeValueOrDefault("ows_Title", ""),
StartDate = listItemRow.AttributeValueOrDefault("ows_StartDate", ""),
EndDate = listItemRow.AttributeValueOrDefault("ows_EndDate",""),
Link = listItemRow.StrippedAttributeValueOrDefault("ows_Link", "")
}).OrderBy(listItemRow => listItemRow.StartDate));
Where as I am struck at one point:
- I want to extract the list rows with future events. I mean StartDate should be >= present date. I tried some options but no success. I know I need to use .where at the end of AddRange, but how can I check the condition for the date.
I tried as below but didn't work
}).where(listItemRow => listItemRow.StartDate >= dateTime.now)
Any help would be great...
Thankx
Have you make sure you had your data? Is there any error?
If not, try to use:
}).Where(listItemRow => listItemRow.StartDate.After(DateTime.Now));

hesitate to create a condition in the radiobutton mvc razor

Good morning.
I have the following conditions in radiobutton
<td>
#Html.RadioButtonFor(model => model.ImagensSelecionadas, new { Checked = (Model.ImagemCapaSelecionada == item.Value ? "Checked" : "") })
</td>
idea is as follows.
I have an image gallery, Pardo'm doing editing where I can change the cover gallery
and am using the radio button to mark the cover and tried to make the following condition, that I posted up there
that has to compare the value of the Value of ImagemCapaSelecionada item.Value and when the values ​​are equal
must to get the radio button. this condition is within a foreach loop like this:
# foreach (var item in Model.ImagensDaGaleria)
and do not use jQuery, I have to do without Jquery ..
how do I put this condition in the html help radio button??
or what is the best way to do this?
thank you
in this case I am not getting true or false values ​​in the loop if I'm getting this imagemCapaSelecionada receives ai id ImagemCapaSelecionada querai compare the values ​​of Item.value
example
imagemcapaSelecionada | value | status
1 5 not checked
1 1 checked
I think the following should work for you...
#Html.RadioButtonFor(model => model.ImagensSelecionadas, Model.ImagemCapaSelecionada == item.Value)
notice that the second parameter is the object value (i.e. a true or false value)
in the foreach loop...
#foreach(var item in Model.ImagensDaGaleria)
{
#Html.RadioButtonFor(model => model.ImagensSelecionadas, Model.ImagemCapaSelecionada == item.Value)
}

Resources