ASP .NET MVC html.textbox not returning value

杀马特。学长 韩版系。学妹 提交于 2020-01-06 19:30:14

问题


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?


回答1:


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;


来源:https://stackoverflow.com/questions/15167635/asp-net-mvc-html-textbox-not-returning-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!