如何在asp.net mvc中检索文本框的值以将其存储到某个变量中?
我在首页视图页面上有一个像这样的文本框:<%=Html.TextBox("testbox") %>
我有一个按钮,像这样:<input type="submit" />
我正在使用打开新的mvc应用程序时出现的默认视图页面。
谢谢。
如何在asp.net mvc中检索文本框的值以将其存储到某个变量中?
我在首页视图页面上有一个像这样的文本框:<%=Html.TextBox("testbox") %>
我有一个按钮,像这样:<input type="submit" />
我正在使用打开新的mvc应用程序时出现的默认视图页面。
谢谢。
在你的控制器中:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Search(FormCollection collection)
{
String g = collection["textFieldname"]
}
TryUpdateModel(modelName);
public class MyFormViewModel
{
public string myInput {get; set;}
}
public ActionResult Search()
{
MyFormViewModel fvm = new MyFormViewModel();
return View(fvm);
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Search(FormCollection collection)
{
MyFormViewModel fvm = new MyFormViewModel();
TryUpdateModel<MyFormViewModel>(fvm);
string userInput = fvm.myInput;
}
如果在您的视图中;
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<YOURNAMESPACE.Controllers.MyFormViewModel>" %>
<%= Html.TextBox("myInput", Model.myInput) %>
<% using (Html.BeginForm()) {%>