【发布时间】:2019-08-31 14:06:22
【问题描述】:
我是 MVC 的新手。我有一个数据表,可以从完整的日历中加载数据。在同一页面中,我也有 3 个文本框。文本框位于标题视图模型下,表格位于详细视图模型下。如何将表格和文本框中的这些值一起传递到控制器中。完全卡住了,没有想法,但有些方法,我尝试了一些代码。
我的看法:
@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; }
}
以前,我只使用一个视图模型将数据从视图发送到控制器。如,同一页面中两个不同视图模型中的详细信息和标题,我不知道如何将其发送到控制器,该怎么做?
【问题讨论】:
标签: javascript asp.net-mvc model-view-controller datatable viewmodel