将逗号分隔的字符串从视图传递到另一个控制器 [英] Pass comma separate string from view to another controller

查看:163
本文介绍了将逗号分隔的字符串从视图传递到另一个控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

我需要将逗号分隔的字符串作为参数从View传递到另一个Controller.

例如:

我在/Home/findaContractor视图中有参数,并且想要传递/Postwork/Create Controller

目前,我正在传递如下参数:

Hello,

I have a requirement to pass comma separate string as a parameter from View to another Controller.

Ex:

I have parameter in /Home/findaContractor View and want to pass /Postwork/Create Controller

Currently, I am passing parameters like below:

str=''1,2,3,4,5'';
window.location.href = "/PostWork/Create?Contrators=" + str;



str =''1,2,3,4,5''是一串Id(在str中可能是更多Id).

目前,我正在传递查询字符串,但是如何传递查询字符串呢?因此它不应显示在querystring中,而且我还会在另一个控制器上获取string参数.

谢谢

我尝试过的事情:

目前,我正在传递如下参数:



str=''1,2,3,4,5'' is string of Id (it may be more Ids in str).

Currently, I am passing in querystring, but how can I pass instead Querystring? so it should not display in querystring and I also get string parameter on another controller.

Thanks

What I have tried:

Currently, I am passing parameters like below:

str=''1,2,3,4,5'';
window.location.href = "/PostWork/Create?Contrators=" + str;

推荐答案

str=''1,2,3,4,5'';
window.location.href = "/PostWork/Create?Contrators=" + encodeURIComponent(str);


如果您不希望这些值出现在查询字符串中,则您需要发出POST请求:
If you don''t want the values to appear in the querystring, then you need to make a POST request:
var form = document.createElement("form");
form.setAttribute("method", "POST");
form.setAttribute("action", "/PostWork/Create");

var contractors = document.createElement("input");
contractors.setAttribute("type", "hidden");
contractors.setAttribute("name", "Contractors");
contractors.setAttribute("value", str);

form.appendChild(contractors);
document.body.appendChild(form);
form.submit();


您的操作显然需要接受POST请求和GET请求.如果您尚未在操作中添加[HttpGet]属性,则应该可以正常工作.


Your action will obviously need to accept POST requests as well as GET requests. If you haven''t added an [HttpGet] attribute to the action, it should just work.


这篇关于将逗号分隔的字符串从视图传递到另一个控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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