最佳的方式来转发从一个ActionResult选择成另一个数据 [英] Optimal way to forward data selected from an ActionResult to another

查看:146
本文介绍了最佳的方式来转发从一个ActionResult选择成另一个数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不相信,标题以及把这么随意批评。 :)

我有一个控制器,返回上用户可以点击一些选项创建的项目清单(动态建成使用JS在客户端)的页面。由于用户满意,他们可以点击一个按钮,然后...

...目前,一些div是隐藏/显示,在(相同)页面转换到选择的一个只读的观众。

...优化,另一个的ActionResult 的应调用presenting的信息。

我的一击是,我不能在一个很好的方式决定将数据从一个页面转移到另一个:查询字符串是一种选择,存储/检索/从DB是其他。我不开心任何与这些。

什么是将数据传输到一个新的观点withing相同的控制器顺利和推荐的方式?


解决方案

  $。阿贾克斯({
    网址:'@(Url.Action(动作,控制器)),
    类型:'后',
    数据:{
        ID:身份证,
        数据1:DATA1
    },
    成功:函数(结果){
        如果(result.Success){
        }
});

有一个非常简单的方法就是像上面的,只要你想,你定义为许多领域,只要输入参数相匹配,他们将在控制器上接收

 公众的ActionResult动作(字符串ID,字符串数据1){...

如果你想获得更多的复杂,你可以建立列表和数组的JSON数据,然后它通常是一个好主意,它字符串化。

  VAR数据= {};
data.id ='ID';
Data.List模块= [];
data.list.push({名称:'名',位置:位置,等})

然后在Ajax调用

 数据:Json.stringify(数据)

再次,只要名称匹配,控制器将接收到它。希望这有助于

编辑:
Json.stringify]是用于发送数据的工具。我不知道在座的是做什么的详细资料,但建议使用更复杂的数据。这里的例子中,我用来发送一个模型回控制器,但你提到不想创建一个模型。我认为收到你需要有匹配的是什么数据定义的输入参数控制器端此数据。从上面我所名单是一个复杂的类型,这样你的控制器会是这样的。

 公众的ActionResult动作(字符串ID,列表与LT;的ComplexType>名单){...

I'm not confident that the title is well put so feel free to criticize. :)

I have a controller that returns a page on which the user can click on some options creating a list of items (dynamically built up on the client using JS). As the user is satisfied, they can click on a button and then...

...currently, some DIVs are hidden/displayed, converting the (same) page to a read-only viewer of the selection.

...optimally, another ActionResult should be invoked presenting the information.

My biff is that I can't decide on a good way to transfer the data from one page to another: query string is one option, storing/retrieving to/from DB is an other. I'm not happy with any of these.

What would be a smooth and recommended way to transfer the data to a new view withing the same controller?

解决方案

$.ajax({
    url: '@(Url.Action("Action", "Controller"))',
    type: 'post',
    data: {
        id: id,
        data1: data1
    },
    success: function (result) {
        if (result.Success) {
        }
});

A very simple way is like the above where you define as many fields as you want and as long as the input parameters match they will be received on the controller

public ActionResult Action(string id, string data1){...

if you want to get more complicated you can build lists and arrays with the json data and then it is usually a good idea to stringify it.

var data = {};
data.id = 'id';
data.list = [];
data.list.push({ name: 'name', location: 'location', etc })

then in the ajax call

data: Json.stringify(data),

again, as long as the names match the controller will receive it. Hope this helps

Edit: Json.stringify is a tool that is used for sending the data. I don't know all of the details of what it does but it is recommended to use for more complex data. The example here I used for sending a model back to the controller but you mentioned not wanting to create a model. I believe to receive this data on the controller side you need to have input parameters that match what is defined in data. From what I have above list is a complex type so your controller would be something like this.

Public ActionResult Action(string id, List<ComplexType> list){...

这篇关于最佳的方式来转发从一个ActionResult选择成另一个数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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