设置一个值使用jQuery模型 [英] set a value to model using jQuery

查看:90
本文介绍了设置一个值使用jQuery模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何设置一个值使用jQuery?

我的模型

我有一个输入字段(它的id =意见),我想在它被插入 @ Model.Comment 正文
使用jQuery。

是这样的: @ Model.Comment = $(#注释)VAL();


解决方案

  我

如何设置一个值使用jQuery?

我的模型

这没有任何意义。 jQuery的运行在客户机上。示范居住在服务器上。因此,通过jQuery的客户端上执行的时候,服务器端code和模型是死多头。

您可以从客户端做的是发送一个AJAX请求到服务器传递给它的输入字段的值,使服务器可以采取相应的行动,并更新模型:

  $。阿贾克斯({
    网址:'@ Url.Action(富),
    输入:POST,
    数据:{注释:$(#注释)VAL()},
    功能(结果){
        // TODO:过程结果服务器
    }
});

在服务器上,你将有一个Foo控制器动作会被调用:

  [HttpPost]
公众的ActionResult美孚(字符串评论)
{
    // TODO:做一些与注释的值,并返回结果
    //到客户端
    ...
}

How can I set a value to my model using jQuery?

I have an input field (which its id="comment") and I want the text in it to be inserted into @Model.Comment using jQuery.

something like: @Model.Comment = $("#comment").val();

解决方案

How can I set a value to my model using jQuery?

This doesn't make any sense. jQuery runs on the client. The Model lives on the server. So by the time jQuery executes on the client, the server side code and the Model is long dead.

What you could do from the client is send an AJAX request to the server passing it the value of the input field so that the server can take respective actions and update the model:

$.ajax({
    url: '@Url.Action("foo")',
    type: 'POST',
    data: { comment: $("#comment").val() },
    function(result) {
        // TODO: process the server results
    }
});

Where on the server you will have a Foo controller action that will be invoked:

[HttpPost]
public ActionResult Foo(string comment)
{
    // TODO: do something with the value of the comment and return a result
    // to the client
    ...
} 

这篇关于设置一个值使用jQuery模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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