在运行时创建动态视图 asp.net mvc

问题描述 投票:0回答:1

我对 mvc 相当陌生,并且已经开始学习 asp.net mvc 5 和 django

我想创建一个应用程序,用户可以在运行时创建新视图。假设我在 Web 应用程序中创建一个功能,供用户添加一个新页面,他们可以在其中填写表单,比如说标题可能是文本,或者他们想要在视图上显示的字段,当用户保存它时信息被保存到数据库并创建一个新视图。

我的问题是:

  • 你可以在运行时创建动态视图吗?

  • 如何创建正确的 URL 来路由到该新页面?

  • 如果前两个是可能的,您可以使用模型或视图模型来显示该页面的数据库内容吗?

任何关于是否可以做到这一点的建议将不胜感激。谢谢

c# asp.net asp.net-mvc razor model-view-controller
1个回答
9
投票

我认为你需要制作一个页面,根据用户需求将用户配置保存在数据库中。

从我的角度来看,我建议采用以下方法来做到这一点。

  1. 无论您从数据库中获取什么数据,都会返回如下所示的快照。

  2. 在控制器中创建一个操作,并将这些数据分配到操作中的一个数据表/列表中。

    public ActionResult LoadContent()
    {
        dynamic expando = new ExpandoObject();
        var model = expando as IDictionary<string, object>;
    
        /*
        Let say user insert the detail of employee registration form. Make the
        database call and get the distinct detail of particular inserted form by Id
        or whatever. As an example below datatable contains the data that you fetch
        during database call.
        */
    
        DataTable objListResult =
            HeaderViewActionHelper.GetFinalResultToRenderInGenericList(id);
    
        if (objListResult != null && objListResult.Rows.Count > 0)
        {
            foreach (DataRow row in objListResult.Rows)
            {
                model.Add(row["DisplayName"].ToString(),
                          row["DisplayNameValue"].ToString());
    
                /*
                If you want to handle the datatype of each field than you can
                bifurcation the type here using If..else or switch..case.
                For that you need to return another column in your result
                from database i.e. DataType coloumn.
                Add in your model as:
                model.Add(row["DisplayName"].ToString(),
                          row["DisplayNameValue"].ToString(), 
                          row["DataType"].ToString());
                */
            }
        }
    
        /* return the model in view. */
        return View(model);
    }
    
  3. 在视图中,您可以遍历模态循环并渲染细节。

    @model dynamic
    
    @using (Html.BeginForm("SubmitActionName", "ControllerName")
    {
        <div class="table-responsive">
            <table>
                @if (Model != null)
                {
                    foreach (var row in Model)
                    {
    
                    }
                }
            </table>
        </div>
    }
    

欲了解更多详情,请点击this链接。

© www.soinside.com 2019 - 2024. All rights reserved.