Advertisements

Archive

Posts Tagged ‘MVC’

Kendo grid error : kendo.all.min.js:12 Uncaught TypeError: e.slice is not a function

November 13, 2017 Leave a comment

Getting Uncaught TypeError: e.slice is not a function error message while binding kendo grid with data using controller action method

Following is the sample code for View and Controller action

View :

$(function () {
$(“#grid”).kendoGrid({
height: 400,
dataSource: {
serverPaging: true,
serverFiltering: true,
serverSorting: true,
pageSize: 10,
transport: {
read: “Employee/Read”,
contentType: “application/json”,
type: “POST”
},
},
schema: {
data: “Data”,
total: “Total”,
},

columns: [
{ field: “Salary”, format: “{0:c}”, width: “150px” },
{ field: “EmployeeName”, width: “150px” },
{ field: “SalaryColor”, width: “100px” },
{ command: “destroy”, title: “Delete”, width: “110px” }
],

editable: true, // enable editing
pageable: true,
sortable: true,
filterable: true,
toolbar: [“create”, “save”, “cancel”], // specify toolbar commands
parameterMap: function (options) {
return kendo.stringify(options);
}
});
});

Controller Read Action :

public ActionResult Read(int take, int skip, IEnumerable<Sort> sort, Kendo.DynamicLinq.Filter filter)
{
SalesERPDAL salesDal = new SalesERPDAL();

var result = salesDal.Employees.OrderBy(p => p.FirstName)
.Select(p => new EmployeeViewModel
{
EmployeeName = p.FirstName,
Salary = “5000”,
SalaryColor = “yellow”
}).ToDataSourceResult(take, skip, sort, filter);

return Json(result,JsonRequestBehavior.AllowGet);
}

Problem is in above code we are passing whole result instead of only Data part of result should be passed.

return Json(result.Data,JsonRequestBehavior.AllowGet);

Hope this help !

Advertisements
Categories: ASP.NET Tags: , , ,

EF code first – Model compatibility cannot be checked because the database does not contain model metadata

October 10, 2017 Leave a comment

This suggests that migration table is out of sync (even if your data isn’t), and that’s been part of the db schema now (since 4.3 I think – under system tables).

There could be many reasons and ways to experience that error , but most of the time…

The problematic part is some combination of manually backing/restoring the full database with code changes alongside – I’m not entirely certain as to why always.

In short, even if Db-s are the same migration table data might not be – and hash comparison may fail (still full restore sounds like good enough – but you have ‘two sides’).

What works for me is to use
Update-Database -Script

That creates a script with a ‘migration difference’,
which you can manually apply as an SQL script on the target server database (and you should get the right migration table rows inserted etc.).

If that still doesn’t work – you can still do two things…

a) remove the migration table (target – under system tables) – as per http://blogs.msdn.com/b/adonet/archive/2012/02/09/ef-4-3-automatic-migrations-walkthrough.aspx comments in there – that should fail back to previous behavior and if you’re certain that your Db-s are the same – it’s just going to ‘trust you’,

b) as a last resort I used – make a Update-Database -Script of the full schema (e.g. by initializing an empty db which should force a ‘full script’),
find the INSERT INTO [__MigrationHistory] records,
just run those, insert them into the database,
and make sure that your databases – and code match,

that should make things run in sync again.

(disclaimer: this is not a bullet proof to work at all times, you may need to try a few things given your local scenarios – but should get you in sync)

Also this will work

I found the code will work by changing

static LaundryShopContext()
{
Database.SetInitializer<LaundryShopContext>(
new DropCreateDatabaseIfModelChanges<LaundryShopContext>());
}
into

static LaundryShopContext()
{
Database.SetInitializer<LaundryShopContext>(
new DropCreateDatabaseAlways<LaundryShopContext>());
}

 

Razor vs WebForm(aspx)


RAZOR Web Form(ASPX)
Razor View Engine is an advanced view engine and introduced with MVC3. This is not a language but it is a mark-up syntax. ASPX View Engine is the default view engine for the ASP.NET MVC that is included with ASP.NET MVC from the beginning.
Namespace:System.Web.Razor. Namespace: System.Web.Mvc.WebFormViewEngine
Extension:.cshtml with C# or

.vbhtml with VB extension for views, partial views, editor templates and for layout pages.

Extension:.aspx extension for views

.ascx extension for partial views & editor templates and .master extension for layout/master pages.

RAZOR is much easier and cleaner than Web Form. It uses @ symbol in coding.
e.g.@Html.ActionLink(“link”, “click”)
ASPX Uses <% and %> delimiter in coding.
e.g. <%: Html.ActionLink(“link”, “Click”) %>
RAZOR engine comparatively slow but provides better security than ASPX. Razor Engine prevents XSS attacks(Cross-Site Scripting Attacks) means it encodes the script or html tags like <,> before rendering to view. Web Form is comparatively faster but less secure than RAZOR. Web Form Engine does not prevent XSS attacks means any script saved in the database will be fired while rendering the page
Razor Engine, doesn’t support design mode in visual studio means you cannot see your page look and feel without running application. Web Form engine support design mode in visual studio means you can see your page look and feel without running the application.
Razor Engine support TDD (Test Driven Development) since it is not depend on System.Web.UI.Page class. Web Form Engine doesn’t support TDD (Test Driven Development) since it depend on System.Web.UI.Page class which makes the testing complex.

 

MVC: From where to find System.Web.MVC dll in a system

April 6, 2015 2 comments

Default path for System.Web.MVC dll

MVC 2
C:\Program Files (x86)\Microsoft ASP.NET\ASP.NET MVC 2\Assemblies\System.Web.Mvc.dll

MVC 3
C:\Program Files (x86)\Microsoft ASP.NET\ASP.NET MVC 3\Assemblies\System.Web.Mvc.dll

MVC 4
C:\Program Files (x86)\Microsoft ASP.NET\ASP.NET MVC 4\Assemblies\System.Web.Mvc.dll

MVC 5
C:\Program Files (x86)\Microsoft ASP.NET\ASP.NET Web Stack 5\Packages\ Microsoft.AspNet.Mvc.5.0.0\lib\net45\System.Web.Mvc.dll

Hope this help !

Understanding asp.net MVC (Model View Controller) architecture

November 28, 2012 3 comments

This article is intended to provide basic concept and fundamentals of asp.net MVC (Model View Controller) architecture workflow for beginners.

Introduction:

“M” “V” “C” stands for “MODEL” “VIEW” “CONTROLLER” , asp.net MVC is an architecture to develop asp.net web applications in a different manner than the traditional asp.net web development , web applications developed with asp.net MVC is even more SEO (Search Engine Friendly ) friendly.

Developing asp.net MVC application requires Microsoft .net framework 3.5 or higher.

MVC interaction with browser:

Like a normal web server interaction, MVC application also accept request and respond web browser same way.


Inside MVC architecture:

Whole asp.net MVC architecture is based on Microsoft .net framework 3.5 and in addition uses LINQ to SQL Server.

What is a Model?

  1. MVC model is basically a C# or VB.net class
  2. A model is accessible by both controller and view
  3. A model can be used to pass data from Controller to view.
  4. A view can use model to display data in page.

What is a View?

  1. View is an ASPX page without having a code behind file
  2. All page specific HTML generation and formatting can be done inside view
  3. One can use Inline code (server tags ) to develop dynamic pages
  4. A request to view (ASPX page) can be made only from a controller’s action method

What is a Controller?

  1. Controller is basically a C# or VB.net class which inherits system.mvc.controller
  2. Controller is a heart of whole MVC architecture
  3. Inside Controller’s class action methods can be implemented which is responsible for responding to browser OR calling view’s.
  4. Controller can access and use model class to pass data to view’s
  5. Controller uses ViewData to pass any data to view


MVC file structure & file naming standards

MVC uses a standard directory structure and file naming standards which is very important part of MVC application development.

Inside the ROOT directory of the application there must be 3 directories each for model, view and Controller.

Apart from 3 directories there must have a Global.asax file in root folder. And a web.config like a traditional asp.net application.

  • Root [directory]
    •  Controller [directory]
      • Controller CS files
    •  Models [directory]
      • Model CS files
    •  Views [directory]
      • View CS files
    • Global.asax
    • Web.config

Asp.net MVC Execution life cycle

Here is how MVC architecture executes the requests to browser and objects interactions with each other.

A step by step process is explained below: [Refer figure as given below]


Step 1: Browser request

Browser request happens with a specific URL. Let’s assume that user entering URL like: [xyz.com]/home/index/

Step 2: Job of Global.asax – MVC routing

The specified URL will first get parsed via application_start() method inside Global.asax file. From the requested URL it will parse the Controller, Action and ID.

So for [xyz.com]/home/index/:

Controller = home

Action = index()

ID = empty — we have not specified ID in [xyz.com]/home/index/, so it will consider as empty string

Step 3: Controller and Action methods

MVC now find the home controller class in controller directory. A controller class contains different action methods,

There can be more than one action method, but MVC will only invokes the action method which is been parsed from the URL, its index() in our case.

So something like: homeController.index() will happen inside MVC controller class.

Invoking action method can return plain text string OR rendered HTML by using view.

Step 4: Call to View (ASPX page)

Invoking view will return view() . a call to view will access the particular ASPX page inside the view directory and generate the rendered HTML from the ASPX and will respond back to the browser.

In our case controller was home and action was index(). So calling view() will return a rendered HTML from the ASPX page located at /views/home/index.aspx.

This is it, the whole process ends here. So this is how MVC architecture works.