learnmore
learnmore

Reputation: 445

How to send data from view to controller in MVC3

Hello everyone I would like to ask how to send data from view to controller ? I would like to describe my question with my controller and view as you can see below

Here is the login action controller

[HttpPost]
public ActionResult Login(Panel model, string Username, string Password, string CaptchaValue, string InvisibleCaptchaValue)
{
    bool cv = CaptchaController.IsValidCaptchaValue(CaptchaValue.ToUpper());
    bool icv = InvisibleCaptchaValue == "";

    if (!cv || !icv)
    {
        ModelState.AddModelError(string.Empty, "The Captcha Code you entered is invalid.");
    }

    if (ModelState.IsValid)
    {
        if (model.Username == Username && model.Password == Password)
        {
            FormsAuthentication.SetAuthCookie(model.Username, false);
            return RedirectToAction("index", "Home");
        }
        else
        {
            ModelState.AddModelError("", "Check your name or password");
        }
    }
    return View();
}

So when user login, redirect to Home/index view. At this point everything is okey.

Here is my index view:

[Authorize]
public ActionResult index()
{
    return View();
}

My question is how can I hold user's password parameter and send from index view to different controller to use this parameter in controller method but how ? For example I would like to use password parameter at my index_test controller method in where clause but first of all I need to send this data from index.

public ActionResult index_test()
{
    return View(db.contents.Where(x => x.test_parameter== password).ToList());
}

Upvotes: 1

Views: 851

Answers (2)

Jan
Jan

Reputation: 16032

You have to add a parameter to your action method:

public ActionResult index_test(string password) { ...

In Your view you can either send data to the action via a standard link:

@Html.ActionLink("Click me", "index_test", "Controller", 
                                  new { password = "StringOrVariable")

Or by doing a form post:

@using(Html.BeginForm("index_test")) { 
    <input type="hidden" name="password" value="mypassword" />
    add some fields
    <input type="submit" value="Send" />
}

Upvotes: 3

62071072SP
62071072SP

Reputation: 1935

In your view post the form back to the controller, for example

<form action = "yourcontroller/youraction" method = "POST" enctype = "multiparrt/form-data">

Upvotes: 0

Related Questions