When do you use View() vs. RedirectToAction

后端 未结 3 603
心在旅途
心在旅途 2021-02-18 14:38

This existing question sums up the basics of my question. The best answers there tells the difference between the two methods. I am looking for guidelines as to which method to

3条回答
  •  时光说笑
    2021-02-18 15:16

    I would venture to say there is a hard and fast rule (well as much as there can be) - the Post/Redirect/Get (PRG) pattern. The standard with MVC (and the html helpers actually expect you to use this pattern) is:

    1. post your data.
    2. If there is an error (i.e. ModelState.IsValid=false) then return View() otherwise return RedirectResult.

    If there was an error the HTML helpers will actually look at the posted values to redisplay as opposed to what you pass them by View(model) - again because the PRG pattern is 'supposed' to be what happened.

提交回复
热议问题