How to pass model from view to controller when click button in MVC?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
Index - View
@model SendAFaxWeb.Models.Send //view start here
Test Upload File
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.
[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; } }