ASP.NET MVC Passing models by ActionLink

后端 未结 4 729
广开言路
广开言路 2020-12-14 07:53

I would like to pass a model and an int to a controller upon clicking an ActionLink.

@Html.ActionLink(\"Next\", \"Lookup\", \"User\         


        
4条回答
  •  無奈伤痛
    2020-12-14 08:30

    While I highly suggest you use a form to accomplish what your attempting to do here for security sake.

     @Html.ActionLink("Next", "Lookup", "User", new 
          { Forenames = Model.UserLookupViewModel.Forenames, 
            Surname = Model.UserLookupViewModel.Surname, 
            DOB = Model.UserLookupViewModel.DOB,  
            PostCode = Model.UserLookupViewModel.PostCode, 
            page = Model.UserLookupViewModel.curPage }, null)
    

    MVC will map the properties appropriately doing this; however, this will use your url to pass the values to the controller. This will display the values for all the world to see.

    I highly suggest using a form for security sake especially when dealing with sensitive data such as DOB.

    I personally would do something like this:

     @using (Html.BeginForm("Lookup", "User")
     {
         @Html.HiddenFor(x => x.Forenames)
         @Html.HiddenFor(x => x.Surname)
         @Html.HiddenFor(x => x.DOB)
         @Html.HiddenFor(x => x.PostCode)
         @Html.HiddenFor(x => x.curPage)
    
         
      }
    

    You can have multiple of these type of forms on the page if needed.

    Your controller then accepts a post but functions the same way:

     [HttpPost]
     public ActionResult Lookup(UserLookupViewModel m, int page = 0)
     {
         return this.DoLookup(m, page);
     }
    

提交回复
热议问题