2

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.

Glenn Sallis
  • 415
  • 3
  • 11
cwkueh
  • 23
  • 3
  • 1. I'm using UO.NET (UniVerse) 2. UniDynArray is reference from U2.DATA.CLIENT.dll under U2.Data.Client.UO namespace 3. Yes, we are using UniSubroutine (only at Controller) 4. No, I do not use U2 Add-ins – cwkueh Mar 20 '13 at 08:41
  • I have figured out how to pass UniDynArray from Controller to View and View to Controller. You can use • MVVM pattern • ViewBag pattern I think you should flatten UniDynArray to .NET POCO Class or .NET DataTable Object. This will make your work easy. Please wait for my response. I would like to answer this in an ‘article’ way (with screen shots) so that other U2 Developers can be benefitted. I would like to cover: • MVVM with RAW UniDynArray • ViewBag with RAW UniDynArray • MVVM with flatten UniDynArray (UniDynArray to DataTable) • MVVM with flatten UniDynArray (UniDynArray to POCO Class) – Rajan Kumar Mar 20 '13 at 16:24
  • Thanks a lot, Rajan. Anxiously waiting for your article :D – cwkueh Mar 21 '13 at 01:38
  • Please see my answer below. I will describe later 'MVVM Pattern (flatten UniDynArray, UniDynArray to .NET Object DataTable) '. – Rajan Kumar Mar 21 '13 at 04:28

2 Answers2

3

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

Rajan Kumar
  • 718
  • 1
  • 4
  • 9
  • First of all thanks for your effort on going through the trouble on getting the result and paste them nicely with step by step walkthrough, appreciate it. But, I would like to use UniDynArray at View, like how it is being used in Controller/Model (e.g. dim Variable as UniDynArray) – cwkueh Mar 22 '13 at 01:26
  • This is easy. See below. @{ U2.Data.Client.UO.UniDynArray lvar = @Model.MyCustomer.MyUniDynArray;

    @lvar

    }
    – Rajan Kumar Mar 22 '13 at 13:52
  • See screen shot in my answer area. You can see Typed UniDynArray in MVC View. – Rajan Kumar Mar 22 '13 at 20:20
  • I tried to type the command into "View" page but I just couldn't get "U2" on to my namespace list (sorry, couldn't post pictures, not enough reputation point). I've added U2.Data.Client to my project Reference. Am I missing something here? should I add this (<%@ register Assembly="U2ToolKit" Namespace="U2.Data.Client.UO" TagPrefix="U2.Data.Client" %>) in to my aspx page? – cwkueh Mar 25 '13 at 01:28
  • I would like to provide you my sample source code for both MVVM pattern (Raw and Flatten). Could you please contact U2 Support so that we can provide you source code? We do not need to do this (<%@ register Assembly="U2ToolKit".......... – Rajan Kumar Mar 25 '13 at 03:54
  • Hi Rajan, I've tried to send email to u2support@roketsoftware.com and it kept on rejecting me. Why? – cwkueh Mar 27 '13 at 08:00
  • Please try : U2AskUs@rs.com – Rajan Kumar Mar 29 '13 at 01:19
0

In this post , I would like to describe 'MVVM Pattern (flatten UniDynArray, UniDynArray to .NET Object Object) '.

Create a Model pic1

Create Controller pic2

Create View pic3

Open Model file (Models\CustomerViewModel2.cs) and paste the coode

namespace Test_MvcApplication.Models {

public class Customer2
{
    public int ID { get; set; }
    public string Name { get; set; }
    public DateTime HireDate { get; set; }
}

public class Customer2Repository
{
    private List<Customer2> m_custList = new List<Customer2>();
    public  List<Customer2> CustomerList 
    {

        get
        {
            U2ConnectionStringBuilder l = new U2ConnectionStringBuilder();
            l.Server = "localhost";
            l.UserID = "user";
            l.Password = "pass";
            l.Database = "HS.SALES";
            l.ServerType = "universe";
            string lconnstr = l.ToString();
            U2Connection c = new U2Connection();
            c.ConnectionString = lconnstr;
            c.Open();
            U2Command command = c.CreateCommand();
            command.CommandText = "CALL MV_TO_DATASET_SELECT_SUBROUTINE(?,?)"; // UniVerse subroutine
            command.CommandType = CommandType.StoredProcedure;
            U2Parameter p1 = new U2Parameter();
            p1.Direction = ParameterDirection.InputOutput;
            p1.Value = "";
            p1.ParameterName = "@arg1";

            U2Parameter p2 = new U2Parameter();
            p2.Direction = ParameterDirection.InputOutput;

            p2.Value = "";
            p2.ParameterName = "@arg2";


            command.Parameters.Add(p1);
            command.Parameters.Add(p2);

            command.ExecuteNonQuery();


            string lRetValue = (string)command.Parameters[1].Value;


            //command.Parameters[1].MV_To_POCO<int>();
            m_custList = command.Parameters[1].MV_To_POCO<Customer2>();




            return m_custList;
        }


        set
        {
            m_custList = value;
        }

    }
}

public class CustomerViewModel2
{
   public Customer2 MyCustomer2 { get; set; }
   public List<Customer2> CustomerList { get; set; }
    public CustomerViewModel2(Customer2 pCustomer)
    {
        MyCustomer2 = pCustomer;
    }
    public CustomerViewModel2(List<Customer2> pCustomerList)
    {
        CustomerList = pCustomerList;
    }
}

}

Open Controller file (Controllers\MyUniDynArray2Controller.cs)

namespace Test_MvcApplication.Controllers { public class MyUniDynArray2Controller : Controller { // // GET: /MyUniDynArrayController2/

    public ActionResult Index()
    {
        Customer2Repository lvar = new Customer2Repository();
        List<Customer2> lCustomer2List = lvar.CustomerList;


        var l = new CustomerViewModel2(lCustomer2List);

        return View(l);



    }

}

}

pic4

Open View File (Views\MyUniDynArray2\Index.cshtml)

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>
<table border="1"> 
<tr> 
    <td>ID</td> 
    <td>Name</td> 
    <td>HireDate</td> 

</tr> 
@foreach (var myItem in Model.CustomerList)
{

    <tr> 
        <td>@myItem.ID</td> 
        <td>@myItem.Name</td> 
        <td>@myItem.HireDate</td> 

    </tr> 
}
</table>

pic5

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

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

                </ul>
</nav>

pic6

Run the application and press ‘MyUniDynArray2’. You will see Flatten UniDynArray. Basically UniDynArray becomes array of .NET objects(List)

pic7

Used UniVerse Subroutine

SUBROUTINE MV_TO_DATASET_SELECT_SUBROUTINE(ARG_INPUT,ARG_OUTPUT)

x = ARG_INPUT

ARG_OUTPUT = "100":@VM:"101":@VM:"102":@VM:"103":@FM:"Nancy":@VM:"Andrew":@VM:"Janet":@VM:"Margaret":@FM:"01/06/1991":@VM:"06/07/1996":@VM:"11/08/1999":@VM:"12/10/2001"

RETURN

Rajan Kumar
  • 718
  • 1
  • 4
  • 9