How to get data in Controller from view

How to get data in Controller from view

pass value from view to controller | mvc 4 razor
how to pass data from view to controller on button click
how to pass data from view to controller in mvc
how to pass list from view to controller in mvc
how to pass data from view to controller in mvc using tempdata
how to pass data from model to view in mvc
how to pass data from controller to view using viewdata
pass model from view to controller on button click

I want to save the array from view to database but I can't get the array from view.

in view page

<?php
$nilai = array(
array(1,2,3,4),
array(a,b,c,d)
);

<a href="<?= BASEURL; ?>/penerima/tambah?nilai=$nilai" class="btn btn-l btn-info">simpan</a>

in controller page

<?php

class Penerima  extends Controller
{

    public function index()
    {
        $data['penerima'] = $this->model('Penerima_model')->getAll();
        $this->view('templates/header');
        $this->view('penerima/index', $data);
        $this->view('templates/footer');
    }
    public function tambah() and I'm using MVC concept
    {

        $nilai = $_GET['nilai'];
        $this->model('Penerima_model')->tambahData($nilai);
    }
}

I have two-dimensional array.

How can I resolve this problem?


I found the solution. Here is what worked for me. I was passing array of arrays as a string value using

json_encode()

The solution was to escape the characters, so I had to replace the above line with

htmlspecialchars(json_encode(array))

Solution:

 $nilai = array(
    array(1,2,3,4),
    array('a','b','c','d')
    );
<a href="http://localhost/code/tambah?nilai=<?= htmlspecialchars(json_encode($nilai)); ?>" class="btn btn-l btn-info">simpan</a>

And in the controller, I had to get my array from JSON with the following lines

function tambah(){
$dataJson = $this->input->get('nilai');
$dataArray = json_decode(htmlspecialchars_decode($dataJson), true);
print_r($dataArray);
}

Best Solution

The best way is to use session

$this->session->set_userdata('nilai',$nilai);
<a href="http://localhost/code/tambah" class="btn btn-l btn-info">simpan</a>

Now, get in the controller

function tambah(){
$array = $this->session->userdata('nilai');
}

Note: Load session library in controller or autoload

Various Ways to Pass Data From Controller to View in MVC, In the above image, you see how data is initialized on the "View" page itself using 'ViewBag.Title = "Index"' and then how it is getting rendered� Add a controller class by right clicking on Controllers folder. Select an Empty Controller from the list of Scaffold. Give a meaningful name to your Controller. Controller is ready now. By using ViewBag, we can pass the data from Controller to View. ViewBag – ViewBag gets the dynamic view data dictionary.


look reference Passing arrays as url parameter and if you are using codeigniter framework then to get value from URL try with

$nilai = $this->input->get('nilai');

ASP.NET MVC - Passing Data From Controller To View, ViewBag – ViewBag gets the dynamic view data dictionary. Create an object of your Model class and resolve it by namespace. ASP.NET. I have� Get the view's data in the Action Method. I will now explain the four ways to get the view's data in the controller action. These are: Using Traditional approach; Using the FormCollection Object; Using the Parameters; Strongly type model binding to view; Using Traditional Approach. In the traditional approach we use the request object of the HttpRequestBase class. The request object has view input field values in name/value pairs.


You can't send array in get request as it is. So you can change your code as shown below


 in view page
<?php
$nilai = array(
array(1,2,3,4),
array(a,b,c,d)
);

$nilai = json_encode($nilai);

<a href="<?= BASEURL; ?>/penerima/tambah?nilai=$nilai" class="btn btn-l btn-info">simpan</a>


in controller page

<?php

class Penerima  extends Controller`
{

    public function index()
    {
        $data['penerima'] = $this->model('Penerima_model')->getAll();
        $this->view('templates/header');
        $this->view('penerima/index', $data);
        $this->view('templates/footer');
    }
    public function tambah()
    {

        $nilai = $_GET['nilai'];
        $nilai = json_decode($nilai);

        $this->model('Penerima_model')->tambahData($nilai);
    }
}

Passing Data from Controller to View, back to Controller, back to , So, I have an object that needs to be passed through couple of views. Similar to the article NerdDinner Step 6: ViewData and ViewModel. I retain the data from Get� Here Mudassar Ahmed Khan has explained with an example, how to pass (get) data from View to Controller in ASP.Net MVC Core 2.1. This article will explain how to create Form Fields using Model class and then pass (get) data from View to Controller using Model class object in ASP.Net MVC Core 2.1.


You can json_encode the array and use urlencode to pass it.

In your view:

<?php
$nilai = array(
  array(1,2,3,4),
  array(a,b,c,d)
);
// encode to json string and encode to valid url
$nilai_urlencoded = urlencode(json_encode($nilai));

?>

<a href="<?= BASEURL; ?>/penerima/tambah?nilai=<?=$nilai_urlencoded; ?>" class="btn btn-l btn-info">simpan</a>

In your controller, decode the json string

<?php

class Penerima  extends Controller
{

    public function index()
    {
        $data['penerima'] = $this->model('Penerima_model')->getAll();
        $this->view('templates/header');
        $this->view('penerima/index', $data);
        $this->view('templates/footer');
    }
    public function tambah()
    {
        // Decode the url first and then convert the json_string to array
        $nilai = json_decode(urldecode($_GET['nilai'])));
        $this->model('Penerima_model')->tambahData($nilai);
    }
}

ASP.Net MVC How to pass data from view to controller, You can do it with ViewModels like how you passed data from your controller to view. Assume you have a viewmodel like this public class ReportViewModel� In Solution Explorer, right-click the Controllers folder and then click Add, then Controller. In the Add Scaffold dialog box, click MVC 5 Controller with views, using Entity Framework, and then click Add. Select Movie (MvcMovie.Models) for the Model class. Select MovieDBContext (MvcMovie.Models) for the Data context class.


4 Basic ways to pass data from Controller to View in ASP.NET MVC , In the Index action of your controller create a Product object and assign it to a ViewData dictionary object as follows. Make sure you have added a using statement for your Product Model class. Change the Index View file contents as follows, build and run your application. In this Model binding in ASP.NET Core article, we will learn How to pass data from View to Controller.. We learn what is Model binding is and how it works. The ASP.NET core allows us to bind data from various binding sources like HTML Forms using [FromForm], Route Values using [FromRoute], Query string using [FromQuery], Request body using [FromBody] and From Request Header using [FromHeader].


Passing data from Controller to View in ASP.NET Core MVC , In this tutorial, we are going to learn how to pass data from Controller to View. We should create View We already have a tutorial on Viewdata & ViewBag. There are four ways to pass the data from View to Controller which are being explained below :- 1) Traditional Approach: In this approach we can use the request object of the HttpRequestBase class. This object contains the input field name and values as name-value pairs in case of the form submit.


Passing Data [View-to-Controller, Controller-to-View & Controller-to , So we can easily get the values of the controls by their names using as indexer from the request object in the controller. For example: Let's say� You can do it with ViewModels like how you passed data from your controller to view. Assume you have a viewmodel like this. public class ReportViewModel { public string Name { set;get;} } and in your GET Action, public ActionResult Report() { return View(new ReportViewModel()); } and your view must be strongly typed to ReportViewModel