在asp.net MVC和Razor中使用XML生成FORM

时间:2014-07-24 07:42:01

标签: c# xml asp.net-mvc razor

我想生成动态表单并将它们与来自xml文件的DB中的数据绑定。我们有一个桌面应用程序,几乎所有逻辑都在ms sql中,我们想制作Web界面(我希望避免手动制作数百种表格)。我想创建一个通用控制器,它接收一个xml文件并对db进行所有调用并在视图中显示数据。这在mvc4中甚至可能吗?

XML:

<eXaForm xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning"
xmlns:eXa="eXa"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="eXa file:/Z:/Projects/eXactSQL-WEB/eXactSchema.xsd">
<Name Type="Search">Product</Name>
<SqlQuery>
    <Query>select top 10 * from Product</Query>
</SqlQuery>
<Controls>
    <Control name="ID" type="Text" label="Šifra blaga" class="tekstbox">
        <HelpNote> (Ident)</HelpNote>
        <BoundField>bla_id</BoundField>
        <format>999999</format>
    </Control>
    <Control name="Description" type="Text" label="Opis" class="tekstbox">
        <HelpNote>Description of product</HelpNote>
        <BoundField>bla_genOpis</BoundField>
     </Control>
    <Control name="NeAktiven" type="Check" label="Neaktiven" class="checkbox">
        <BoundField>bla_neaktiven</BoundField>
    </Control>
</Controls>

到目前为止,我可以成功显示表单,但我有问题用值填充它。我可以通过名称以某种方式从模型获取数据,而不是使用model.products.bla_blago 剃刀:

//get data from db. I think I need here some universal class that is build on runtime or could I just send sqldatareader to the view???
        @foreach (var controle in Model.products)
        {<tr>
          //read nodes from xml
           @foreach (XElement control in childList.Descendants("Control"))
           {

          //Check what type is control of: text,checbox
            switch (@control.Attribute("type").Value)
            {

                //if text display textbox and bind data???
                case "Text":

                    string HelpNote = control.Elements("HelpNote").FirstOrDefault().Value;
                    string BoundField= control.Elements("BoundField").FirstOrDefault().Value;
                  //can I somehow get the value from Model by name of the column? I can't hard coded it because in next foreach there should be value from next column                     
                <td>@Html.TextBox(control.Attribute("name").Value, **Model.product["BoundField"] ???**, new { Title = HelpNote })</td>

                      break;   
                case "Check":

                <td>@Html.CheckBox(control.Attribute("name").Value, ?????)</td>
                      break;
                default:
                <text>
                <h1>Default</h1>
                </text>
                      break;
            }

           }           

        </tr>  

1 个答案:

答案 0 :(得分:0)

我成功地反思了。

 @foreach (var **controle** in Model.product)
        {<tr>

            @foreach (XElement control in childList.Descendants("Control"))
            {

                switch (@control.Attribute("type").Value)
                {

                    case "Text":
                        // Use the text block below to separate html elements from code
                        string HelpNote = control.Elements("HelpNote").FirstOrDefault().Value;
                        string value = control.Elements("BoundField").FirstOrDefault().Value;                           
                        var vrednost = **controle**.GetType().GetProperty(value).GetValue(**controle**, null);