문제

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.

도움이 되었습니까?

해결책

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

다른 팁

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top