我如何将两个文本输入传递给 ActionResult 方法
How do i pass two text inputs into ActionResult method
几天来我一直在处理一个大型项目。除了这部分,其他都完成了。我正在尝试将 2 个文本框传递到我的家庭控制器方法中。
我试过使用 "model." 和 "item." 但没有成功。它总是传递 null
<p>
@Html.ActionLink("Display", "Home", FormMethod.Post)
</p>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div>
Month:<input type="text" name="Month" id="MonthInput">
Extra:<input type="text" name="Extra" id="ExtraInput"><br /><br />
<input type="submit" value="Add" class="btn btn-default"> <br> <br>
</div>
}
并且在家庭控制器中是
[HttpPost]
public ActionResult Display(String Month, String Extra)
{
if (ModelState.IsValid)
{
...
return RedirectToAction("Index");
}
return view;
}
我希望它在单击 "Add" 按钮时传递在文本框中键入的任何值,但它给出 null
为了正确映射到操作的参数,您必须使用 name
属性。
@using (Html.BeginForm("Display", "Home"))
{
@Html.AntiForgeryToken()
<div>
Month:<input type="text" name="Month" id="MonthInput">
Extra:<input type="text" name="Extra" id="ExtraInput"><br /><br />
<input type="submit" value="Add" class="btn btn-default">
</div>
}
完全不相关,但您似乎有 2 个嵌套表单,其中一个 POST 到 php
页面。这是想要的吗?
此外,删除以下代码,因为它没有任何意义:
<p>
@Html.ActionLink("Display", "Home", FormMethod.Post)
</p>
为了补充上一个答案,我建议在控制器中接受表单作为 FormCollection,因为表单似乎与模型无关:
[HttpPost]
public ActionResult Display(FormCollection collection)
{
if (ModelState.IsValid)
{
string month = collection["Month"];
string extra = collection["Extra"];
return RedirectToAction("Index");
}
return View();
}
试试这个,
在视图中
@using (Html.BeginForm("Display", "Home", FormMethod.Post))
{
@Html.AntiForgeryToken()
<div>
Month:<input type="text" name="Month" id="MonthInput">
Extra:<input type="text" name="Extra" id="ExtraInput"><br /><br />
<input type="submit" value="Add" class="btn btn-default"> <br> <br>
</div>
}
在控制器中,
[HttpPost]
public ActionResult Display(SomeClass someClass)
{
if (ModelState.IsValid)
{
...
return RedirectToAction("Index");
}
return view;
}
Create Normal class name SomeClass,名称按您的要求命名,
public class SomeClass{
public string Month {get;set;}
public string Extra {get;set;}
}
inputs的name属性要和SomeClass的属性匹配,它会自动映射,然后你就可以通过someClass对象访问这个值
someClass.Month and someClass.Extra
namespace Your.Models
{
public class DisplayModel
{
public string Month {get;set;}
public string Extra {get;set;}
}
}
[HttpPost]
public ActionResult Display(DisplayModel model)
{
if (ModelState.IsValid)
{
string month = model.Month;
string extra = model.Extra;
return RedirectToAction("Index");
}
return View();
}
@using Your.Models
@model DisplayModel
@using (Html.BeginForm("Display", "Home", new { id="displayview" }))
{
@Html.AntiForgeryToken()
<div>
Month:<input type="text" name="Month" id="MonthInput">
Extra:<input type="text" name="Extra" id="ExtraInput"><br /><br />
<input type="submit" value="Add" class="btn btn-default">
</div>
}
几天来我一直在处理一个大型项目。除了这部分,其他都完成了。我正在尝试将 2 个文本框传递到我的家庭控制器方法中。
我试过使用 "model." 和 "item." 但没有成功。它总是传递 null
<p>
@Html.ActionLink("Display", "Home", FormMethod.Post)
</p>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div>
Month:<input type="text" name="Month" id="MonthInput">
Extra:<input type="text" name="Extra" id="ExtraInput"><br /><br />
<input type="submit" value="Add" class="btn btn-default"> <br> <br>
</div>
}
并且在家庭控制器中是
[HttpPost]
public ActionResult Display(String Month, String Extra)
{
if (ModelState.IsValid)
{
...
return RedirectToAction("Index");
}
return view;
}
我希望它在单击 "Add" 按钮时传递在文本框中键入的任何值,但它给出 null
为了正确映射到操作的参数,您必须使用 name
属性。
@using (Html.BeginForm("Display", "Home"))
{
@Html.AntiForgeryToken()
<div>
Month:<input type="text" name="Month" id="MonthInput">
Extra:<input type="text" name="Extra" id="ExtraInput"><br /><br />
<input type="submit" value="Add" class="btn btn-default">
</div>
}
完全不相关,但您似乎有 2 个嵌套表单,其中一个 POST 到 php
页面。这是想要的吗?
此外,删除以下代码,因为它没有任何意义:
<p>
@Html.ActionLink("Display", "Home", FormMethod.Post)
</p>
为了补充上一个答案,我建议在控制器中接受表单作为 FormCollection,因为表单似乎与模型无关:
[HttpPost]
public ActionResult Display(FormCollection collection)
{
if (ModelState.IsValid)
{
string month = collection["Month"];
string extra = collection["Extra"];
return RedirectToAction("Index");
}
return View();
}
试试这个,
在视图中
@using (Html.BeginForm("Display", "Home", FormMethod.Post))
{
@Html.AntiForgeryToken()
<div>
Month:<input type="text" name="Month" id="MonthInput">
Extra:<input type="text" name="Extra" id="ExtraInput"><br /><br />
<input type="submit" value="Add" class="btn btn-default"> <br> <br>
</div>
}
在控制器中,
[HttpPost]
public ActionResult Display(SomeClass someClass)
{
if (ModelState.IsValid)
{
...
return RedirectToAction("Index");
}
return view;
}
Create Normal class name SomeClass,名称按您的要求命名,
public class SomeClass{
public string Month {get;set;}
public string Extra {get;set;}
}
inputs的name属性要和SomeClass的属性匹配,它会自动映射,然后你就可以通过someClass对象访问这个值
someClass.Month and someClass.Extra
namespace Your.Models
{
public class DisplayModel
{
public string Month {get;set;}
public string Extra {get;set;}
}
}
[HttpPost]
public ActionResult Display(DisplayModel model)
{
if (ModelState.IsValid)
{
string month = model.Month;
string extra = model.Extra;
return RedirectToAction("Index");
}
return View();
}
@using Your.Models
@model DisplayModel
@using (Html.BeginForm("Display", "Home", new { id="displayview" }))
{
@Html.AntiForgeryToken()
<div>
Month:<input type="text" name="Month" id="MonthInput">
Extra:<input type="text" name="Extra" id="ExtraInput"><br /><br />
<input type="submit" value="Add" class="btn btn-default">
</div>
}