c# - Asp.net ViewModel and Controller MVC inheritance -


being new mvc, i'm attempting edit pre-existing application following structure:

    public actionresult create()     {         return edit(new book());     } 

the edit method uses editview viewmodel. needed reference id i've added id viewmodel, works great editing books, throws exception when creating books of course no id exists model @ point.

what i've done is: copy properties editview createview viewmodel, , have editview extend createview as:

public class editview : createview {     public guid id { get; set; } } 

i replace reference edit method above

return create(new book()); 

and code create method per edit method without reference id property, works looks ugly due lots of duplicated code - have long edit , create methods in controller identical bar id property:

private actionresult edit(book book) { ....

        return view("edit", new editview         {             id = book.id,             (rest of object initializer code identical create method) 

this doesn't seem way go - perchance direction way of example on please?

what around have partial view shared create/edit fields. embed partial in create , edit views. edit view has id field. in controller can same; factor out shared code new method , call method create , edit actions. example here


Comments

Popular posts from this blog

javascript - AngularJS custom datepicker directive -

javascript - jQuery date picker - Disable dates after the selection from the first date picker -