Model is always NULL on form post to controller

后端 未结 2 581
执笔经年
执笔经年 2020-12-12 00:22

Whenever I submit the form the model passed into the controller is NULL. I\'ve spent ages looking at this. I think I am missing something fundamental here.

@         


        
相关标签:
2条回答
  • 2020-12-12 00:54

    You need to pass ReinviteVisitorModel model in your Action

    Update you action with this:

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult CreateMeeting(ReinviteVisitorModel model)
    {
        return RedirectToAction("ReinviteVisitor2", "Home", new { visitorId = model.NewMeeting.SubjectId });
    }
    
    0 讨论(0)
  • 2020-12-12 01:05

    The model in your view is typeof ReinviteVisitorModel which means the signature of the POST method must match since your posting ReinviteVisitorModel

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult CreateMeeting(ReinviteVisitorModel model)
    

    alternatively you can use the Prefix property of BindAttribute to strip the NewMeeting prefix from the names of the form controls your are posting.

    public ActionResult CreateMeeting([Bind(Prefix="NewMeeting")]Meeting model)
    

    Side note: Remove new { @Value = Model.Info.SubjectId } from the hidden input and instead set the value of NewMeeting.SubjectId in the GET method before you pass the model to the view.

    0 讨论(0)
提交回复
热议问题