3

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

0

3 Answers 3

9

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I dont think will work because this is not dynamic views
0
<div class="card shadow mb-4">
<div class="card-header py-3">
    <h6 class="m-0 font-weight-bold text-primary">Designation Create</h6>
</div>
<div class="card-body">
    <form enctype="multipart/form-data" method="post">
        <div class="form-group">
            <label asp-for="Fullname"></label>
            <input class="form-control" asp-for="Fullname">
            <span class="text-danger" asp-validation-for="Fullname"></span>
        </div>
        <div class="form-group">
            <div class="d-flex">
                <img witdh="90px" src="~/imgs/agents/@Model.Image" />
            </div>
            <label asp-for="File"></label>
            <input accept="image/*" class="form-control" asp-for="File">
            <span class="text-danger" asp-validation-for="File"></span>
        </div>
        <div class="form-group">
            <label asp-for="DesignationId"></label>
            <select class="form-control" asp-for="DesignationId" asp-items='new SelectList(ViewBag.Categories,"Id","Name")'>
                <option disabled selected>
                    Designation Select
                </option>
            </select>
            <span class="text-danger" asp-validation-for="DesignationId"></span>
        </div>
        <button type="submit" class="btn btn-primary my-2">Submit</button>
    </form>
</div>

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
0
<div class="card shadow mb-4">
<div class="card-header py-3">
    <h6 class="m-0 font-weight-bold text-primary">Designation Create</h6>
</div>
<div class="card-body">
    <form enctype="multipart/form-data" method="post">
        <div class="form-group">
            <label value="Fullname"></label>
            <input class="form-control" value="Fullname">
            <span class="text-danger" value="Fullname"></span>
        </div>
        <div class="form-group">
            <div class="d-flex">
                <img witdh="90px" src="~/imgs/agents/@Model.Image" />
            </div>
            <label value="File"></label>
            <input accept="image/*" class="form-control" value="File">
            <span class="text-danger" value="File"></span>
        </div>
        <div class="form-group">
            <label value="DesignationId"></label>
            <select class="form-control"value="DesignationId" values='new SelectList(ViewBag.Categories,"Id","Name")'>
                <option disabled selected>
                    Designation Select
                </option>
            </select>
            <span class="text-danger" value="DesignationId"></span>
        </div>
        <button type="submit" class="btn btn-primary my-2">Submit</button>
    </form>
</div>

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.