user5297740
user5297740

Reputation: 147

MVC Data Model from URL to Controller

Hi i need to pass some data from an URL to a controller in MVC. The data are in @Value = @Request.QueryString["rt"] in this code:

@using (Html.BeginForm("ResetPasswordToken", "Account")){
@Html.AntiForgeryToken()
@Html.ValidationSummary(true, "Reset Password non riuscito")
<div class="container above-footer login-form">
    <div class="col-md-6" align="center" style=" margin-left:25%; margin-top:100px; margin-bottom:100px;">
        <div class="editor-label">
            @Html.LabelFor(m => m.ResetToken)
        </div>
        <div class="editor-field">
            @Html.TextBoxFor(m => m.ResetToken, new { @Value = @Request.QueryString["rt"] })
            @Html.ValidationMessageFor(m => m.ResetToken)
        </div>

And i need to retrieve this value in the AccountController when i go on the submit button associated to this view. What is the best mode to do this without see it in the page?

I know is a very simple question but i need to do only this modification for tomorrow and i am very busy. Thanks to all and sorry for the question

Upvotes: 0

Views: 42

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038720

In the controller action that rendered this view (the GET action):

model.ResetToken = Request["rt"];
return View(model);

and then in the view simply:

@Html.TextBoxFor(m => m.ResetToken)

or if you don't want this to be shown in the form you could also use a hidden field:

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

Now when the form is submitted back to the ResetPasswordToken action you can read this value from the model:

[HttpPost]
public ActionResult ResetPasswordToken(MyViewModel model)
{
    // you can use the model.ResetToken property here to read the value
}

Alternatively you could include this value as part of the url when generating the action attribute of your HTML form:

@using (Html.BeginForm("ResetPasswordToken", "Account", new { ResetToken = Request["rt"] }))
{
    ...
}

Upvotes: 2

Related Questions