如何将ObservableArray传递给MVC控制器?没有Ajax [英] How to pass a ObservableArray to MVC controller ? Without Ajax
问题描述
嗨朋友
我有一个带有视图模型数据列表的可观察数组,我需要传递给Normal控制器,它返回类型为ActionResult
public ActionResult Index(list< qualities> data)
{
return excelCon.DownloadTMExcel(data); // 这里我将actionResult作为ReturnType
}
Java脚本代码为休闲:
var URL = / DownloadExcel / ?索引数据=跨度> + self.qualities(); // self.qualities保存我的整个列表,它在控制器中命中断点但我得到零列表。
window .open(URL, _ blank 跨度>);
实际上,激动人心的情况是当一切正常时我会在新窗口打开的情况下下载excelsheet。
我需要有关如何传递observableArray的建议,就像我处理的方式一样。
我厌倦了:
var 网址= / DownloadExcel / Index?data = + ko.toJson(self.qualities()); // 这不是控制器本身
我尝试使用Ajax调用仍然以一种方式工作,即我可以传递ObservableArray但ActionResult返回它无法处理的类型。总是它去了ajax调用的错误功能,我不会下载我的excel。
唯一适用于我的案例:还有其他方案我只需要将参数传递给控制器然后我就可以打开一个新窗口并下载相关的Excel。
var URL = / DownloadExcel / Index?typeId = 2;
window .open(URL, _ blank 跨度>); // 打开新窗口后我获得了excel
编辑:
我的观点:
< a data-bind = click:data.submit> download < / a >
我的视图模型Js:
self.submit = function ()
{
var URL = < span class =code-string> / DownloadExcel / Index?data = + self.qualities();
window .open(URL,_ blank);
}
如上所述点击后我进入提交功能提交我必须做我做的任何事情来通过我observableArray to controller。
非常感谢任何帮助。
这可能是一个比这更好的方法,但这就是我在大约5分钟内放在一起的东西。在这个例子中,我发布在Person对象列表中。
public 类人
{
public string 标题{ get ; set ; }
public string Forename {获得跨度>; set ; }
public string 姓氏{获得跨度>; set ; }
}
控制器类如下:
public class HomeController:Controller
{
[HttpGet]
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Index( string people)
{
var serializer = new Newtonsoft.Json。 JsonSerializer();
var data = serializer.Deserialize< list>< person>>(
new JsonTextReader(
new TextBufferReader(
new SeekableTextReader(people)) ));
return 查看();
}
} < / person > < / list >
最后是HTML和JavaScript:
< !DOCTYPE html >
< html >
< head >
< title > 索引< 跨度> / title >
< / head >
< ; 正文 >
< div >
< 表格 action = / Home / Index 方法 = POST >
< 输入 id = 人员 名称 = people type = text / >
< 按钮 类型 = 按钮 data-bind < span class =code-keyword> = 点击:提交 > ; 提交< / button >
< / form >
< / div >
< script src = / Scripts /jquery-2.1.1.min.js\" type = text / javascript > < / script >
< script src = / Scripts / knockout-3.1.0.js type = text / javascript > < / script >
< script type = text / javascript >
var Person = function (title,forename,surname){
this.title = ko.observable(title);
this.forename = ko.observable(forename);
this.surname = ko.observable(surname);
}
this.people = ko.observableArray();
this.people.push(新人(先生,鲍勃,马利));
this.people.push(新人(Mrs,Grace,Jones));
this.people.push(新人(先生,米克,贾格尔));
函数submit(){
(#people)。val(JSON.stringify(ko.toJS(this.people())));
( 形式)提交();
}
ko.applyBindings(this);
< / script >
< / body >
< / html > 跨度>
Hi friends
I am having an observable array with a list of view model data which i need to pass to Normal controller which is having a return type of ActionResult
public ActionResult Index(list<qualities> data)
{
return excelCon.DownloadTMExcel(data); //here i get actionResult as ReturnType
}
Java script code as fallows:
var URL = "/DownloadExcel/Index?data="+self.qualities(); //self.qualities holds my entire list which hits break point in controller but i get Zero list .
window.open(URL, "_blank");
Actually the excat scenario is when everything works fine i get excelsheet downloded with new window open .
I need advice in how to pass observableArray like the way i am dealing .
I tired something like :
var URL = "/DownloadExcel/Index?data="+ko.toJson(self.qualities()); //this dont to controller itself
I tried using Ajax call still it works one way i.e i can pass ObservableArray but ActionResult return Type it can't handle . always it goes to error function of ajax call and i wont get my excel downloaded .
The only case worked for me : There is other scenario i just need to pass parameters to controller then i am able to open a new window and download relevant excel .
var URL = "/DownloadExcel/Index?typeId="+2;
window.open(URL, "_blank"); //on open of new window i get excel downloaded
EDIT :
My view :
<a data-bind="click:data.submit">download</a>
My view model Js:
self.submit = function()
{
var URL = "/DownloadExcel/Index?data="+self.qualities();
window.open(URL, _blank);
}
As mentioned above after click i get into submit function inside submit i have to do whatever i do to pass my observableArray to controller .
Any help is dearly appreciated .
There's probably a nicer way of doing it than this, but this is what I've put together in about 5 minutes. In this example, I'm posting in a list of Person objects.
public class Person { public string Title { get; set; } public string Forename { get; set; } public string Surname { get; set; } }
The controller class is as follows:
public class HomeController : Controller { [HttpGet] public ActionResult Index() { return View(); } [HttpPost] public ActionResult Index(string people) { var serializer = new Newtonsoft.Json.JsonSerializer(); var data = serializer.Deserialize<list><person>>( new JsonTextReader( new TextBufferReader( new SeekableTextReader(people)))); return View(); } }</person></list>
And finally the HTML and JavaScript:
<!DOCTYPE html> <html> <head> <title>Index</title> </head> <body> <div> <form action="/Home/Index" method="POST"> <input id="people" name="people" type="text"/> <button type="button" data-bind="click: submit">Submit</button> </form> </div> <script src="/Scripts/jquery-2.1.1.min.js" type="text/javascript"></script> <script src="/Scripts/knockout-3.1.0.js" type="text/javascript"></script> <script type="text/javascript"> var Person = function(title, forename, surname) { this.title = ko.observable(title); this.forename = ko.observable(forename); this.surname = ko.observable(surname); } this.people = ko.observableArray(); this.people.push(new Person("Mr", "Bob", "Marley")); this.people.push(new Person("Mrs", "Grace", "Jones")); this.people.push(new Person("Mr", "Mick", "Jagger")); function submit() {
("#people").val(JSON.stringify(ko.toJS(this.people())));
("form").submit(); } ko.applyBindings(this); </script> </body> </html>
这篇关于如何将ObservableArray传递给MVC控制器?没有Ajax的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!