Question

ASP MVC scaffolding creates Index, Create, Update, Delete views, but how practical is that in reality. There seems to be a lot of duplication of UI code with the Create, Update and Delete views. Would it not be more practical to have one view for listing and another for Add/Edit and use some switch to format the view appropriately for Adding or Editing, and allowing deletion off the listing and edit views without redirecting to another view, instead simply popping up some sort of a "Please confirm the delete..." message?

If anyone has done something like this and is willing to share some code snippets or T4 scaffolding templates for generic cases it would be greatly appreciated.

Was it helpful?

Solution

Actually the NuGet package MvcScaffolding does exactly that, using a CreateOrEdit partial view. (See here.) The add/edit views are then created by referencing the partial view (targeting a different controller action respectively):

<fieldset>
    @Html.Partial("_CreateOrEdit", Model)
    <input type="submit" value="Create" />
</fieldset>

Another alternative would be to use the default MVC scaffold (as defined in the model using data annotations attributes).

<fieldset>
    @Html.EditorForModel()
    <input type="submit" value="Create" />
</fieldset>

As far as delete, you can always add a second mini-form at the bottom of any view (or in a list):

@{ using (Html.BeginForm("Delete", "MyController", FormMethod.Post))
    {
        @Html.HiddenFor(model => model.id)
        <input type='submit' value='Delete' />
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top