Results 1 to 6 of 6

Thread: A question about MVC

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    May 2002
    Posts
    1,602

    A question about MVC

    Hi!

    I ahve been woring in ASP. NET for many years and is now starting my first MVC 3.0 project. Things are rather confusing, but I do like the pattern itself.

    How I have a small question, for someone who is good at explaining things..

    In my view I have this code (a search page and a list)

    Code:
    @using (Html.BeginForm())
    {
        <p>
            Find by name: @Html.TextBox("SearchString") &nbsp;
            <input type="submit" value="Search" />    
        </p>
    
    }
    And my controller looks like this:

    Code:
     public ViewResult Index(string sortOrder, string searchString)
            {
                ViewBag.NameSortParm = String.IsNullOrEmpty(sortOrder) ? "Name desc" : "";
                ViewBag.DateSortParm = sortOrder == "Date" ? "Date desc" : "Date";
             
                
                var students = from s in db.Students
                               select s;
    
                if (!String.IsNullOrEmpty(searchString))
                {
                    students = students.Where(s => s.LastName.ToUpper().Contains(searchString.ToUpper())
                                            || s.FirstMidName.ToUpper().Contains(searchString.ToUpper()));
                }
                switch (sortOrder)
                {
                    case "Name desc":
                        students = students.OrderByDescending(s => s.LastName);
                        break;
                    case "Date":
                        students = students.OrderBy(s => s.EnrollmentDate);
                        break;
                    case "Date desc":
                        students = students.OrderByDescending(s => s.EnrollmentDate);
                        break;
                    default:
                        students = students.OrderBy(s => s.LastName);
                        break;
                }
                return View(students.ToList()); 
    
            }
    How on earth is the search string parsed from the view to the controller? How is the textbox name searchString connected to the parameter of the Index method? Is it just the name? I have read it is a HTTP form post, but is all that stuff abstracted away from the developer?

    It feels very abstract at the moment. Note that I have tread two books on C# 4.0 and MVC 3 so I might be suffering from information overload...
    If someone can provide a nice clear explanation about this I would be more than grateful! I am supposed to explain this later to a junior developer (lol).

    /Henrik

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: A question about MVC

    Yes, it is just the name. That's part of the MVC magic. MVC will automatically map form fields to action parameters of the same name or properties of action parameters of the same name.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    May 2002
    Posts
    1,602

    Re: A question about MVC

    Thanks for the reply. That makes sense I guess. I just hope I don't hit a bottleneck or something in more advanced scenarios.

    Anyways, we are starting a new project here that involves MVC 3, and we use SQL Server as the database. This is in an enterprise environment. I have read about developing mvc apps in ent env, but I am still a bit unsure which is the best way to handle the database, basically we have 3 options

    * code first (feels really scary to me.... and everyone else)

    * entity framework (what happens in 5 years or 10 years when we have vs 2018, will I still be able to edit my model without having to hack the edmx files manually? Most people here are unsure about entity framework, because of it's tight integration with visual studio.

    * nhibernate (almost a standard here, but we find it very hard for new developers to learn)


    Also curious, we will develop a backend with WCF that will serve both the mvc client and windows mobile 6.5 with data. Which is the "best practice" way to hook up a mvc web app with wcf? I guess I can use the same model items both on backend and frontend. So it will basically be the WCF project that handle all data access, and just passes objects to the mvc.

    I appreciate your time to answer... it is really helpful.

    /Henrik

  4. #4
    Fanatic Member
    Join Date
    Feb 2000
    Location
    Dunmow,Essex,England
    Posts
    898

    Re: A question about MVC

    we are in the thick of writing an MVC3 application (enterprise scale). We are using SQLServer backend and have opted to use Entity Framework with the Poco Generator to keep the model in sync with the database. We are also using the Telerik controls for MVC.

    We have created a service layer, so that the controllers themselves are as thin as possible with the service layer doing all the donkey work with business logic.

    We have to link part of the application into a Legacy third party dll, and for this we are using WCF. This was a pig to get working asyncronously, until I realised that you have to use a seperate thread and do not use wsDualBinding as the binding method but just standard http. Now the WCF service works a treat and is fast.

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    May 2002
    Posts
    1,602

    Re: A question about MVC

    Thanks for your reply!

    We are it he process of building a compact framework client, a web admin client and a service backend. For the backend we use entity framework and wcf, but for the web admin, we have mixed feelings. I have done numerous tutorials with MVC 3, and somehow it feels like going back to 2000 when using inline vb code in asp files and having the backend in com+. ASp.NET webforms really has lots of functionality, but they sure have a way to grow in lines of code you place in the code behind... but with MVC it is the other way around, you really have to code everything... which can become tedious, even though you have new tools such as razor syntax and LINQ. But MVC 3 feels cleaner somehow, you can "get down to the basics" and focus on your individual components instead of drowning in controls and code behind soup in webforms.

    We will opt for a 20&#37; larger budget if we use mvc 3 just to cover the learning experience... and that will decide which we use.

    Thanks for your thoughts, if you have any more to share regarding mvc 3, please let me know!! We could sure use some insight in what problems existing mvc3 projects has suffered from.

    kind regards
    Henrik

  6. #6
    Frenzied Member tr333's Avatar
    Join Date
    Nov 2004
    Location
    /dev/st0
    Posts
    1,605

    Re: A question about MVC

    We use Code First and it is quite easy to maintain. The Entity Framework Power Tools CTP1 (http://visualstudiogallery.msdn.micr...2-846072eff19d) has a tool to Reverse Engineer Code First which will generate the POCO classes, DbContext, and Code First mapping for an existing database. It's not 100&#37; perfect so you sometimes have to rename classes when the auto-naming screws up.
    CSS layout comes in to the 21st century with flexbox!
    Just another Perl hacker,

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width