Transferring data from view to partial view in MVC

I want to send 3 parameters from View to Partial View, and I open a partial view on a new browser tab.

Options

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

These are the 3 parameters that I want to send in a partial view. The code is similar to

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

So, how can I pass the data in a partial view.

Thanks at Advance.

+9
source share
3 answers

If you just visualize a partial with only a partial name:

 @Html.Partial("_SomePartial") 

It will actually pass your model as an implicit parameter, just as if you were calling:

 @Html.Partial("_SomePartial", Model) 

Now, in order for your partial to actually use this, it must also have a certain model, for example:

 @model Namespace.To.Your.Model @Html.Action("MemberProfile", "Member", new { id = Model.Id }) 

Alternatively, if you are dealing with a value that does not apply to your view model (it is in the ViewBag or the value generated in the view itself somehow, then you can pass ViewDataDictionary

 @Html.Partial("_SomePartial", new ViewDataDictionary { { "id", someInteger } }); 

And then:

 @Html.Action("MemberProfile", "Member", new { id = ViewData["id"] }) 

As with the model, Razor implicitly passes your partial ViewData by default, so if you have ViewBag.Id in your view, you can reference the same thing in partial.

Please find the sample code below.

 @{ ViewBag.Title = "Index"; string someValue ="abcd"; } 

Now pass a string value like this

  <h2>Index</h2> @Html.Partial("_MyPartial", someValue) 

There is no change in how Partial View consumes your transferred data. As for the partial view, it gets data that you can access through @Model.

 Data received is : @Model 
+11
source

From what I can understand from your question and the provided code, you extract some values โ€‹โ€‹from the DOM elements, and then into some event trying to open the view in a new tab. Now you want this DOM element data to be available in a new view. If this understanding is correct, you can do it as shown below.

You already have it.

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

with these values, now you just need to click on the controller that displays the view. So you can pass these values โ€‹โ€‹to the controller by building a query string.

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

Also your controller should be like that.

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

You will now have the data available in your partial view through the ViewBag.

OR a cleaner way is to have a model for receiving and transmitting data.

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

Then the controller will 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(); } 
+1
source

The easiest way is in the query string. Sort of.

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

Then you transfer the data to a new window. However, now you have exposed your data to the URL, and the complexity of processing complex types can become difficult. If possible, itโ€™s best to pass the identifier to your action, which will allow you to rebuild the model that you want to display in the window.

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

Source: https://habr.com/ru/post/1244864/


All Articles