Using same partial view for edit and display
I'd like to use the same partial view to both display a record and edit a record. The layout will be the same for both functions and it seems much cleaner than having an EditRe开发者_开发技巧cord partial view and a DisplayRecord partial view. Maintenance will be much easier if I only have one partial view to update.
I'm using this right now and it works:
<div class="editor-label">
@Html.LabelFor(model => model.FirstName)
</div>
<div class="editor-field">
@if (@ViewBag.ViewMode == "display")
{
@Html.DisplayFor(model => model.FirstName)
}
else
{
@Html.EditorFor(model => model.FirstName)
}
@Html.ValidationMessageFor(model => model.FirstName)
</div>
Is there a better way to do it?
Thanks.
I strongly suggest using seperate edit and display templates. Trying to do them in a single form makes them more confusing, and harder to maintain in the long run. Seperation of concerns is the mantra of MVC, and you're trying to combine concerns..
my 2 cents is to use two partial views. i believe it presents a better seperation of concerns and if you use say razor the amount of editing you need to do when you are updateing is minimal.
i also believe it represents a better model and allows for better seperation in your controller and above.
I recently had an application that needed a similar reuse of the same partial view. The idea was that the user would only see the display version unless they were authenticated and then furthermore made a request to change the data.
To avoid the big IF statement, I used the equivalent of the EditorFor in both (MVC 3 wasn't actually out yet, so I had some more work to do for it). But the key was that the "display version" disabled everything from the server side unless the requirements had been met.
The appearance was that of the editor in both cases, but only one allowed you to actually edit. I recommend a similar tack for you unless your requirements are different.
While I am on board with the importance of segregating concerns I don't think it's the only tenet of modern programming. What about code reuse?
It seems to me that having two partial views that are virtually identical begs for a common control (in this case a common partial view). I must say that the maintenance overhead of having to update two partial views outweighs whatever ills comes from not segregating the edit/display functions. If I change a field label, add/remove a field, change a type or length I have to remember to do that in two places...and ensure I do it exactly the same in both.
I agree completely about the segregation of concerns except it seems that's more important at a higher level. Keep the presentation/business/data layers absolutely separate. Who wants to go back to the days of Classic ASP? However, I can't imagine what bad things will come as a result of adding some conditionals to a partial view to allow for code reuse. Is it messy? Well, a little but still fairly readable and the intent is obvious. I guess the reason I asked this question was to see if anyone had a cleaner way of implementing code reuse than with the conditionals.
Does anyone agree that in this case code reuse outweighs the benefit of segregation of concerns? If so, is there a better way of implementing it?
Thanks
精彩评论