如何将表单数据从jQuery获取到ASP.NET控制器
How do I get form data from jQuery to ASP.NET controller?
我对web开发完全陌生。在阅读了文档和谷歌搜索后,我对这个问题同样感到困惑。我有一个控制器、一个模型和一个执行form.submit()的视图(前端代码不是我的)。如何在控制器上获取表单数据?下面的代码什么都不做(没有错误,没有值)。
控制器
[HttpPost]
public ActionResult ManagerAnalysis(FormCollection collection)
{
AnalysisModel model = new AnalysisModel();
model.analysisName = collection["analysisName"];
model.description = collection["description"];
model.dueDate = collection["dueDate"];
model.inviteInfo = collection["inviteInfo"];
model.manual = collection["manual"];
model.custom = collection["custom"];
return View();
}
视图
@{
ViewBag.Title = "Wizard";
}
<div class="row wrapper border-bottom white-bg page-heading">
<div class="col-lg-10">
<h2>Analysis Setup</h2>
</div>
<div class="col-lg-2">
</div>
</div>
<div class="wrapper wrapper-content animated fadeInRight">
<div class="row">
<div class="col-lg-12">
<div class="ibox">
<div class="ibox-title">
<h2>New</h2>
<div class="ibox-tools">
<a class="collapse-link">
<i class="fa fa-chevron-up"></i>
</a>
<a class="dropdown-toggle" data-toggle="dropdown" href="#">
<i class="fa fa-wrench"></i>
</a>
<ul class="dropdown-menu dropdown-user">
<li>
<a href="#">Config option 1</a>
</li>
<li>
<a href="#">Config option 2</a>
</li>
</ul>
<a class="close-link">
<i class="fa fa-times"></i>
</a>
</div>
</div>
<div class="ibox-content">
<h2>
General Information
</h2>
<p>
Get started by providing general details and inviting others to work with you
</p>
<form id="form" action="#" class="wizard-big">
<h1>Initial</h1>
<fieldset>
<h2>Basics</h2>
<div class="row">
<div class="col-lg-8">
<div class="form-group">
<label>Analysis Name *</label>
<input id="analysisName" name="analysis" type="text" class="form-control required" >
</div>
<div class="form-group">
<label>Description *</label>
<div> @Html.TextArea("name", new { @class = "form-control required" })</div>
</div>
<div class="form-group">
<label>Due date *</label>
<input id="date" name="date" type="date" class="form-control required">
</div>
</div>
<div class="col-lg-4">
<div class="text-center">
<div style="margin-top: 20px">
<i class="fa fa-sign-in" style="font-size: 180px;color: #e5e5e5 "></i>
</div>
</div>
</div>
</div>
</fieldset>
<h1>Collboration</h1>
<fieldset>
<h2>Invite Others</h2>
<p>Invite others to work with you by providing their email. We will get them set up in no time.</p>
<div class="row">
<div class="col-lg-6">
<div class="form-group">
<label>Manual* </label>
<p>Enter the number of employees whose information you will be entering manually (no invitation)</p>
<input id="manualNumber" name="number" type="number" class="form-control required" >
</div>
<div class="form-group">
<label>Invitation* </label>
<p>Enter the number of employees you will be inviting to enter their own data</p>
<input id="inviteNumber" name="number" type="number" class="form-control required">
</div>
</div>
</div>
</fieldset>
<h1>Invite</h1>
<fieldset>
<h2>Invite Your Employees</h2>
<p>Now enter the name and email of the employees you chose to invite in the pervious step</p>
<div class="row">
<div class="col-lg-6">
<div class="form-group">
<label>Name * </label>
<input name="employeeName" type="text" class="form-control required">
</div>
<div class="form-group">
<label>Email * </label>
<input name="email" type="email" class="form-control required email">
</div>
<div><button type="button" onClick="nextInvite" class="btn btn-success">Next Invite</button></div>
</div>
</div>
</fieldset>
<h1>Finish</h1>
<fieldset>
<div class="row">
<div class="col-lg-6">
<div>
<h3>Would you like to create custom forms for your employees?</h3>
<label>
Yes<input id="yes" type="checkbox" class="checkbox">
</label>
<label>
No
<input id="no" type="checkbox" class="checkbox"/>
</label>
</div>
</div>
</div>
</fieldset>
</form>
</div>
</div>
</div>
</div>
</div>
@section Styles {
@Styles.Render("~/Content/plugins/iCheck/iCheckStyles")
@Styles.Render("~/plugins/wizardStepsStyles")
}
@section Scripts {
@Scripts.Render("~/plugins/wizardSteps")
@Scripts.Render("~/plugins/iCheck")
@Scripts.Render("~/plugins/validate")
<script type="text/javascript">
$(document).ready(function () {
function nextInvite() {
var input1 = document.getElementById("employeeName");
var input2 = document.getElementById("email");
input1.value = " ";
input2.value = " ";
alert("shitttt!");
};
$("#wizard").steps();
$("#form").steps({
bodyTag: "fieldset",
onStepChanging: function (event, currentIndex, newIndex) {
// Always allow going backward even if the current step contains invalid fields!
if (currentIndex > newIndex) {
return true;
}
// Forbid suppressing "Warning" step if the user is to young
if (newIndex === 3 && Number($("#age").val()) < 18) {
return false;
}
var form = $(this);
// Clean up if user went backward before
if (currentIndex < newIndex) {
// To remove error styles
$(".body:eq(" + newIndex + ") label.error", form).remove();
$(".body:eq(" + newIndex + ") .error", form).removeClass("error");
}
// Disable validation on fields that are disabled or hidden.
form.validate().settings.ignore = ":disabled,:hidden";
// Start validation; Prevent going forward if false
return form.valid();
},
onFinishing: function (event, currentIndex) {
var form = $(this);
// Disable validation on fields that are disabled.
// At this point it's recommended to do an overall check (mean ignoring only disabled fields)
form.validate().settings.ignore = ":disabled";
// Start validation; Prevent form submission if false
return form.valid();
},
onFinished: function (event, currentIndex) {
var form = $(this);
// Submit form input
form.submit();
}
}).validate({
errorPlacement: function (error, element) {
element.before(error);
},
rules: {
confirm: {
equalTo: "#password"
}
}
});
});
</script>
}
视图更长,但剩下的是验证内容,我认为这与无关
和模型
namespace coach.Models
{
public class AnalysisModel
{
public string analysisName { get; set; }
public string description { get; set; }
public string dueDate { get; set; }
public string manual { get; set; }
public string inviteInfo { get; set; }
public string custom { get; set; }
}
}
我对编程并不陌生,所以如果你能给我一些资源,我可能会找到一些东西。谢谢
编辑
控制器中另一种实际被击中的方法:
//GET MangerView
public ActionResult ManagerAnalysis() {
Console.WriteLine("getting first controller");
return View();
}
您的form
缺少发布data
的action url
。因此操作应该具有值@Url.Action("ManagerAnalysis","ControllerName")
而不是"#"
如下所示:
<form id="form" action="@Url.Action("ManagerAnalysis","ControllerName")" class="wizard-big">
相关文章:
- 获取ASP.NET Ajax Timer状态
- 获取ASP.NET标签's在JavaScript中的文本或值
- 如何从c#代码中获取asp控件id
- 如何获取Asp:Repeater生成的行总数
- 尝试从ajax静态WebMethod获取asp.net(C#)文本框文本时出错
- 如何在javascript中获取asp文件上传的值
- 如何在右键单击时获取 ASP.NET GridView在jQuery中的单元格ID
- jQuery 获取 ASP.net 隐藏文本框值的值
- 使用 jquery 在数据列表中获取 asp:checkbox 的 id
- 如何检测和获取 asp.net 中的复选框/选择框(html 标记)值
- 如何在数组中获取 asp.net 中的值(或asp-classic会更好)
- 获取 ASP 页中从代码隐藏到 javascript 的文件路径
- 在外部javascript文件中获取asp控件ID的最佳方式是什么
- 如何在javascript中获取asp检查框的值
- 使用javascript获取asp:Label工具提示
- 如何使用Javascript获取asp:Table单元格值
- 获取ASP服务器端所选复选框值的数组
- 从客户端获取ASP下拉列表的ID
- 如何在c#中获取asp.net文本框内的静态web方法
- 获取asp的属性.javascript函数中的按钮函数