在单击按钮时调用控制器方法时需要将文本框值作为参数传递
Need to pass text box values as parameters when calling controller method on button click
我正在开发一个 MVC 表单,它应该让用户在各个字段中输入他们的问题的一些描述,然后这些字段中的文本将被编译成一封电子邮件,并在用户点击后发送给我们"Submit" 按钮。
我已经准备好在用户单击按钮时将硬编码字符串作为参数传递。
@model IntakeFormV2.Models.BotRequest
@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<style type="text/css">
#textarea {
height: 500px;
}
</style>
<form class="form-horizontal" method="post" asp-controller="HomeController" asp-action="Create">
@Html.AntiForgeryToken()
<fieldset>
<!-- Form Name -->
<legend>New Request</legend>
<!-- Text input-->
<div class="form-group">
<label class="col-md-4 control-label" for="textinput">Summarize your issue in 100 characters</label>
<div class="col-md-4">
<input id="textinput" name="title" type="text" class="form-control input-md" required="" />
</div>
</div>
<!-- Textarea -->
<div class="form-group">
<label class="col-md-4 control-label" for="textarea">Let us know about any additional information here</label>
<div class="col-md-4">
<textarea class="form-control" id="textarea1" name="description"></textarea>
</div>
</div>
<!-- Button -->
<div class="form-group">
<label class="col-md-4 control-label" for="singlebutton"></label>
<div class="col-md-4">
<p>@Html.ActionLink("Submit Request", "Create", "Home", routeValues: new { title = "hello", description = "world" }, htmlAttributes: new { @class = "btn btn-primary"})</p>
</div>
</div>
</fieldset>
</form>
但我需要将用户输入的任何内容传递到 "textinput" 和 "textarea1" 字段,而不是传递 "Hello" 和 "World"。
我是 MVC 的新手,但我知道模型应该涉及到这里的某个地方。但是一旦输入中实际存在文本,我还无法找到如何从视图中设置模型值。
namespace RPAIntakeFormV2.Models
{
public class BotRequest
{
public string RequestTitle { get; set; }
public string RequestDescription { get; set; }
public string RequesterName { get; set; }
public string RequesterDepartment { get; set; }
private bool RequestAdded = false;
}
}
从广义上讲,这是我的控制器 - 我一直没有添加很多预期的功能,直到我可以在那里真正获得我需要的参数。
public class HomeController : Controller
{
private IEnumerable<IssuePriority> priorities;
private IEnumerable<IssueStatus> statusses;
public HomeController()
{
index i = 1;
}
public ViewResult Index()
{
return View("Index");
}
public ActionResult Create(string title, string description)
{
try
{
MessageBox.Show("Hello world");
return RedirectToAction("Index");
}
catch (Exception ex)
{
return View("Index2");
}
}
}
谁能告诉我我错过了什么?
因为您将输入标签和文本区域的名称设置为与您在操作中的参数相同,因此您可以将action="/Home/Create"
添加到表单并更改操作link <button>
标签或 <input type="submit" />
标签,像这样
<form class="form-horizontal" method="post" action="/Home/Create">
@Html.AntiForgeryToken()
<fieldset>
<!-- Form Name -->
<legend>New Request</legend>
<!-- Text input-->
<div class="form-group">
<label class="col-md-4 control-label" for="textinput">Summarize your issue in 100 characters</label>
<div class="col-md-4">
<input id="textinput" name="title" type="text" class="form-control input-md" required="" />
</div>
</div>
<!-- Textarea -->
<div class="form-group">
<label class="col-md-4 control-label" for="textarea">Let us know about any additional information here</label>
<div class="col-md-4">
<textarea class="form-control" id="textarea1" name="description"></textarea>
</div>
</div>
<!-- Button -->
<div class="form-group">
<label class="col-md-4 control-label" for="singlebutton"></label>
<div class="col-md-4">
<button>Submit Request</button>
</div>
</div>
</fieldset>
</form>
如果有两个Create方法,需要标记HttpPost属性
[HttpPost]
public ActionResult Create(string title, string description)
你的表单是 Post
的类型,那么你的控制器操作方法应该是 [HttpPost]
你的 "hello" 和 "world" 是有效的,因为你在你的查询参数中发送了这个它被认为是 [HttpGET]
所以首先,如果您需要提交表单,那么您的按钮应该是 SUBMIT
的输入类型
而不是 actionlink
使用输入类型 submit
<input type="submit" value="Submit Request" class="btn btn-primary" />
如果你习惯了asp.net core
那么
<form class="form-horizontal" method="post" asp-controller="HomeController" asp-action="Create">
此处 asp-controller
和 asp-action
是 asp.net 核心标签助手。
您的表单操作被认为是 Home/Create
只需将控制器用作 Home
并且如果您使用 MVC 4
作为 tag 在 MVC-4
taghelpers 中不受支持。
对于 MVC-4
你应该使用
@using(Html.BeginForm("create", "home", FormMethod.Post))
{
}
然后你的代码问题是在表单中设置你的动作并在你的动作方法上设置 [HttpPost]
属性。
你的操作应该如下所示
[HttpPost]
public ActionResult Create(string title, string description)
{
//----
}
和您的 HTML textbox
名称必须与您要绑定的模型 属性 相同。或者你可以使用 @Html.TextBoxFor()
喜欢的例子
如果您需要更多信息,请告诉我。
<div class="col-md-4">
<input id="textinput" name="RequestTitle" type="text" class="form-control input-md" required="" />
</div>
现在 id="textinput"
与您的模型绑定 属性 RequestTitle
我正在开发一个 MVC 表单,它应该让用户在各个字段中输入他们的问题的一些描述,然后这些字段中的文本将被编译成一封电子邮件,并在用户点击后发送给我们"Submit" 按钮。
我已经准备好在用户单击按钮时将硬编码字符串作为参数传递。
@model IntakeFormV2.Models.BotRequest
@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<style type="text/css">
#textarea {
height: 500px;
}
</style>
<form class="form-horizontal" method="post" asp-controller="HomeController" asp-action="Create">
@Html.AntiForgeryToken()
<fieldset>
<!-- Form Name -->
<legend>New Request</legend>
<!-- Text input-->
<div class="form-group">
<label class="col-md-4 control-label" for="textinput">Summarize your issue in 100 characters</label>
<div class="col-md-4">
<input id="textinput" name="title" type="text" class="form-control input-md" required="" />
</div>
</div>
<!-- Textarea -->
<div class="form-group">
<label class="col-md-4 control-label" for="textarea">Let us know about any additional information here</label>
<div class="col-md-4">
<textarea class="form-control" id="textarea1" name="description"></textarea>
</div>
</div>
<!-- Button -->
<div class="form-group">
<label class="col-md-4 control-label" for="singlebutton"></label>
<div class="col-md-4">
<p>@Html.ActionLink("Submit Request", "Create", "Home", routeValues: new { title = "hello", description = "world" }, htmlAttributes: new { @class = "btn btn-primary"})</p>
</div>
</div>
</fieldset>
</form>
但我需要将用户输入的任何内容传递到 "textinput" 和 "textarea1" 字段,而不是传递 "Hello" 和 "World"。
我是 MVC 的新手,但我知道模型应该涉及到这里的某个地方。但是一旦输入中实际存在文本,我还无法找到如何从视图中设置模型值。
namespace RPAIntakeFormV2.Models
{
public class BotRequest
{
public string RequestTitle { get; set; }
public string RequestDescription { get; set; }
public string RequesterName { get; set; }
public string RequesterDepartment { get; set; }
private bool RequestAdded = false;
}
}
从广义上讲,这是我的控制器 - 我一直没有添加很多预期的功能,直到我可以在那里真正获得我需要的参数。
public class HomeController : Controller
{
private IEnumerable<IssuePriority> priorities;
private IEnumerable<IssueStatus> statusses;
public HomeController()
{
index i = 1;
}
public ViewResult Index()
{
return View("Index");
}
public ActionResult Create(string title, string description)
{
try
{
MessageBox.Show("Hello world");
return RedirectToAction("Index");
}
catch (Exception ex)
{
return View("Index2");
}
}
}
谁能告诉我我错过了什么?
因为您将输入标签和文本区域的名称设置为与您在操作中的参数相同,因此您可以将action="/Home/Create"
添加到表单并更改操作link <button>
标签或 <input type="submit" />
标签,像这样
<form class="form-horizontal" method="post" action="/Home/Create">
@Html.AntiForgeryToken()
<fieldset>
<!-- Form Name -->
<legend>New Request</legend>
<!-- Text input-->
<div class="form-group">
<label class="col-md-4 control-label" for="textinput">Summarize your issue in 100 characters</label>
<div class="col-md-4">
<input id="textinput" name="title" type="text" class="form-control input-md" required="" />
</div>
</div>
<!-- Textarea -->
<div class="form-group">
<label class="col-md-4 control-label" for="textarea">Let us know about any additional information here</label>
<div class="col-md-4">
<textarea class="form-control" id="textarea1" name="description"></textarea>
</div>
</div>
<!-- Button -->
<div class="form-group">
<label class="col-md-4 control-label" for="singlebutton"></label>
<div class="col-md-4">
<button>Submit Request</button>
</div>
</div>
</fieldset>
</form>
如果有两个Create方法,需要标记HttpPost属性
[HttpPost]
public ActionResult Create(string title, string description)
你的表单是 Post
的类型,那么你的控制器操作方法应该是 [HttpPost]
你的 "hello" 和 "world" 是有效的,因为你在你的查询参数中发送了这个它被认为是 [HttpGET]
所以首先,如果您需要提交表单,那么您的按钮应该是 SUBMIT
的输入类型
而不是 actionlink
使用输入类型 submit
<input type="submit" value="Submit Request" class="btn btn-primary" />
如果你习惯了asp.net core
那么
<form class="form-horizontal" method="post" asp-controller="HomeController" asp-action="Create">
此处 asp-controller
和 asp-action
是 asp.net 核心标签助手。
您的表单操作被认为是 Home/Create
只需将控制器用作 Home
并且如果您使用 MVC 4
作为 tag 在 MVC-4
taghelpers 中不受支持。
对于 MVC-4
你应该使用
@using(Html.BeginForm("create", "home", FormMethod.Post))
{
}
然后你的代码问题是在表单中设置你的动作并在你的动作方法上设置 [HttpPost]
属性。
你的操作应该如下所示
[HttpPost]
public ActionResult Create(string title, string description)
{
//----
}
和您的 HTML textbox
名称必须与您要绑定的模型 属性 相同。或者你可以使用 @Html.TextBoxFor()
喜欢的例子
如果您需要更多信息,请告诉我。
<div class="col-md-4">
<input id="textinput" name="RequestTitle" type="text" class="form-control input-md" required="" />
</div>
现在 id="textinput"
与您的模型绑定 属性 RequestTitle