MVC 4:使用 JQUERY 和 PartialViewResult 动态创建 HTML 文本框。如果动态添加代码,如何填充模型?
MVC 4: Create HTML textboxes dynamically with JQUERY and PartialViewResult. How to fill the model if the code is added dynamically?
我正在创建表单输入字段 JQUERY like
$('#students').live('change', function () {
var value = $(this).val();
if (value) {
$.ajax({
type: "GET",
timeout: 10000,
url: "@Url.Action(MVC.Company.ManageWorkReport.GetStudent())",
data: { studentId: value },
cache: false,
success: function (data) {
if (data) {
$("#students tbody").html(data);
}
},
error: function (xhr, status, error) {
alert(xhr.responseText);
}
});
}
return false;
});
HTML 插入数据的代码是
@using (Html.BeginDefaultForm(MVC.Company.ManageWorkReport.Create()))
{
<table class="table table-striped table-bordered bootstrap-datatable datatable" id="students">
<thead>
<tr>
<th>Ime in Priimek</th>
<th>Vrsta</th>
<th>Začetek dela</th>
<th>Konec dela</th>
<th>Enota</th>
<th>Cena za enoto</th>
<th>Količina</th>
<th>Neto znesek</th>
<th>Bruto znesek</th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<td colspan="11">Podatek še ne obstaja</td>
</tr>
</tbody>
</table>
@Html.SimpleSubmitAndCancelButton(Translations.Global.SAVE, Translations.Global.CANCEL)
}
和 C#
[HttpGet]
public virtual PartialViewResult GetStudent(int studentId)
{
StudentsWorksReportsFormModel studentsWorksReportsFormModel = new StudentsWorksReportsFormModel();
.....
var view = PartialView("StudentWorkReportResult", studentsWorksReportsFormModel);
return view;
}
问题是,当我在表单中输入数据并单击“提交”按钮时,模型始终为空。如果我用 JQUERY 填充页面然后在文本字段中输入数据,为什么模型是空的?如何填充模型,我可以在数据库中插入数据。
为了让模型绑定器为您挑选新值,您需要设置控件的名称 属性。
每个项目都需要用 属性 名称进行索引和设置。
例如,如果您的 collection 是 MyCollection
要在第一项上设置 Name
属性,您将添加以下内容:
Name="MyCollection[0].Name"
第二等:
Name="MyCollection[1].Name"
The Features and Foibles of ASP.NET MVC Model Binding
如果要返回局部视图,则需要遍历 collection,如下所示:
@for(i = 0; i < Model.StudentWorkReportFormModel.Count; i++)
{
@Html.TextBoxFor(modelItem => Model.StudentWorkReportFormModel[i].StartDate})
}
我正在创建表单输入字段 JQUERY like
$('#students').live('change', function () {
var value = $(this).val();
if (value) {
$.ajax({
type: "GET",
timeout: 10000,
url: "@Url.Action(MVC.Company.ManageWorkReport.GetStudent())",
data: { studentId: value },
cache: false,
success: function (data) {
if (data) {
$("#students tbody").html(data);
}
},
error: function (xhr, status, error) {
alert(xhr.responseText);
}
});
}
return false;
});
HTML 插入数据的代码是
@using (Html.BeginDefaultForm(MVC.Company.ManageWorkReport.Create()))
{
<table class="table table-striped table-bordered bootstrap-datatable datatable" id="students">
<thead>
<tr>
<th>Ime in Priimek</th>
<th>Vrsta</th>
<th>Začetek dela</th>
<th>Konec dela</th>
<th>Enota</th>
<th>Cena za enoto</th>
<th>Količina</th>
<th>Neto znesek</th>
<th>Bruto znesek</th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<td colspan="11">Podatek še ne obstaja</td>
</tr>
</tbody>
</table>
@Html.SimpleSubmitAndCancelButton(Translations.Global.SAVE, Translations.Global.CANCEL)
}
和 C#
[HttpGet]
public virtual PartialViewResult GetStudent(int studentId)
{
StudentsWorksReportsFormModel studentsWorksReportsFormModel = new StudentsWorksReportsFormModel();
.....
var view = PartialView("StudentWorkReportResult", studentsWorksReportsFormModel);
return view;
}
问题是,当我在表单中输入数据并单击“提交”按钮时,模型始终为空。如果我用 JQUERY 填充页面然后在文本字段中输入数据,为什么模型是空的?如何填充模型,我可以在数据库中插入数据。
为了让模型绑定器为您挑选新值,您需要设置控件的名称 属性。
每个项目都需要用 属性 名称进行索引和设置。
例如,如果您的 collection 是 MyCollection
要在第一项上设置 Name
属性,您将添加以下内容:
Name="MyCollection[0].Name"
第二等:
Name="MyCollection[1].Name"
The Features and Foibles of ASP.NET MVC Model Binding
如果要返回局部视图,则需要遍历 collection,如下所示:
@for(i = 0; i < Model.StudentWorkReportFormModel.Count; i++)
{
@Html.TextBoxFor(modelItem => Model.StudentWorkReportFormModel[i].StartDate})
}