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

前端 未结 11 1541
离开以前
离开以前 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:45

    Responding to the title question: yes.

    There are some situations that FormCollection needs to be used. For instance, suppose a ViewModel that has a property that implements the 1 to N relation (in concrete case, a TimesheetViewModel with ICollection), and the Controller has to perform a validation between the time entries to not get a time collision between the end time of an entry and the start time of the following entry. To mark a related entry with a validation error, how can be the line index be retrieved?

    Well, with the default model binding, the index value is lost in the Controller logic. Fortunately, FormController stores the index you used in the View and a more specific validation can be done.

提交回复
热议问题