how to pass parameters to controller from view

630 views Asked by At

I need to pass parameters from a view to control in a form. These parameters are for example a string that is not in a textField. The string is "FATHER" and I send this string from the view to the controller when I click on the submit button in the form. Anyone can suggest how I can do it?

4

There are 4 answers

0
codecrunch On BEST ANSWER

add a hidden input field to your form with a value of "FATHER":

<?= form_hidden('name_of_field', 'FATHER') ?>

and in your controller, get the value when the form is submitted:

$value = $this->input->post('name_of_field');
0
EluciusFTW On

So if this question is about asp.net MVC ... : Are you using a strongly typed view, do you have a ViewModel? If so, add a property of type string, say StringToHide, to it, set it to "FATHER" and then, in your form, add

@Html.HiddenFor(StringToHide)

Then the information will be passed to the controller, but it is not editable by the user. And if you're not using a viewmodel yet, well, then you should. It's a clean way to compose all your data when passing from views to controllers and vice versa.

0
HaveNoDisplayName On

Assuming Asp.net MVC

In View

@{ Html.RenderAction("Menu", "", new { parameterFromView= "FATHER" }); }

In Controller

[HttpPost]
public ActionResult Menu(string parameterFromView)
{
    // do whatever
}
0
Amol Kulkarni On

In jquery call append the text Father to the URL and accept it in controller. see code below.

$("#submit").click(funtion(){
   window.url = "Controller/Action/" + "Father";
});