Is there any good reason to use FormCollection instead of ViewModel?

前端 未结 11 1580
离开以前
离开以前 2020-11-27 15:09

I\'ve inherited a code base written in ASP.Net MVC 4. Every post method takes a FormCollection. Aside from annoyance of having to access the values through quot

11条回答
  •  醉话见心
    2020-11-27 15:23

    There are always workarounds for getting away from a FormCollection lol.. you can have hidden fields bound to your view model variables in the form to your heart's content.

    Form collections mostly emerge from the laziness of creating a view model but still end up taking time trying to get figure out how to get the values out of it in your controller :P

    I think it was simply created in the very beginning of MVC as an alternative to using strongly typed views when having very simple forms - back in the days when everyone used ViewBag :) ... and once hey had it in there they couldn't just take it out as simple as that.

    Maybe you can use it if you are absolutely sure your view will never have more than one form input? Probably still a bad idea though..

    I cant find any recent articles talking about any advantages of form collections.. while strongly typed views are everywhere.

提交回复
热议问题