2

I have been developing an application that uses Models that are based on a single USP/dbml and using ViewBags to populate the look up lists for data entry and edit views. This has been ok until I start to implement business logic. The Dropdownlists because they are done with ViewBag.llkup aren't available in the Script functions unless explictly passed to the function. I could create a single model that had multiple USP in it. I was wondering what is considered best practice concerning this issue. Thanks Bruce I have the following code in a controller:

    using (var dc = new usp_TM_Select_ShortNameDataContext())
    {
        String[] s = this.User.Identity.Name.Split('\\');
        string[] s2 = s[1].Split('.');
        string a = s2[0] + '.' + s2[1];
        ViewBag.Message = a.ToString();
        ViewBag.DetailsList = new SelectList((System.Collections.IEnumerable)dc.usp_TM_Select_ShortName().ToList(), "short_title", "short_title");
        var poc = new usp_ARD_Select_POCDataContext();
        return View(poc.usp_ARD_Select_POC().Single());
    }

Both are strongly typed views but how do I return both if they are in a single model to the view so that they can be used (accessed)? I have more complex examples where I have a Single record returned with multiple lookups. I am currently doing this with ViewBag and would like to use ViewModels. Thanks Bruce

user1011441
  • 161
  • 2
  • 14

2 Answers2

0

The best practice 99% of the time is to create a strongly type View based off of your Models (or ViewModel, if it so applies). That way you can tailor your Views for the domain-specific Model(s).

  • While both of these answers are correct, they didn't quite answer my issue. The problem is not whether to use strongly type views (which I totally agree) but how to apply business logic to the choices that the user makes client side. My comprimise is that the model state can be seen in its client side state in the httppost so I do both client side businees logic and server side businees logic. – user1011441 Dec 16 '11 at 22:12
0

Having the view strongly typed to a ViewModel would be my choice.

You can see examples here:

http://www.bidn.com/blogs/mbrown/development/2139/mvc-3-view-models

When do I use View Models, Partials, Templates and handle child bindings with MVC 3

Community
  • 1
  • 1
Shawn
  • 1,871
  • 2
  • 21
  • 36