0

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

  • Please show your query and tag me here (using @) when you do that. – Denis V Apr 29 '16 at 15:00
  • @DenisV I just edited the question, please check it. – Ashwani Kumar May 03 '16 at 07:24
  • On your Edit Mode, you want to be able to edit all rows you want? Or you want to edit a specific row by time. Like this: [List With Edit](https://gyazo.com/2fbd96bb4b0f3a4395e8226650f979ea) and [Edit Page](https://gyazo.com/8d912109aea166d467026681023ffd04) – Denis V May 03 '16 at 08:47
  • I do not think you are getting my question. I do not want separate views for Edit and View only page. – Ashwani Kumar May 03 '16 at 09:43

0 Answers0