MVC 4 - Use a different model in partial view

前端 未结 4 427
走了就别回头了
走了就别回头了 2021-02-05 02:51

Please bear with my noobness, I\'m super new to the MVC pattern.

What I\'m trying to do

I am building a profile information page for re

4条回答
  •  傲寒
    傲寒 (楼主)
    2021-02-05 03:20

    In your [HttpPost] profil function, if modelstate.isvalid is false, you return your edit view, but you need to define your pmViewModel again , other wise your partial view will not have an object to display. Try using the following and let us know what happens

    [HttpPost]
    [Authorize]
    [ValidateAntiForgeryToken]
    public ActionResult Profil(ProfileModel model)
    {
        if (ModelState.IsValid)
        {
            //insert into database
            return Content("everything's good");
        }
        else
        {
            //outputs form errors
            var pmViewModel = new ProfileUserViewModel  
            {
                ProfileModelObject = profileModel,
                UserModelObject = userModel
            };
    
            return View(model);
        }
    }
    

提交回复
热议问题