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

j12*_*211 3 c# asp.net model-view-controller asp.net-mvc razor

我是mvc的新手,已经开始学习asp.net mvc 5和django了

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

我的问题是:

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

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

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

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

Dip*_*iya 9

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

从我的结束,我建议采取以下方法来做到这一点.

  1. 无论你从数据库得到什么数据,返回如下所示.

在此输入图像描述

  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);
    }
    
    Run Code Online (Sandbox Code Playgroud)
  2. 在视图中,您可以通过模态循环并渲染细节.

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

有关详细信息,请通过链接.