简体   繁体   English

如何从视图向控制器传递参数

[英]how to pass parameters to controller from view

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 . 例如,这些参数是不在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. 字符串为“ FATHER”,当我单击表单中的“提交”按钮时,会将其从视图发送到控制器。 Anyone can suggest how I can do it? 任何人都可以建议我该怎么做?

So if this question is about asp.net MVC ... : Are you using a strongly typed view, do you have a ViewModel? 因此,如果这个问题是关于asp.net MVC ...的:您使用的是强类型视图,是否有ViewModel? If so, add a property of type string, say StringToHide , to it, set it to "FATHER" and then, in your form, add 如果是这样,请向其添加字符串类型的属性,例如StringToHide ,将其设置为“ FATHER”,然后在您的表单中添加

@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. 从视图传递到控制器时,反之亦然,这是一种整理所有数据的干净方法。

Assuming Asp.net MVC 假设Asp.net MVC

In View 视野中

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

In Controller 在控制器中

[HttpPost]
public ActionResult Menu(string parameterFromView)
{
    // do whatever
}

add a hidden input field to your form with a value of "FATHER": 将一个隐藏的输入字段添加到您的表单,其值为“ 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');

In jquery call append the text Father to the URL and accept it in controller. 在jquery调用中,将文本父亲添加到URL并在控制器中接受它。 see code below. 参见下面的代码。

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

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

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