简体   繁体   English

将查询参数从强类型视图传递回控制器

[英]Passing query parameters back to the controller from a strongly-typed view

Perhaps I'm making this harder than it needs to be, but... 也许我正在使这一过程变得比它需要的难,但是...

I have a strongly-typed view (a registration page) which works great however users arrive at the view using a link which is sent to them via email and this URL has a few querystring parameters in it that I need to work with when the registration form is submitted. 我有一个强类型的视图(注册页面),该视图效果很好,但是用户使用通过电子邮件发送给他们的链接到达该视图,并且此URL中有一些querystring参数,我在注册时需要使用该参数表格已提交。

I'm able to capture these parameters in the controller when the page is loaded and stuff them in the "view bag" and such, but I can't figure out how to hand them back to the controller when the form is submitted. 加载页面后,我可以在控制器中捕获这些参数并将其填充到“查看包”等中,但是我无法弄清楚在提交表单时如何将它们交给控制器。 The only parameter to the controller method is an instance of the "type", and I've even tried extending this type to hold the additional parameters but when I try to assign them in the view, there is no instance of the object (I'm guessing this gets instantiated during the POST which would explain why it's not available when the page is rendered). 控制器方法的唯一参数是“类型”的实例,我什至尝试扩展此类型以容纳其他参数,但是当我尝试在视图中分配它们时,没有该对象的实例( '我猜想这是在POST期间实例化的,这将解释为什么呈现页面时它不可用)。

In a nutshell, I'm looking for a way to hold on to some values from the querystring and to access these values in the controller during the submission of a strongly-typed view, if this is even possible. 简而言之,我正在寻找一种方法来保留查询字符串中的某些值,并在提交强类型视图的过程中(如果可能的话)在控制器中访问这些值。

Add all the parameters you need to a hidden input tag inside a form . 将所需的所有参数添加到form内的隐藏input标签中。 This will make those values get POSTed back to a controller action. 这将使这些值重新发布回控制器操作。

Your URL with the parameters links to a GET Action, right? 您的带有参数的URL链接到GET Action,对吗? If so, add the parameters names as varaibles to the declaration of that Action. 如果是这样,请将参数名称作为变量添加到该动作的声明中。 So for example, say my URL sent via email was: 例如,假设我通过电子邮件发送的URL是:

http://mywebsite.com/register?id=511&sl=department

Then my corresponding Action is: 那么我对应的动作是:

public ActionResult Register(int id, string sl)
{
    MyModel myModel = new MyModel();
    myModel.id = id;
    myModel.sl = sl;
    return View(myModel);
}

To keep those during a multi-step process you can either use Html.HiddenFor() in the view to add a hidden field or save to some other location (ie database). 要在多步骤过程中保留这些内容,可以在视图中使用Html.HiddenFor()添加隐藏字段,也可以保存到其他位置(即数据库)。


If you don't want to add them to your model then you can do this: 如果您不想将它们添加到模型中,则可以执行以下操作:

public ActionResult Register(int id, string sl)
{
    ViewData["id"] = id;
    ViewData["sl"] = sl;
    return View();
}

And now in the view have a hidden field for each. 现在,在视图中每个视图都有一个隐藏字段。 Then in the POST-to controller action: 然后在POST-to控制器操作中:

[HttpPost]
public ActionResult Register(MyModel myModel, int id, string sl)
{
    // the hidden fields are now in id and sl
    // ASSUMPTION: the names of "id" and "sl" don't exist in MyModel -- if they do, collision
    ...
    return View();
}

You could add appropriately named hidden fields in the view and add these as parameters onto the post function, if you want to keep them separate from the model. 如果要使它们与模型分开,则可以在视图中添加适当命名的隐藏字段,并将它们作为参数添加到post函数中。

Alternatively you should be able to add these onto the model and have them come back out as long as you assign values correctly on the way in. 或者,只要您在输入过程中正确分配了值,就应该能够将它们添加到模型中并让它们返回。

You can add them as named parameters to your controller action, or use the generic FormCollection , which will have all form values. 您可以将它们作为命名参数添加到控制器操作中,或使用将具有所有表单值的通用FormCollection

Alternatively, just add them to your model class, eg, RegisterModel , and they will be matched by name during model binding. 或者,只需将它们添加到您的模型类中,例如RegisterModel ,它们将在模型绑定期间按名称进行匹配。

To keep them separate from your page model, use either: 要将它们与页面模型分开,请使用以下任一方法:

protected ActionResult Register(RegisterModel model, string parameterName1, int parameterName2)
{
    // parameterName1 and parameterName2 now contain values from the form with the same names
    ...
}

OR 要么

protected ActionResult Register(RegisterModel model, FormCollection fc)
{
    // fc now contains all form values
    ...
}

You need to use Hidden Fields to carry over the parameters from URL to View to Controller. 您需要使用“隐藏字段”将参数从URL传递到“视图”到“控制器”。 For Example, if you have say an ID parameter in your model, which you need to pass to your controller, you should have the following in your Registration form in the view. 例如,如果您在模型中说了一个ID参数,需要将其传递给控制器​​,则该视图的Registration表单中应包含以下内容。

@Html.HiddenFor(m=>m.ID)

This way, when your form is posted back, your model will be populated with the ID property as well. 这样,当您的表单回发时,您的模型也将填充ID属性。

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

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