Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

creating dynamic views at runtime asp.net mvc

I'm fairly new to mvc, and have started learning asp.net mvc 5 and django

I want to create an application where the user can create a new view at runtime. So lets say I create a feature in the web app for a user to add a new page where they can fill out a form, say the title maybe text, or fields they want to display on the view, and when the user saves it that info gets saved to the db and creates a new view.

My questions are:

  • can you create dynamic views at runtime?

  • how do you create the proper url to route to that new page?

  • if the 1st two are possible can you use a model or viewModel to then display the content from the db for that page?

Any advice on if this can be done would be appreciated. Thanks

like image 476
j1211 Avatar asked May 26 '26 08:05

j1211


1 Answers

I think you need to make a page that saves the user configuration in the database as per user demand.

From my end, I suggest the following approach to do it.

  1. Whatever you gets the data from database which are returns like as below snap.

    enter image description here

  2. Make one Action in controller and assign those data in one datatable/list in 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);
    }
    
  3. In the view you can go through the loop over modal and render the detail.

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

For more detail, Please go through this link.

like image 69
Dipak Delvadiya Avatar answered May 30 '26 07:05

Dipak Delvadiya



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!