简体   繁体   English

在MVC中将数据从视图传递到局部视图

[英]Passing data from view to partial view in MVC

I want to send 3 parameters from View to Partial View and i am opening the partial view into new browser tab. 我想从View向Partial View发送3个参数,我将部分视图打开到新的浏览器选项卡中。

Parameters are 参数是

var Hdata = 'some value'; 
var abc = document.getElementById('mycontrol1');
var abcd = document.getElementById('mycontrol2');

These 3 parameters i want to send it to Partial view. 这三个参数我想将它发送到部分视图。 The code is like 代码就像

window.open('@Url.Action("Action", "Controller")', '_blank');

So how can i pass data to Partial view. 那么如何将数据传递给Partial视图呢?

Thanks in Advance. 提前致谢。

The easiest way is in the query string. 最简单的方法是在查询字符串中。 Something like. 就像是。

window.open('@Url.Action("Action", "Controller", new { data1 = Model.Data1 })', '_blank');

Then you are passing data to your new window. 然后您将数据传递到新窗口。 However now you have exposed your data to the URL and handling complex types can get difficult. 但是现在您已将数据暴露给URL并且处理复杂类型可能会变得困难。 If possible would be better to pass an identifier to your action that lets you rebuild the model you wish to display in the window. 如果可能,最好将标识符传递给您的操作,以便重建您希望在窗口中显示的模型。

window.open('@Url.Action("Action", "Controller", new { id = Model.Id })', '_blank');

From what I can understand from your question and the code provided is, You are extracting some values from DOM elements and then on some event trying to oepn a view in new tab. 根据您的问题和提供的代码我可以理解,您正在从DOM元素中提取一些值,然后尝试在新选项卡中查看视图。 Now you want these data of the DOM elements to be available in the new view as well. 现在,您希望DOM元素的这些数据也可以在新视图中使用。 If this understanding is correct then you can do like below. 如果这种理解是正确的,那么你可以这样做。

You already have this. 你已经有了这个。

var Hdata = 'some value'; 
var abc = document.getElementById('mycontrol1');
var abcd = document.getElementById('mycontrol2');

having these values now you just need to hit a controller which will render the view. 拥有这些值现在你只需要点击一个控制器来渲染视图。 So you can pass these values to controller by constructing a query string. 因此,您可以通过构造查询字符串将这些值传递给控制器​​。

var URL = '@Url.Action("Action", "Controller")' + '?Hdata=' +Hdata + '&abc='+abc +'&abcd='+abcd;
window.open(URL , '_blank');

Also your controller must be like so. 你的控制器也必须如此。

 public ActionResult YourControllerName(string Hdata,string abc, string abcd)
 {
    ViewBag.Hdata = Hdata;
    ViewBag.abc = abc;
    ViewBag.abcd= abcd;

    return PartialView();
 }

Now you will have the data available in your partial view via ViewBag. 现在,您将通过ViewBag在部分视图中获得数据。

OR the cleaner way would be to have a model to receive and pass the data. 或者更简洁的方法是让模型接收和传递数据。

 public class YourModel
 {
  public string Hdata {get;set;}
  public string abc {get;set;}
  public string abcd {get;set;}
 }

Then the controller would be 那么控制器就是

 public ActionResult YourControllerName(YourModel pageData)
  {
    return PartialView(pageData); //this requires your view to use model of type YourModel
   // OR
   // ViewBag.PageData = pageData;
   // return PartialView();
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM