1

I been doing asp.net mvc for a while now and recently came across this techdays tutorial .

This seemed very interesting but has left me kind of confused on how do I get started doing something similar.

Currently I have a web application made with

1.  Jquery
2.  Asp.net mvc 3 razor
3.  Nhibernate 

My application is basically one page with a couple tabs and everything is controlled by ajax and jquery model dialog.

I followed this patterned for my stuff

View -> View Model -> Controller -> Service layer (in a separate library)-> nhibernate

[HttpGet]
public ActionResult Courses()
{
    // get all courses back from service layer
    // automap domain results to view model
    return View(vm);
}

[HttpPost]
public ActionResult CreateCourse(CourseFormViewModel vm)
{
   // check if data meets basic validation
    if (ModelState.IsValid)
    {
       //map back to domain object
        // send to service layer
        // return data back view through json.

        return Json(data);

    }
    // return errors back to server
    return Json(wrapper);
}

// my view model that contains basic validation

   public class CourseFormViewModel
    {
        public int CourseId { get; set; }

        [Required(ErrorMessage = "Course name is required")]
        [StringLength(40, ErrorMessage = "Course name cannot be this long.")]
        public string CourseName { get; set; }
    }

// View

My view is pretty much has some strongly typed html helps that use the view model.

// Javascript

As I mentioned I use a lot of Ajax. I will most of the time use serliazeArray and then post the data to a controller Action Result(CreateCourse) which will then in turn bind the values for the view model.

// Service Layer

In my service layer I will do some business logic and then save to the database using nhibernate if needed. If I feel it is necessary I will make Domain Transfer Object otherwise I will just use my Domain Object.

I been recently looking at jquery mobile and phone gap as I been put on a project that uses this technologies and they just seemed very useful. As I am very familiar with asp.net mvc I then started to look if I could use mvc in the mix, that’s when I came across the video I posted at the top of this post.

I am still not 100% sold on single page applications as for instance if I were to look at my current personal project I am making and wanted to make a mobile version of it, the application would have to be quite different in presenting the data to the user.

For instance my web application has a table that just needs lots of space to display everything and probably would not look good on mobile. This would lead me to either have to trim the table down or think of a different way of displaying it (What I probably would do).

I like the fact that what Steve made would work on all mobile devices and I would like to try to something similar to what he is doing but the version I would make would just be for mobile devices and maybe small tablets.

Everything else that could support 1024 resolution and higher I would think should use the current web application.

I just don’t where to begin and how to make it fit with what I am using.

  1. He is using webapi and Entity frame work but how do I make this work with nhibernate?

  2. Do I not use View Models anymore? It seems like he just hooked up his domain model and sent that all to the view. I am not too crazy about that as I like view models.

  3. He uses Knockout.js and all its binding stuff such as binding to a button he uses to send data back to the server. That is nice but what happens when you’re using jquery dialogs and stuff that generate its own buttons. How do you hook knockout in these cases?

  4. He mentions that he could then take his project and use something like phonegap to put it in a market place. How could he do that if he is using asp.net mvc code as phone gap only supports html/javascript/css?

Mark Coleman
  • 40,542
  • 9
  • 81
  • 101
chobo2
  • 83,322
  • 195
  • 530
  • 832
  • I just watched that video and im amazed, the single page makes it much more simplier and producive, i would like to see the particular answers of 1-3 as it drown my attention too. –  Jun 01 '12 at 23:12

1 Answers1

0

With regards to point #3. I'm using Knockout with jqQuery dialog boxes without any problem. First, you can't attach data binding to the dialog created buttons. I'm not sure that you really need to. The dialog box buttons act as click event handlers. In those handlers, you can then do what every you need to the bound View Model.


Edit I've put a demo at http://jsfiddle.net/ex5Qy/3/

One thing to remember is what Knockout is designed to do. It binds a viewmodel to the html elements. That's all. You still need all the rest of the normal logic to make the page do what you need.

photo_tom
  • 7,292
  • 14
  • 68
  • 116
  • Could you give me some examples(with code) of how your using it. Maybe I don't fully understand when to use Knockout and when it is not needed. Do you still use View Models as well? – chobo2 Jun 02 '12 at 17:16
  • So knockout is really to areas of the page update easier? I seen what you done many times where 2 areas of the site get updated at the same time due to changes in another area. I am guessing your save method would have saved the changes to the db. I think what I am getting mixed up with is that Steve uses upshot that combined with knockout can handle the saving part too. In your example how would ViewModels come into effect? Say that dialog was an edit page and I wanted to fill them with values from the server and I was using html helpers? – chobo2 Jun 03 '12 at 18:49
  • You are correct about the save method. I do that quite often. Upshot is an entirely different item from knockout, event though they work well together. If you are just getting started, skip upshot until you feel comfortable with knockout. – photo_tom Jun 03 '12 at 19:27
  • Ya I got to take these things in steps, jquery mobile,knockout,upshot,webapi taking it all at once is just to overwhelming. I still not sure how to incorporate knockout into my application as I still don't know all the things I could do with it. – chobo2 Jun 03 '12 at 20:09
  • I'm still learning knockout myself after a year's use. It's a really cool tool limited by your own imagination. In learning knockout, where every you would use a javascript event, you do with data in knockout. It's a really profound shift in thinking that simplifies what you need to do. – photo_tom Jun 03 '12 at 23:49
  • Can you elaborate what you mean by javascript event. Are you talking about click events. I know your example was meant to be simple but I don't understand for instance what the advantage of using KO for saving vs just using a jquery selector. – chobo2 Jun 04 '12 at 18:50
  • Give me a couples of days. I'm putting a blog post together on the subject. – photo_tom Jun 05 '12 at 10:21
  • Sweet. Can't wait! I was also thinking of your example and a table. Say I had like you had an edit dialog and I am editing a row from that table but I want to do what you are doing and update each cell as the field in the edit box gets updated. What happens if they click cancel? Can knockout revert the object back to before the observed change? Or do you have to keep track of it yourself? – chobo2 Jun 05 '12 at 16:20