How To Save Value With Format Currency Textbox ASP.NET MVC3 - asp.net-mvc-3

I use this code for display currency format in the Edit View. (The salary datatype is decimal)
#Html.TextBoxFor(m => m.Salary, new { dir = "rtl", #Value = string.Format("{0:C}", Model.Salary) })
But when i trying to save my changes, why the salary property always contains null? How to save the value with format currency in a textbox?

Related

Filter Kendo Grid column with Date picker

I have a Kendo MVC Grid that has a column with a DateTime column. Everything looks good and formats correctly. When i filter, it gives me a date picker and a time picker. When i remove the DateTimeFilter Template below and use template contains, it will give me a Date picker (which i want), but still wants to filter by the date and time.. Is there a way i can have the date and time all as the same field, but only filter with a Date picker.? Example: i use the Date picker to pick 07/24/2017 and it filter everything on that date regardless of time.. Or do they need to be completely different fields, or even concatenated fields in the same column.?
the Column data looks as such: 07/24/2017 18:12:00
columns.Bound(c => c.CreatedDate).Title("Submitted On")
.ClientTemplate("#= kendo.toString(kendo.parseDate(CreatedDate), 'MM/dd/yyyy HH:mm:ss') #")
.Filterable(ftb => ftb.Cell(cell => cell.Template("DateTimeFilter")));
If you're using MVC, in your model, you add DataType.Date above your DatePicker property as below:
[DataType(DataType.Date)]
public DateTime SubmittedOn{ get; set; }
Note: add reference to System.ComponentModel.DataAnnotations if its not included in your header.

how i can get current date without time on MVC view

Curently i am using this code then i submit my data to controller and through controller i add to my database
#Html.HiddenFor(model => model.date, new { #Value=DateTime.Now.ToShortDateString() })
If you do not want to show your time value, then you could do that by specifying the string format you want your DateTime to show in your ToString():
#Html.HiddenFor(model => model.date, new { #Value=DateTime.Now.ToString("dd-MM-yyyy") })
dd-MM-yyyy format will only show day-Month-year elements of the DateTime. To show hour-minutes-seconds, use HH-mm-ss or hh-mm-ss. For more info, check MSDN article on standard and custom date time format.

Default Empty value in Kendo DatePicker

I need the default value of datetime picker to be empty.
#(Html.Kendo().DatePickerFor(model => model.RevisionDate)
.Name("RevisionDate")
)
"RevisionDate" is a datetime property in a asp.net mvc model.
how it's currently being displayed
and i need to display it as below
Your RevisionDate property is by default set to DateTime.MinValue which is 01/01/0001 ( all DateTime properties behave like that). This is why the Kendo UI DatePicker shows it like that. The solution is to make the property a nullable DateTime (DateTime?) whose default value is null.
If you don't want to make the property nullable, you can also do it like this:
#(Html.Kendo().DatePickerFor(m => m.Property).HtmlAttributes(new { value = "" }))
I had the same problem with a ComboBoxFor ComboBoxFor(m => m.LocationId).Value(Model.LocationId == 0 ? string.Empty : Model.LocationId.ToString()).
But I see that this approach doesn't work on the DatePickerFor(It could be a bug).
A workaround could be made in JS if($('#RevisionDate ').val() == "1/1/0001") { $('#RevisionDate ').val("") } but I would recommand Atanas's approach with a nullable field.
Blank out the field from the client side using something like this in your markup/cshtml file :
<script>
// Blank out the 1/1/0001 12:00 AM from the date field
$('#RevisionDate').val("");
</script>

Issue To fetch date format At Edit() In ASP.NET MVC 3

When I edit my one of the form sin ASP.NET MVC 3 in this when I edit a user registration form then I got date in startdate is 21-Mar-12 12:00:00 AM in text box but I need 21-Mar-12.
So how can I format textbox date like that?
You could decorate the DateTime property on your view model with the [DisplayFormat] attribute which allows you to specify a given format:
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd-MMM-yy}")]
public DateTime SomeDate { get; set; }
and in your strongly typed view use the EditorFor helper to render the corresponding input field:
#Html.EditorFor(x => x.SomeDate)
As far as binding the value back to a DateTime field when posting back is concerned you could write a custom model binder that will use this format. I have shown an example here.

Getting selected item's Value and Text from bounded DropDownList

My DropDownList is bound with public List<SelectListItem> CountryList { get; set; } Model's property as #Html.DropDownListFor(m => m.CountryCode, Model.CountryList, "-- Select --"). CountryCode is a short property to hold the code of the Country. How can i achieve
1- Show both Code and Name of the country in the DropDownList as "CountryCode - CountryName"
2- Receive both Code and the Name of selected Country as bounded to the Model passed to the Action. In other words, the type of the CountryCode is SelectListItem rather than short
PS. No scripting
1- Show both Code and Name of the country in the DropDownList as
"CountryCode - CountryName"
Got help from Creating a DropDownList helper for enums. Now the DropDownList is showing items as 0001 - Country Name but it is not keyboard friendly because end user should type 0001 to select Country Name rather than typing cou.... And selected Text has to be parsed to separate Value and Text, though not end user's headache but not ideal.
Working and waiting for the answer of second question...

Resources