0

I am trying to get a value from a textbox of my View.

This is my View:

    @model MyDataIndexViewModel

@{
    <div class="row">
        <div class="col-xs-12 col-sm-12 col-md-12">
            <h1>Meine Daten</h1>
        </div>
    </div>
    var item = Model.User;
        <div class="row">
            <div class="col-xs-6 col-sm-6 col-md-6 myDataTitle">Email</div>
            <div class="col-xs-6 col-sm-6 col-md-6">
                @Html.TextBox("txtEmail", "", new { placeholder = item.Email})
            </div>
        </div>
}

<div class="row">
    <div class="col-xs-12 col-sm-12 col-md-12">
        <a class="btn btn-default pull-right" href="/ChangeMyData/Save">Speichern</a>
    </div>
</div>

This is my Controller:

   [HttpPost]
    public ActionResult Save()
    {
        var email = Request["txtEmail"].ToString();
        return View();
    }

I get the error just as it says in the Title. Thank you in advance!

Samuel Silva
  • 43
  • 1
  • 9

2 Answers2

6

VIEW:

@model MyDataIndexViewModel


@using (Html.BeginForm("Save", "CONTROLLER_NAME"))
{
<div class="row">
    <div class="col-xs-12 col-sm-12 col-md-12">
        <h1>Meine Daten</h1>
    </div>
</div>
var item = Model.User;
    <div class="row">
        <div class="col-xs-6 col-sm-6 col-md-6 myDataTitle">Email</div>
        <div class="col-xs-6 col-sm-6 col-md-6">
            @Html.TextBox("txtEmail", "", new { placeholder = item.Email, id="txtEmail"})
        </div>
    </div>
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12">
    <a class="submit btn btn-default pull-right">Speichern</a>
</div>
</div>
}

CONTROLLER

[HttpPost]    
public ActionResult Save()
{
    var email = Request.Form["txtEmail"].ToString();
    return View();
}
Saqib A. Azhar
  • 994
  • 1
  • 15
  • 27
1

You can either use strongly-typed viewmodel binding:

View

@model MyDataIndexViewModel

@* other stuff *@

@Html.TextBox("txtEmail", Model.Email)

Controller

[HttpPost]
public ActionResult Save(MyDataIndexViewModel model)
{
    var email = model.Email;
    return View();
}

Or use a TextBoxFor directly for model binding:

@model MyDataIndexViewModel

@* other stuff *@

@Html.TextBoxFor(model => model.Email)

Or if you still want to use HttpRequest members, Request.Form collection (a NameValueCollection) is available to retrieve text from txtEmail input:

[HttpPost]
public ActionResult Save()
{
    var email = Request.Form["txtEmail"].ToString();
    return View();
}

Note that Request["txtEmail"] is discouraged due to no compile time safety applied for it, because the key value may retrieved from Request.QueryString, Request.Form or other HttpRequestBase members.

Similar issue:

MVC TextBox with name specified not binding model on post

Access form data into controller using Request in ASP.NET MVC

Tetsuya Yamamoto
  • 24,297
  • 8
  • 39
  • 61
  • You can use indexing - and if you test OP's code you will see that `var email = Request["txtEmail"].ToString();` works just fine –  Oct 23 '17 at 08:48
  • Well, as Darin pointed [here](https://stackoverflow.com/questions/5088450/how-to-retrieve-form-values-from-httppost-dictionary-or) I surprised it is possible to use `Request["Key"]` but not recommended due to possibility to fail in runtime. – Tetsuya Yamamoto Oct 23 '17 at 09:29
  • `Controller.Request` is `HttpRequestBase` and has a default [Item](https://msdn.microsoft.com/en-us/library/system.web.httprequestbase.item(v=vs.110).aspx) property (but of course binding to a model is far better). OP has not shown us the code throwing the error (and their view does not even have a form or submit button, just a link which will never go to the `[HttpPost]` method) –  Oct 23 '17 at 09:37