March 09 2014 display MSSQL DateTime on razor.

Tagged Under : ,
MSSQL store DateTime format is standard. But sometimes we need change the format to display it to user, let them more easy read and understand which Datetime format was set in our Application.

If use retrieve the MSSQL DateTime Data to you application, it will display like below format.

But, when validate the DateTime with it will return an error.

Because the DateTime format set in was “06-03-2014 12:00:00 AM”. But now we just need to display Date only not need time.

March 04 2014 summary of return Action Result type

Tagged Under :
There are different type of results that are available in MVC. When create new controller, they will come with actions by default. The Empty controller includes an Index action with a return value of type ActionResult. ActionResul class is the base class for all action results.

There are different ActionResult types explained below.

February 27 2014 how to set model default value

Tagged Under : ,
Because I want to show default value in my create page. and in the create page code was used Html.TextBox and model to generate the input box.

Since it was used model to generate the code and we also can use model to set the default value as well.

Below example code are showing how set the default value in model:
public class Category 
  public int ID { get; set; }
  public string Name { get; set; }
  public DateTime CreateDate { get; set; }

February 24 2014 MSSQL connection test

Tagged Under : ,
I am making a little code to test the functionality server with MSSQL Database connection. The test code was develop with C#.

If the script successful connect with MSSQL Database it will display message like below:
Else, it will display

Server script:

February 21 2014

How to enable details error information in IIS and ASP.NET

Tagged Under :
By default, IIS and ASP.NET hide detailed error information to prevent revealing sensitive information about your web application.
Sometimes we need to see error details (think shared hosting) to debug our coding.

To display the error message. Add these entries to your web.config file to disable generic errors:

February 13 2014 MVC Model add a new field

Tagged Under : ,
After add extra field inside the Model. I get an error message when I run the application.

The error message as below:
The model backing the 'MyContext' context has changed since the database was created.
It was because the Model was different with Database field. And now, you need rebuild the Database structure again.

The solution is:
1. Open Package Manager Console from Tools -> Libraray Package Manager -> Package Manager Console
2. PM> Enable-Migrations -ContextTypeName <TheDatabaseName>
3. Set AutomaticMigrationsEnabled=true, from Migrations folder of Configuration file
4. PM> Add-Migration InitialCreate
5. PM> Update-Database
Now try run the application again. it will work fine and updated to the new Model.

Or you can use another way to solve the problem.

February 13 2014

Cannot attach the file as database

Tagged Under : ,
Because I did delete the DB file and now I get an error message when I run the application. I expected it was the DB file was deleted by me and it didn’t help me rebuild the DB.

To solve this issue, we need stop and delete the Database with follwing step:
1. Open Package Manager Console from Tools -> Libraray Package Manager -> Package Manager Console
2. Stop local DB with command: PM > sqllocaldb.exe stop v11.0
3. and then delete it PM > sqllocaldb.exe delete v11.0
Now try run the application again. it will work fine again.

December 30 2013 add class to App_Code folder

Tagged Under :
In ASP MVC4 Appliction I can add a cshtml file in app_code folder like below so that I can use my custom helper method in another cshtml.

@helper LabelExtensions(string input) {

Using the Helper in a Page.
<p>This is some paragraph text.</p>
@MyHelpers.LabelExtensions("My Test Label.")
<p>This is some paragraph text.</p>

<p>This is some paragraph text.</p>
<pre>My Test Label.</pre>
<p>This is some paragraph text.</p>

But if the helper is classes (cs file) then you are not allow call the helper function like above. Below example show you how to use it in a Page.

December 08 2013 MVC 4 Razor WebGrid auto increase row number

Tagged Under :
Below example showing you how to add the row number in WebGrid.

You just need add the following script inside the grid columns.
grid.Column(header: "No.", 
                format: item => item.WebGrid.Rows.IndexOf(item) + 1 + Math.Round(Convert.ToDouble(grid.TotalRowCount / grid.PageCount) / grid.RowsPerPage) * grid.RowsPerPage * grid.PageIndex)

December 06 2013 MVC Razor if else statement display output

Tagged Under :
Not all content container blocks start with a tag element tag, though, and there are scenarios where the Razor parser cannot implicitly detect a content block.

Razor addresses this by enabling you to explicitly indicate the beginning of a line of content by using the @: character sequence within a code block. The @: sequence indicates that the line of content that follows should be treated as a content block:
@if (someCondition) {
  @: Some content to display...

For more practical example, the below snippet demonstrates how we could output a "Not Stock" message to our product if numbers of product is zero.
@if (model.unit_stock == 0) {
  @: Not Stock

Because I am not wrapping the "Not Stock" message in an HTML tag element, Razor can’t implicitly determine that the content within the @if block is the start of a content block. We are using the @: character sequence to explicitly indicate that this line within our code block should be treated as content.