I have been following this article for rendering view from database and then using VirtualPathProvider
class to register the route.
My main requirement is such that i want to render the same view, based on condition, so that it will open once in Edit mode and once in Read only mode.
Now i am trying to achieve this beacuse i do not want to change the views twice(once for edit and once for readonly) if i have to change the page, means adding new textboxes(edit mode) or labels (readonly mode) then it would mean to actually manage two views or more when the application expand and became more complex.
Saving the view in database as a string is option that i found on internet but then it is not possible to manipulate string in c# to add textboxes or labels dynamically.
So is it possible to achieve this kind of behaviour, I know many of us do not want to manage multiple views, also we would have over crowded solution folder for views if we add views as file in filesystem.
Any kind of opinion(help or criticism) is welcome.
EDIT
Using If Else
code block will result in same effect of managing multiple html's
EDIT 2
Code?, i only want to know the implementation, Instead of saving a view hardcoded in DB table, i want to be able to change the view according to the condition.
So my requirement is not only to serve the view from DB but to be able to change the View on demand, Hence making it very dynamic.
Any way i am providing a small implementation for this. without VirtualPathProvider
Class
Let's keep things simple, I am querying the database using EF
and passing the Model
to View
.
So in Controller
i am using following query.
public ActionResult DynamicView(int id)
{
var abc= db.DummyTable.Where(c => c.ID == id).ToList();
return View(abc);
}
Then in View
@model IEnumerable<Project.Models.DummyTable>
@foreach (var item in Model)
{
<tr>
<td>
<label>@item.Name</label>
</td>
<td>
<label>@item.Class</label>
</td>
<td>
<label>@item.RollNumber</label>
</td>
</tr>
}
So instead saving my whole view in the table, I want the view to be able to change if it is in Edit mode like I would want to have Textboxes instead of Labels.
Thank you