开发者

Model error and re-retrieving combo-box items

开发者 https://www.devze.com 2023-02-11 13:20 出处:网络
On loading up a form, I retrieve a list of items to put in a combo-box. Now lets say during the post, there is an error. I have to re-write the code to get and build back my viewmodel again?

On loading up a form, I retrieve a list of items to put in a combo-box. Now lets say during the post, there is an error. I have to re-write the code to get and build back my viewmodel again?

For eg.

    public ActionResult Index()
    {
        var vModel = GetViewModel();
        return View(vModel);
    }

    [HttpPost]
    public ActionResult Index(SomeModel model)
    {
        if (ModelState.IsValid)
       开发者_如何学JAVA {

        }
        else
        {
            //Why do I have to write monkey-code here?
            var vModel = GetViewModel();
            return View(vModel);
        }
    } 

Am I going to have to do it like this example where I have a method to build my viewmodel for both actions?


Why not just return Index(); instead?

0

精彩评论

暂无评论...
验证码 换一张
取 消