Javascript 如何从视图向控制器发送文本框和数据表值?
我是MVC新手。我有一个datatable,它从完整的日历加载数据。在同一页中,我也有3个文本框。文本框位于标题视图模型下,表格位于详图视图模型下。如何将这些值从表和文本框一起传递到控制器。完全卡住了,没有IDE,但我尝试了一些代码 我的观点:Javascript 如何从视图向控制器发送文本框和数据表值?,javascript,asp.net-mvc,model-view-controller,datatable,viewmodel,Javascript,Asp.net Mvc,Model View Controller,Datatable,Viewmodel,我是MVC新手。我有一个datatable,它从完整的日历加载数据。在同一页中,我也有3个文本框。文本框位于标题视图模型下,表格位于详图视图模型下。如何将这些值从表和文本框一起传递到控制器。完全卡住了,没有IDE,但我尝试了一些代码 我的观点: @model IList<Model.ViewModel.DetailVM> ... @using (Html.BeginForm("Create", "Master", FormMethod.Post, new { @enctype = "
@model IList<Model.ViewModel.DetailVM>
...
@using (Html.BeginForm("Create", "Master", FormMethod.Post, new { @enctype = "multipart/form-data" }))
{
<div class="col-lg-3">
<label>Description</label>
@Html.TextBox("Description", null, new { @class = "form-control", @placeholder = "Enter Description" })
</div>
<div class="col-lg-3">
<label>Code</label>
@Html.TextBox("Code", null, new { @class = "form-control", @placeholder = "Enter Code" })
</div>
<div class="col-lg-3 text-center">
<br />
<input id="chkBoxIsActive" name="chkBoxIsActive " type="checkbox" />
<label>Is Active</label>
</div>
<table cellspacing="1" cellpadding="2" style="width: 100%;" id="CalendarSelection" class="table table-striped table-hover table-bordered table-hd">
<thead>
<tr class="gridheader">
<td valign="middle" align="center" style="width: 2%;"></td>
<td style="width: 50%;">Calendar Date</td>
<td style="width: 50%;">Description</td>
</tr>
</thead>
<tbody>
<tr>
<td></td>
<td></td>
<td></td>
</tr>
</tbody>
</table>
<div class="btn-group">
<button type="submit" id="btnSave" onclick="Save();" name="btnSave" class="btn btn-block btn-success btn-flat"><span class="hide-on-mobile">Save</span> <i class="fa fa-save"></i></button>
</div>
}
<script>
function Save() {
var data = ('#CalendarSelection').DataTable().$('input,select,textarea').serialize();
$.ajax({
url: '/Master/Create/',
data: data,
success: function () {
alert('success');
},
error: function () {
alert('failure');
}
});
}
</script>
**Master Controller**
[HttpGet]
public ActionResult Create()
{
return View();
}
[HttpPost]
public ActionResult Create(string[] data)
{
return View();
}
public class CalenderVM
{
public string Id { get; set; }
public string CalenderYear { get; set; }
public string Description { get; set; }
public string Code { get; set; }
public bool IsActive { get; set; }
}
public class DetailVM
{
public DateTime [] CalenderDate { get; set; }
public string[] Description { get; set; }
}
以前,我单独使用一个viewmodel将数据从视图发送到控制器。由于同一页面中的两个不同视图模型中的detail和header,我不知道如何将其发送给controller,如何执行?问题是您没有在ajax请求中使用type,因此在调用ajax时,httpGet方法将运行 您需要在网络中发送ajax响应