How to pass model from view to controller when click button in MVC?

后端 未结 2 706
梦如初夏
梦如初夏 2020-12-12 06:58

Index - View

@model SendAFaxWeb.Models.Send
//view start here

    

Test Upload File

相关标签:
2条回答
  • 2020-12-12 07:24

    You have implemented cross origin security like : @Html.AntiForgeryToken(). So you have to pass that value as parameter in your AJAX call as below.

    AJAX:

    data:{__RequestVerificationToken:$('[name=__RequestVerificationToken]').val();}
    

    Also you have to add attribute in controller.

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Send(Send contact)
    
    0 讨论(0)
  • 2020-12-12 07:26

    You can do it using the below code

    @model PK.LifeStyles.Web.Models.Reimbursement //model declaration on top
    
    <button type="button" onclick="location.href='@Url.Action("ActionName", "ControllerName",Model)'">
    

    Your code behind should look something like this

        public ActionResult Save(Reimbursement data)
        {
          // your  code
        }
       //just some random class
       public class Reimbursement{
             public string Destination { get; set; }
       }
    
    0 讨论(0)
提交回复
热议问题