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

时间:2017-05-25 01:49:39

标签: c# asp.net asp.net-mvc razor model-view-controller

我对mvc很新,并且已经开始学习asp.net mvc 5和django

我想创建一个用户可以在运行时创建新视图的应用程序。因此,假设我在Web应用程序中创建了一个功能,供用户添加新页面,用户可以填写表单,例如标题可能是文本,或者要在视图上显示的字段,以及用户保存时info保存到数据库并创建一个新视图。

我的问题是:

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

  • 如何创建正确的网址以路由到新页面?

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

如果可以做到这一点的任何建议将不胜感激。感谢

1 个答案:

答案 0 :(得分:9)

我认为您需要根据用户需求创建一个在db中保存用户配置的页面。

从我的结论来看,我建议采用以下方法。

  1. 无论你从数据库得到什么数据,如下所示都会返回。
  2. enter image description here

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

      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);
      }
      
    2. 在视图中,您可以浏览模态循环并渲染细节。

      @model dynamic
      
      @using (Html.BeginForm("SubmitActionName", "ControllerName")
       {
       <div class="table-responsive">
        <table>
            @if (Model != null)
                {
                   foreach (var row in Model)
                    {
      
                    }
                }
        </table>
      
      </div>
      }
      
    3. 有关详细信息,请访问this链接。

相关问题