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

匿名 (未验证) 提交于 2019-12-03 01:25:01

问题:

Index - View

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

Test Upload File

@Html.AntiForgeryToken()
@Html.TextBoxFor(m => m.Recipients[0].Number)
@if (Model != null) { foreach (var item in Model.Documents) {
  • FileName: @item.Name
  • } }

    Javascript- the javascript doesnt work

     

    Controller

    public ActionResult Send(Send contact) {     //some code here } 

    I tried to pass model by using javascript to the controller, but its not working. The alert in javascript also not popup. Can any one tell me what wrong with the code.

    回答1:

    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) 


    回答2:

    You can do it using the below code

    @model PK.LifeStyles.Web.Models.Reimbursement //model declaration on top  

    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; }    } 


    标签
    易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
    该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!