顯示具有 MVC 標籤的文章。 顯示所有文章
顯示具有 MVC 標籤的文章。 顯示所有文章

2014年6月5日 星期四

Return JsonResult in MVC

image

public ActionResult Movies()
{
var movies = new List<object>();

movies.Add(new { Title = "Ghostbusters", Genre = "Comedy", Year = 1984 });
movies.Add(new { Title = "Gone with Wind", Genre = "Drama", Year = 1939 });
movies.Add(new { Title = "Star Wars", Genre = "Science Fiction", Year = 1977 });

return Json(movies, JsonRequestBehavior.AllowGet);
}
 
 
<input name="btnGetMovies" id="btnGetMovies" type="submit" value="Get Movies">
<ul id="movieList"></ul>

<script src="~/Scripts/jquery-1.10.2.js"></script>
<script type="text/javascript">
$("#btnGetMovies").click(function () {
var actionUrl = '@Url.Action("Movies", "Home")';
$.getJSON(actionUrl, displayData);
});

function displayData(response) {
if (response != null) {
for (var i = 0; i < response.length; i++) {
$("#movieList").append("<li>" + response[i].Title + " " + response[i].Genre + " " + response[i].Year + "</li>")
}
}
}
</script>

2014年6月4日 星期三

Microsoft.AspNet.Identity in MVC 5

Microsoft.AspNet.Identity.Core

image

image

 

Microsoft.AspNet.Identity.EntityFramework

image

image

 

AccountController.cs

new UserManager<ApplicationUser>(…)

image

 

IdentityModel.cs

class ApplicationUser and ApplicationDbContext

image

 

1. Add customize field, FavoriteBook, into IdentityUser table

2. Add customize table, Movie, into IdentityDbContext

image

3. Don’t forget to add textbox of customize field on related Views

4. Don’t forget to do DB Migration to apply schema changes in database, otherwise we will see following error

image

Configuration.cs in Migration folder

5. new ApplicationUser{}

6. new PasswordHasher()

image

7. Or the other way to create default user in Seed method

image

8. Run command, update-database, in Package Manager Console

image

9. If want to add default Role to defaut user

image

2013年10月13日 星期日

Web Services by Using Service Stack Web Service Framework

Install Service Stack from NuGet

image

All dependencies will be installed too

image

After installation, you will several references have been included

image

 

On-line document of Service Stack

http://servicestack.net, click the Documentation button

image

Click Create your first webservice

image

This page is for non MVC application, so scoll down to the link for MVC

image

Click the link below:

https://github.com/ServiceStack/ServiceStack/wiki/Run-servicestack-side-by-side-with-another-web-framework.

image

Copy the content of Web.config

image

Other steps

image

 

Implement in Visual Studio

image

image

image

image

Add “Api” folder and one class, “HelloService.cs”.

Code separate into 3 parties:

1. Service

2. Request

3. Response, the name is always requestName+Response

image

Using the service that we just created

image

 

Testing on Web Service

The default web site URL has error, it is no doubt

image

Has to add “/api” to see available web services, but it’s still fail so far

image

Check the error message, we forgot to new an AppHost() in Application_Start() in Global.asax

image

So add the code, “new ProteinTrackerAppHost().Init();” in Application_Start() function

image

Now it works, and showing following screen.

One operation shows, Hello. It supports different format, XML, JSON, JSV, CSV, …etc.

Click JSON, for example.

image

Two routes are available for this service

image

Using /api/hello/xxx to consume the web service

image

In json format

image

In xml format

image

2013年7月29日 星期一

DotNet.Highcharts Report in MVC 4

If you want beautiful chart report like below in MVC 4, DotNet.Highcharts is one of the solution. It’s a javascript solution, and must easy to understand and use in the web application.

image

In this blog, I will show you step by step if your solution is MVC 4.

 

Step 1, Download and install DotNet.Hightcharts into your Visual Studio solution from either Manage NuGet Packages window or from Package Manager Console

image

Install-Package DotNet.Highchars

image

 

Step 2, Verify installed package:

1. DLL reference in References folder

image

2. Highcharts-3.0.1 folder and javascript files in Scripts folder

image

 

Step 3, Add two script reference, jQuery.js and Modernizr.js (version doesn’t matter, my jQuery 1.7.1 & modernizr 2.5.3)

image 

Note:

The place of these 2 scripts is quite important, I spend around 3 hours to realize it. It must happen BEFORE Body Section. Later, I will show you why.

 

Step 4, Create a new Report controller and Index Action.

image

public ActionResult Index()
{
    DotNet.Highcharts.Highcharts chart = new DotNet.Highcharts.Highcharts("chart")
        .SetXAxis(new XAxis
                    {
                        Categories = new[] { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }
                    })
        .SetSeries(new Series
                    {
                        Data = new Data(new object[] { 29.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4 })
                    });

    return View(chart);
}

 

Step 5, Add a BundleConfig in BundleConfig.cs of App_Start folder, "~/Scripts/Highcharts-3.0.1/js/highcharts.js"

image

 

Step 6, Add a new View, Index with strongly-typed Hightcharts Model class

image

 

Step 7, In the Index.cshtml, add code of part 2 & 3

image

@(Model)

@section Scripts {
    @Scripts.Render("~/bundles/HighChart")
}

 

Step 8, Complie and run the solution, click on Report to show the chart

image

 

Step 9, View HTML source to understand why jQuery & Modernizr must BEFORE body section.

image

It’s because DotNet.Highcharts will generate javascript code (point 2 in below picture) and the place is between body section.

image

 

Addtional Reference:

https://dotnethighcharts.codeplex.com/

image