asp.net-mvc-3u2universeuniobjectsu2netdk

Using UniDynArray on ASP.net MVC view page


Can anyone help me on how to use UniDynArray on ASP.net MVC3 (MS Visual Studio 2010) View Page?

I managed to add reference (U2.Data.Client) to the project and I'm able to use it in the Controller, but not in View page.

The reason to utilize the UniDynArray is that, I would like to pass a dynamic array from Controller to View and back to controller. This way I will not have to set every field to VIEWDATA in order to be use in View.


Solution

  • I would like to explain how to pass UniDynArray to MVC View from Controller the following ways:

    1. MVVM Pattern (Raw UniDynArray)
    2. ViewBag Pattern (Raw UniDynArray)
    3. MVVM Pattern (flatten UniDynArray, UniDynArray to .NET Object DataTable)
    4. MVVM Pattern (flatten UniDynArray, UniDynArray to POCO Object)

    In this post , I will answer MVVM Pattern (Raw UniDynArray). Later I will cover rest.

    Create ASP.NET MVC3 Project pic1

    Create a Model pic2

    pic3

    Add a controller pic4

    pic5

    Create a View pic5a

    pic5b

    Open ‘CustomerViewModel.cs’ file and paste the following code

    namespace Test_MvcApplication.Models {

    public class CustomerViewModel
    {
        public Customer MyCustomer { get; set; }
        public CustomerViewModel(Customer pCustomer)
        {
            MyCustomer = pCustomer;
        }
    }
    
    public class Customer
    {
        private UniDynArray myVar;
        public UniDynArray MyUniDynArray
        {
            get
            {
                U2ConnectionStringBuilder conn_str = new U2ConnectionStringBuilder();
                conn_str.UserID = "user";
                conn_str.Password = "pass";
                conn_str.Server = "localhost";
                conn_str.Database = "HS.SALES";
                conn_str.ServerType = "UNIVERSE";
                conn_str.AccessMode = "Native";   // FOR UO
                conn_str.RpcServiceType = "uvcs"; // FOR UO
                conn_str.Pooling = false;
                string s = conn_str.ToString();
                U2Connection con = new U2Connection();
                con.ConnectionString = s;
                con.Open();
                Console.WriteLine("Connected.........................");
    
                // get RECID
    
                UniSession us1 = con.UniSession;
    
                UniSelectList sl = us1.CreateUniSelectList(2);
    
                // Select UniFile
                UniFile fl = us1.CreateUniFile("CUSTOMER");
                fl.RecordID = "2";
                myVar = fl.Read();
                return myVar;
            }
            set
            {
                myVar = value;
            }
        }
    }
    

    }

    Open ‘MyUniDynArrayController.cs’ and paste the following code. As you notice that you are passing object to view and that object has UniDynArray

    namespace Test_MvcApplication.Controllers { public class MyUniDynArrayController : Controller { // // GET: /MyUniDynArray/

        public ActionResult Index()
        {
            Customer c = new Customer();
            UniDynArray r = c.MyUniDynArray;
    
            var l = new CustomerViewModel(c);
    
            return View(l);
    
        }
    
    }
    

    }

    Open ‘MyUniDynArray\ Index.cshtml’ and paste the following code. @Model contains ViewModel object (UniDynArray)

    @{ ViewBag.Title = "Index"; }

    MyUniDynArray

    ==================

    @Model.MyCustomer.MyUniDynArray

    pic8

    Open ‘Shared\Layout.cshtml’ file and add the following line

    <nav>
                <ul id="menu">
                    <li>@Html.ActionLink("MyUniDynArray", "Index", "MyUniDynArray")</li>
                    <li>@Html.ActionLink("Home", "Index", "Home")</li>
                    <li>@Html.ActionLink("About", "About", "Home")</li>
    
                </ul>
     </nav>
    

    pic9

    Run the application and press ‘MyUniDynArray’. You will see UniDynArray in View. I am not sure how are you going to bind UniDynArray with HTML5/Razor Controls. That’s why I sugest you to flatten UniDynArray.

    pic6

    pic7

    Typed UniDynArray in MVC View

    enter image description here