.net Net mvc4 form2在form1在同一页面上发布成功地将数据返回到同一模型后,以空模型提交
我用的是一个表单,里面有两个表单帖子。第一个表单(“frmTemps”)成功地检索数据并在表单上显示。我可以向您保证,通过调试,返回视图显示数据时,数据位于模型中 数据返回后,在同一视图上显示另一个按钮,但第二个表单(“frmProcess”)用于对从第一个表单返回的选定数据进行处理(“frmTemps”)。我知道在sme视图上可以有多个表单。这都是MVC提供给您的控制的一部分 问题是,当我发布第二个表单(“frmProcess”)并且在该操作结果中立即设置了断点,并且检查模型的内容时,我发现模型中的所有项都是null、零或false。通过对模型对象进行调试.net Net mvc4 form2在form1在同一页面上发布成功地将数据返回到同一模型后,以空模型提交,.net,asp.net-mvc-4,.net,Asp.net Mvc 4,我用的是一个表单,里面有两个表单帖子。第一个表单(“frmTemps”)成功地检索数据并在表单上显示。我可以向您保证,通过调试,返回视图显示数据时,数据位于模型中 数据返回后,在同一视图上显示另一个按钮,但第二个表单(“frmProcess”)用于对从第一个表单返回的选定数据进行处理(“frmTemps”)。我知道在sme视图上可以有多个表单。这都是MVC提供给您的控制的一部分 问题是,当我发布第二个表单(“frmProcess”)并且在该操作结果中立即设置了断点,并且检查模型的内容时,我发现模
[HttpPost]
public ActionResult GeneratePDF(ViewModelTemplate_Guarantors model) {
我在表格2中使用的模型与我在表格1中使用的模型相同
我想大家都知道,在表单上填写数据并提交之后,让您的模型类型在action方法中接收数据,这是做事情的标准方式
下面是我的ViewModel
public partial class ViewModelTemplate_Guarantors
{
public int SelectedTemplateId { get; set; }
public IEnumerable<PDFTemplate> Templates { get; set; }
public int SelectedGuarantorId { get; set; }
public IEnumerable<tGuarantor> Guarantors { get; set; }
public string LoanId { get; set; }
public string SelectedDeptText { get; set; }
public string SelectedDeptValue { get; set; }
public string LoanType { get; set; }
public bool ShowTemps { get; set; }
public string Error { get; set; }
public string ErrorT { get; set; }
public string ErrorG { get; set; }
public bool ShowGeneratePDFBtn { get; set; }
}
公共部分类ViewModelTemplate\u担保人
{
public int SelectedTemplateId{get;set;}
公共IEnumerable模板{get;set;}
public int SelectedGuarantorId{get;set;}
公共IEnumerable担保人{get;set;}
公共字符串LoanId{get;set;}
公共字符串SelectedDeptText{get;set;}
公共字符串SelectedDeptValue{get;set;}
公共字符串LoanType{get;set;}
公共bool ShowTemps{get;set;}
公共字符串错误{get;set;}
公共字符串错误{get;set;}
公共字符串错误{get;set;}
公共bool ShowGeneratePDFBtn{get;set;}
}
以下是我的看法:
@model PDFConverterModel.ViewModels.ViewModelTemplate_Guarantors
@{
ViewBag.Title = "BHG :: PDF Generator";
}
<h2>@ViewBag.Message</h2>
<div>
<table style="width: 1000px">
<tr>
<td colspan="5">
<img alt="BHG Logo" src="~/Images/logo.gif" />
</td>
</tr>
@using (Html.BeginForm("Refresh", "Home", FormMethod.Post, new { id = "frmTemps" }))
{
<tr>
<td>
@*@(Html.Kendo().NumericTextBox<int>()
.Name("txtLoanID")
.Placeholder("Enter numeric value")
)*@
@Html.LabelFor(model => model.LoanId)
@Html.TextBoxFor(model => model.LoanId)
@Html.ValidationMessageFor(model => model.LoanId)
<td colspan="3">
<input type="submit" id="btnRefresh" value='Refresh' />
</td>
</tr>
<tr>
<td>@Html.LabelFor(model => model.LoanType)
@Html.TextBox("SBA", "SBA")
@Html.ValidationMessageFor(model => model.LoanType)
@*@Html.TextBoxFor(model => model.LoanType)*@
</td>
<td>
<label for="ddlDept">Department:</label>
@(Html.Kendo().DropDownListFor(model => model.SelectedDeptText)
.Name("ddlDept")
.DataTextField("DepartmentName")
.DataValueField("DepartmentID")
.Events(e => e.Change("Refresh"))
.DataSource(source =>
{
source.Read(read =>
{
read.Action("GetDepartments", "Home");
});
})
)
@Html.ValidationMessageFor(model => model.SelectedDeptText)
</td>
</tr>
}
@using (Html.BeginForm("GeneratePDF", "Home", FormMethod.Post, new { id = "frmProcess" }))
{
if (Model.ShowGeneratePDFBtn == true)
{
if (Model.ErrorT != string.Empty)
{
<tr>
<td colspan="5">
<u><b>@Html.Label("Templates:")</b></u>
</td>
</tr>
<tr>
@foreach (var item in Model.Templates)
{
<td>
@Html.CheckBoxFor(model => item.IsChecked)
@Html.DisplayFor(model => item.TemplateName)
</td>
}
</tr>
}
else
{
Model.Error = Model.ErrorT;
}
if (Model.ErrorG != string.Empty)
{
<tr>
<td colspan="5">
<u><b>@Html.Label("Guarantors:")</b></u>
</td>
</tr>
<tr>
@foreach (var item in Model.Guarantors)
{
<td>
@Html.CheckBoxFor(model => item.isChecked)
@Html.DisplayFor(model => item.GuarantorFirstName) @Html.DisplayFor(model => item.GuarantorLastName)
</td>
}
</tr>
}
else
{
Model.Error = Model.ErrorG;
}
<tr>
<td>
<input type="submit" id="btnGeneratePDF" value='Generate PDF' />
</td>
</tr>
<tr>
<td colspan="5">
@Model.Error
</td>
</tr>
}
}
</table>
</div>
<script type="text/javascript">
$('btnRefresh').on('click', '#btnRefresh', function () {
Refresh();
});
function Refresh() {
var LoanID = $("#LoanID").val();
if (LoanID != "") {
document.forms["frmTemps"].submit();
}
}
</script>
@model PDFConverterModel.ViewModels.viewmodel模板\u担保人
@{
ViewBag.Title=“BHG::PDF生成器”;
}
@查看包。留言
@使用(Html.BeginForm(“刷新”、“主页”、FormMethod.Post、新的{id=“frmTemps”}))
{
@*@(Html.Kendo().NumericTextBox())
.名称(“txtLoanID”)
.占位符(“输入数值”)
)*@
@LabelFor(model=>model.LoanId)
@Html.TextBoxFor(model=>model.LoanId)
@Html.ValidationMessageFor(model=>model.LoanId)
@LabelFor(model=>model.LoanType)
@文本框(“SBA”、“SBA”)
@Html.ValidationMessageFor(model=>model.LoanType)
@*@Html.TextBoxFor(model=>model.LoanType)*@
部门:
@(Html.Kendo().DropDownListFor(model=>model.SelectedDeptText)
.名称(“部门”)
.DataTextField(“部门名称”)
.DataValueField(“部门ID”)
.事件(e=>e.Change(“刷新”))
.DataSource(source=>
{
source.Read(Read=>
{
阅读。行动(“获取部门”、“主页”);
});
})
)
@Html.ValidationMessageFor(model=>model.SelectedDeptText)
}
@使用(Html.BeginForm(“GeneratePDF”、“Home”、FormMethod.Post、new{id=“frmProcess”}))
{
如果(Model.ShowGeneratePDFBtn==true)
{
if(Model.error!=string.Empty)
{
@Label(“模板:”)
@foreach(Model.Templates中的var项)
{
@Html.CheckBoxFor(model=>item.IsChecked)
@DisplayFor(model=>item.TemplateName)
}
}
其他的
{
Model.Error=Model.Error;
}
if(Model.error!=string.Empty)
{
@Html.Label(“担保人:”)
@foreach(模型担保人中的var项目)
{
@Html.CheckBoxFor(model=>item.isChecked)
@Html.DisplayFor(model=>item.GuarantorFirstName)@Html.DisplayFor(model=>item.GuarantorLastName)
}
}
其他的
{
Model.Error=Model.Error;
}
@模型误差
}
}
$('btnRefresh')。在('click','#btnRefresh',函数(){
刷新();
});
函数刷新(){
var LoanID=$(“#LoanID”).val();
如果(LoanID!=“”){
文件。表格[“frmTemps”]。提交();
}
}
以下是我在控制器中的相关部分:
[HttpPost]
public ActionResult GeneratePDF(ViewModelTemplate_Guarantors **model**)
{
try
{
int FolderNo, GuarantorNum = 0;
string Folder, LoanFolder = String.Empty;
string FormId, FormName, GuarantorName = String.Empty;
int LoanId = Convert.ToInt32(model.LoanId);
LoanFolder = LoanId.ToString().PadLeft(8, '0');
//To calculate FolderId based on LoanId
if ((LoanId > 0) && (LoanId < 99000))
{
FolderNo = ((int)(LoanId / 10000) * 10000);
}
else
{
FolderNo = ((int)(LoanId / 1000) * 1000);
}
Folder = ((int)FolderNo).ToString();
Folder = Folder.PadLeft(8, '0');
[HttpPost]
公共行动结果生成PDF(视图模型模板\担保人**模型**)
{
尝试
{
int FolderNo,GuarantorNum=0;
string文件夹,LoanFolder=string.Empty;
斯特里
@Html.HiddenFor(m => m.LoanId)