1

I have a model class Person

public class Person
{
    public string name { get; set; }
    public string area { get; set; }
}

Now, in my Index view, I want to pass values from view to controller by taking value name property from user and area ="foo". I know how I can take values from user by like below

@using (Html.BeginForm())
{
  @Html.Label("Name")
  @Html.TextBoxFor(m=>m.name)

  <input type="submit" value="Name" /> 
}

Now, I want area ="foo" in views. This is general problem. Do not answer like, set value area="foo" in controller.

1 Answer 1

2

Add a hidden field to your form with name "area" and set the value as whatever you want. When your form is posted, the hidden field value will be also posted to your action method.

@using (Html.BeginForm())
{
  @Html.Label("Name")
  @Html.TextBoxFor(m=>m.name)
  <input type="hidden" name="area" value="foo" />
  <input type="submit" value="Name" /> 
}

Now you can get this in your HttpPost action method

[HttpPost]
public ActionResult Create(Person model)
{
  // check for model.name and model.area.
  // TO DO : Save and redirect
}

You should remember that, people can always update the hidden field value in the browser using some tools like firebug or so. If it is a sensitive information (Price of an item in a shopping portal) , don't read like this from client. Read it from server.

Sign up to request clarification or add additional context in comments.

3 Comments

actually, I have to pass url content from view to controller.. can you guide me how to do that
you mean a querystring value ?
Step 1: Controller Action ResetPassword. This sends a confirmation email. Step 2: Controller Action that receives the userId and the secret key for pass reset, inside the controller you check if both are valid, and then proceed to login the user and redirect to a ChangePasswordAction, this renders a view with fields for new pass and confirmation. Step 3: submit new password to controller to update.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.