2

谁能帮助我如何在 ASP.net MVC3 (MS Visual Studio 2010) 查看页面上使用 UniDynArray?

我设法将引用(U2.Data.Client)添加到项目中,并且可以在控制器中使用它,但不能在视图页面中使用。

使用 UniDynArray 的原因是,我想将动态数组从 Controller 传递给 View 并返回给控制器。这样我就不必将每个字段都设置为 VIEWDATA 以便在 View 中使用。

4

2 回答 2

3

我想解释一下如何通过以下方式将 UniDynArray 从 Controller 传递给 MVC View:

  1. MVVM 模式(原始 UniDynArray)
  2. ViewBag 模式(原始 UniDynArray)
  3. MVVM 模式(将 UniDynArray、UniDynArray 展平为 .NET 对象数据表)
  4. MVVM 模式(将 UniDynArray、UniDynArray 展平为 POCO 对象)

在这篇文章中,我将回答 MVVM Pattern (Raw UniDynArray)。稍后我将介绍休息。

创建 ASP.NET MVC3 项目 图1

创建模型 图2

图3

添加控制器 图4

图5

创建视图 图片5a

图片5b

打开“CustomerViewModel.cs”文件并粘贴以下代码

命名空间 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;
        }
    }
}

}

打开“MyUniDynArrayController.cs”并粘贴以下代码。当您注意到您正在将对象传递给视图并且该对象具有 UniDynArray

命名空间 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);

    }

}

}

打开“MyUniDynArray\ Index.cshtml”并粘贴以下代码。@Model 包含 ViewModel 对象 (UniDynArray)

@{ ViewBag.Title = "索引"; }

MyUniDynArray

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

@Model.MyCustomer.MyUniDynArray

图片8

打开“Shared\Layout.cshtml”文件并添加以下行

<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>

图9

运行应用程序并按“MyUniDynArray”。您将在视图中看到 UniDynArray。我不确定您将如何将 UniDynArray 与 HTML5/Razor 控件绑定。这就是为什么我建议你扁平化 UniDynArray。

图6

图7

在 MVC 视图中键入 UniDynArray

在此处输入图像描述

于 2013-03-20T20:39:00.113 回答
0

在这篇文章中,我想描述“MVVM Pattern (flatten UniDynArray, UniDynArray to .NET Object Object)”。

创建模型 图1

创建控制器 图2

创建视图 图3

打开模型文件 (Models\CustomerViewModel2.cs) 并粘贴代码

命名空间 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;
    }
}

}

打开控制器文件 (Controllers\MyUniDynArray2Controller.cs)

命名空间 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);



    }

}

}

图4

打开视图文件 (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>

图5

打开“Shared\Layout.cshtml”文件并添加以下行

<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>

图6

运行应用程序并按“MyUniDynArray2”。您将看到 Flatten UniDynArray。基本上 UniDynArray 成为 .NET 对象的数组(列表)

图7

使用的宇宙子程序

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 :"玛格丽特":@FM:"01/06/1991":@VM:"06/07/1996":@VM:"11/08/1999":@VM:"12/10/2001"

返回

于 2013-03-24T01:26:41.663 回答