How to truncate the decimal part in Asp.Net MVC3 - asp.net-mvc-3

My application is in asp.Net MVC3 coded in C#.Net.
I want to truncate the decimal part of my values when they are displayed in the TextBoxes.
The DataType for these values are set to Decimal in the database so even if i save 150 it saves as 150.0000.And when it displays a particular record it comes back as 150.0000 but i dont want it that way,i only want 150.
I have tried
#Html.TextBoxFor(string.Format("Decimal Truncation Format", model => model.myValue))
But not working..The values are travelling from many views.
So can i have such a truncation in the model itself so that it applied to that value all the time.
If not possible that way then how can i achieve it.?

Why don't you modify the property in your ViewModel to Int32? That should solve your problem in an instant!
EDIT:
This assumes that you have a ViewModel. You should never bind views directly to your db, but to a view-specific model (the ViewModel). Then you can modify that ViewModel as much as you want, so that it serves the needs of the View rather than those of the DB.
Check out the following links:
http://stephenwalther.com/blog/archive/2009/04/13/asp.net-mvc-tip-50-ndash-create-view-models.aspx
http://rachelappel.com/use-viewmodels-to-manage-data-amp-organize-code-in-asp.net-mvc-applications
http://geekswithblogs.net/michelotti/archive/2009/10/25/asp.net-mvc-view-model-patterns.aspx
http://weblogs.asp.net/shijuvarghese/archive/2010/02/01/view-model-pattern-and-automapper-in-asp-net-mvc-applications.aspx
Good luck!

Another solution: use TextBox instead of TextBoxFor, which let you initialize the field with any value.
#Html.TextBox("myValue",String.Format("{0}", Model.myValue)

Related

Why would I use a Kendo model instead of only using a transport/datasource

I am currently looking at Kendo UI and was wandering what is the "advantage" of defining a model in my schema. It seems to work quite well without. Is this for binding reasons (i.e. column discovery for Grid control for example) ? Does it allow particular validations ?
Thanks
Pat
You can use the schema model to specify your data model. On a grid, you can for example set:
Which data type the field has (if set to number, the column filter offers e.g. 'greater than, 'less than' instead of 'contains' etc. for string values).
If a grid field is editable or not (when grid is set to editable)
Several validation properties
...
I usually receive the grid data from a web service in JSON, so everything is a string initially. By using the model I influence how these values are displayed and how they behave.
See also
http://docs.telerik.com/kendo-ui/api/javascript/data/datasource#configuration-schema.model
and http://docs.telerik.com/kendo-ui/api/javascript/data/model
for a full overview of possibilities.

Store translated versions in database for Joomla component

I'm currently developing my first MVC component for Joomla 3.x. All in all I'm bit struggling with language/translation issues in database.
My problem is that I need to store translated content of user generated content which comes from the backend. For example someone enters a new item in German (stored in database) and needs a translation in another language. How to accomplish that in Joomla? I don't like to generate a new item for every different language when the rest is all the same.
I thought about a table "item" and a table "item_language" with that structure (strongly simplified for viewing purposes):
item
id PRIMARY INT
price DOUBLE(4,2)
item_language
itemid PRIMARY INT
language PRIMARY CHAR(5)
name VARCHAR(50)
In item_language I would like to store the different translated versions. In the language field there would be the region code (eg. de-DE) to identify the language.
My problems:
How to display the different (translated) versions in backend?
Is this the right database model?
Any help is appreciated!
You have really found yourself a nice task for a first component in Joomla!
A rather generalist answer:
The database model seems right. Alternatively you could encode in JSON the language data, but this could make later query operations potentially difficult. This way you will only have one table to work with.
As far as I know (if you are using JModel / JTable to manipulate the data) can't do this directly, as JTable is really only designed to manipulate single tables.
What you can do:
For editing: figure a way to represent this graphically ( for your users to see and edit this one to many relationship) and to post this data (language texts as an array) to JModel. In the model you can maintain the desired relationships and save the data using JTable.
Viewing (without editing) shouldn't be an issue, it would be a simple JOIN.
If you are willing to create a basic component on github, I might even give you a hand with JModel / JTable.
I found a way to deal with the things I needed.
Thanks Valentin Despa for guiding me in the right direction :-).
Here the whole procedure (simplified - validations and exact steps omitted):
Define the form fields in the models/forms/site.xml as normal.
In views/site/tmpl/edit.php add self coded Javascript (based on jQuery) to deal with the fields which have content in multiple languages stored as JSON in database.
Clone the original form element and modify the needed attributes (id, name, ...) to display a special version just for the defined languages. As content - extract the JSON for the needed language from original field content and display.
Hide the original field with Javascript and append the customized versions to DOM.
Afterwards in tables/site.php I read the dynamically generated content withJInput and build together the original field by generating JSON and saving to database.
It's working like expected.

MCV3: View to edit entity has to hold every column?

I got a silly general question...
If I generate a strongly typed view of an entity and chose "edit" as scaffolding, then the view does contain every column for that table. Changing and saving the values via setting it modifierd and call db.SaveChanges() does work in the controller. So far, so good.
But if I remove just one of that columns inside the view, then saving doesn't work anymore.
Is there a rule describing this? Is it only possible to make view with every column when wanting to save the model later on? I don't want to make 90 of 100 columns "hidden"...
PS: When editing a value in another table which is connected via Foreign Key (like customer.address.STREET) saving also does not work. Does everything of the entity ADDRESS has to be inside the view? I really don't get that.
Besides that: If I create my own ViewModel containing two entities: Do they also have to hold every column of both entities? This would be a whole bunch of traffic...
Answer is: You should not use the .Modified state. Instead using the UpdateModel method works fine without every field.

Creating "lookup" values in Core Data

I have a Core Data entity which needs a gender property. I just need one of two, the standard male or female values. What's the best practice for this in Core Data?
In the .NET world with databases I would've created a Gender table, with foreign key in the child table. I'm still getting my head around Core Data right now - any suggestions would be greatly appreciated.
Cheers,
Dany.
UPDATE
Based on the comments here I have added an extra NSString property in my Core Data entity called gender. Its getter and setter manipulate the isMale property value. The UI is bound to gender - works a treat so far! Thanks for the help everyone.
I would not do a look up as Core Data is not a database but rather an object hierarchy that happens to persist to a database.
Instead I would have a boolean called male with a getter accessor -isMale since there is no risk of there ever being a third.
update
Despite the commentary, how you interact on the UI is completely separate from how you store the data. How you display is up to your UI design. The code in your controller will handle the translation between the boolean state and the UI display.
You can have a checkbox, radio buttons, drop down list, et. al.; doesn't matter. Just translate what the user interacts with in your controller.

Model Data Type versus View Control

I have having a little trouble wrapping my head around the design pattern for MVC when the data type of the model property is very different than what I wish to display in a form. I am unsure of where the logic should go.
Realizing that I am not really sure how to ask the question I think I will explain it as a concrete example.
I have a table of Invoices with a second table containing the InvoiceDetails. Each of the InvoiceDetail items has an owner who is responsible for approving the charge. A given invoice has one or more people that will eventually sign off on all the detail rows so the invoice can be approved. The website is being built to provide the approval functionality.
In the database I am storing the employee id of the person who approved the line item. This schema provides me a model with a String property for the Approved column.
However, on the website I wish to provide a CheckBox for the employee to click to indicate they approve the line item.
I guess my question is this -- how do I handle this? The Model being passed to the View has a String property but the form value being passed back to the Controller will be of the CheckBox type. I see two possible ways...
1) Create a new Model object to represent the form fields...say something like FormInvoiceDetails...and have the business logic query the database and then convert the results to the other type. Then after being submitted, the form values need to be converted back so the original Model objects can be updated.
2) Pass the original InvoiceDetails collection to the View and have code there create render the CheckBox based on the value of the String property. I am still not sure how to handle the submission since I still need to map back the form values to the underlying database object.
Maybe there is a third way if not one of these two approaches?
To make the situation a bit more complicated (or maybe it doesn't), I am rendering the form to allow for the editing of multiple rows (i.e. collection).
Thanks for any insight anybody can provide.
You need a ViewModel, like #Justn Niessner suggests.
Your controller loads the complete model from the database, copies just the fields it needs into a ViewModel, and then hands the ViewModel off to the view for rendering.
I'd use Automapper to do the conversion from Model to ViewModel. It automates all the tedious thingA.x = thingY.x; code.
Here is an additional blog post going over in detail the use of ViewModels in the Nerd Dinner sample.
I believe what you are looking for is the ViewModel.
In cases where you are using a ViewModel, you design the ViewModel to match the exact data you need to show on your page.
You then use your Controller to populate and map your data from your Model in to your ViewModel and back again.
The Nerd Dinner ASP.NET MVC Example has some very good examples of using ViewModels.

Resources