Asp.net mvc 如何将文本框中输入的值传递给操作方法
我正在使用MVC构建一个电影应用程序。CRUD是VisualStudio自动为我创建的。现在,我正在尝试为用户构建一个搜索功能。以下是我编写的代码:Asp.net mvc 如何将文本框中输入的值传递给操作方法,asp.net-mvc,asp.net-mvc-4,Asp.net Mvc,Asp.net Mvc 4,我正在使用MVC构建一个电影应用程序。CRUD是VisualStudio自动为我创建的。现在,我正在尝试为用户构建一个搜索功能。以下是我编写的代码: @using (Html.BeginForm("SearchIndex", "Movies", new {searchString = ??? })) { <fieldset> <legend>Search</legend> <label>Title</
@using (Html.BeginForm("SearchIndex", "Movies", new {searchString = ??? }))
{
<fieldset>
<legend>Search</legend>
<label>Title</label>
<input type ="text" id="srchTitle" />
<br /><br />
<input type ="submit" value="Search" />
</fieldset>
}
@使用(Html.BeginForm(“SearchIndex”,“Movies”,new{searchString=???}))
{
搜寻
标题
}
我已经构建了SearchIndex方法和相关视图。我就是找不到如何将文本框中输入的值传递给SearchIndex操作方法。
请提供帮助。您需要为输入字段指定一个名称:
<input type="text" id="srchTitle" name="movieToFind" />
注意:表单字段名称必须与控制器中预期的参数匹配。或映射到模型属性(如果需要“模型”) 您需要为输入字段指定一个名称:
<input type="text" id="srchTitle" name="movieToFind" />
注意:表单字段名称必须与控制器中预期的参数匹配。或映射到模型属性(如果需要“模型”) 在您的模型中:
public class Search
{
public String SearchText { get; set; }
}
[HttpPost]
public ActionResult SearchIndex(Search model)
{
String text = model.SearchText;
}
将视图设置为强类型,并使用
@Html.EditorFor(model => model.SearchText)
在您的控制器中:
public class Search
{
public String SearchText { get; set; }
}
[HttpPost]
public ActionResult SearchIndex(Search model)
{
String text = model.SearchText;
}
希望这能有所帮助。在您的型号中:
public class Search
{
public String SearchText { get; set; }
}
[HttpPost]
public ActionResult SearchIndex(Search model)
{
String text = model.SearchText;
}
将视图设置为强类型,并使用
@Html.EditorFor(model => model.SearchText)
在您的控制器中:
public class Search
{
public String SearchText { get; set; }
}
[HttpPost]
public ActionResult SearchIndex(Search model)
{
String text = model.SearchText;
}
希望这有帮助