Question

Controller:

    [HttpPost]
    public ActionResult Edit(FormCollection form)
    {

        string name = form["firstname"];
    }

View:

@Html.TextBox("firstname", null, new { style = "width:100px" })

I am entering some text into the textbox and submitting the form but when I break the program at the string declaration, the name variable is empty, even though when I hover over form["firstname"], the value I entered in the textbox appears in Visual Studio.

How can take the text entered in the textbox and place it into a string variable?

Était-ce utile?

La solution

most likely this happens to you because this variable is not used anywhere in the code later on. If it sees that the variable is not used later, is it smart enough not to care about assigning a value to it.

to see if it works simply add this code after yours:

string s = name;
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top