将javascript中的变量发送到一个模型 [英] send a variable in javascript to one model

查看:73
本文介绍了将javascript中的变量发送到一个模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好



如何将javascript中的变量发送到mvc中的一个模型?

有可能吗?

hello

how can i send a variable in javascript to one model in mvc?
is it possible?

推荐答案

使用JSON对象使用Jquery Ajax Post



Use Jquery Ajax Post using JSON object


.ajax({< br />
url:'@ Url.Action(MyAction)',< br />
类型:POST ,< br />
数据:{名称:Ram},< br />
成功:函数(结果){< br />
alert(result) ;< br />
},< br />
错误:function(xhr,ajaxOptions,thrownError){< br />
alert(xhr.status);< br />
alert(thrownError);< br />
}< br />
});< br />
.ajax({<br /> url: '@Url.Action("MyAction")',<br /> type: "POST",<br /> data: { name : "Ram" }, <br /> success: function (result) {<br /> alert(result);<br /> },<br /> error: function (xhr, ajaxOptions, thrownError) {<br /> alert(xhr.status);<br /> alert(thrownError);<br /> }<br /> });<br />



所以你的行动方法就像



public ActionResult MyAction(MyModel mymodel) )



您的模型应包含您从客户端传递的属性




So your action method will be like

public ActionResult MyAction(MyModel mymodel)

Your model should contain the properties which you are passing from client side

Public class MyModel<br />
{<br />
     public string name { get;set;}<br />
}<br />





谢谢,

Ramanathan



Thanks,
Ramanathan


这篇关于将javascript中的变量发送到一个模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆