gpt4 book ai didi

asp.net-mvc - 当我尝试提交表单时,MVC 4 模型为空

转载 作者:行者123 更新时间:2023-12-01 09:33:08 26 4
gpt4 key购买 nike

我在一个 View 上有两个表单,在一个 Controller 中执行两个单独的 Action 方法。

第一个表单 (frmRefresh) 负责获取数据并将其显示在表单上,​​以便用户可以选择某些复选框。提交后,数据会在 ViewModel 中正常返回并正确显示在表单上。模板的 11 条记录和担保人的 3 条记录在表单上显示为复选框。

第二个表单 (frmProcess) 负责获取表单上的数据(从上面的第一篇文章返回)。用户在屏幕上进行选择并根据 Controller 中的某些逻辑对其进行处理。我在模型中有 List 对象,并且由于对象复杂,我认为我不能使用 FormCollection 来处理数据。基本上,它们是复选框的集合。我真的需要使用应该在模型中提交的数据,因为在 Controller 中对该数据进行了处理。

提交第二个表单时,我意识到除非我将它们放在隐藏字段中(因为它们位于单独的表单中),否则 loanid 和 ddl 将不可用——没关系。我很难理解的是,当我提交第二个表单(frmProcess)时,为什么模型 View 绑定(bind)器不从表单中获取数据,将其放入模型并将其提交给我的 GeneratePDF 操作方法。?

第一,我真的需要一些帮助来理解为什么会发生这种情况;第二,我真的需要一个解决方案,将我的模型数据从表单获取到操作方法并对其进行处理。正如您在 Controller 中看到的那样,在代码的末尾,我在 ViewModel 中枚举模板来处理数据。

请帮忙,因为我在工作中完全坚持这一点,而他们为此依赖我。我只是不明白为什么模型联编程序不采用表单上的值并将其提交给操作方法进行处理。看来我遗漏了一些东西,无法让数据在提交后返回到模型中。

下面是我的相关代码: View 模型

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; }
}

查看

@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 = "frmRefresh" })) { <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)
</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>
<tr>
<td colspan="3">
<input type="submit" id="btnRefresh" value='Refresh' />
</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)&nbsp;@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>

Controller

    public ActionResult Index(ViewModelTemplate_Guarantors model)


{
ViewBag.Error = "";
model.ShowGeneratePDFBtn = false;
return View("Index", model);
}


// used for the first form "frmRefresh" [HttpPost] public ActionResult Refresh(ViewModelTemplate_Guarantors model) {
try
{
model.Error = string.Empty;

bool dbHasRows = db.ChkLoanFields(Convert.ToInt32(model.LoanId));

if (!dbHasRows)
{
model.ShowGeneratePDFBtn = false;
model.Error = "Details not available for this LoanId.";
return View("Index",model);
}
else
{
int TemplateCnt = 0;
int GuarantorCnt = 0;
//todo - modify 2nd & 3rd parms instead of hardcoding
ViewModelTemplate_Guarantors tg = db.SelectViewModelTemplate_Guarantors(Convert.ToInt32(model.LoanId), "All", "All", out TemplateCnt, out GuarantorCnt);

if (TemplateCnt > 0)
model.Templates = tg.Templates;
else
model.ErrorT = "Templates not available for this LoanType.";

if (GuarantorCnt > 0)
model.Guarantors = tg.Guarantors;
else
model.ErrorG = "Guarantors not available for this LoanId.";

model.ShowGeneratePDFBtn = true;
// right before the return here, the model is full of data. return View("Index", model); }
}
catch (Exception ex)
{
throw ex;
}
} [HttpPost] // when I check the data here (via submission from the "frmProcess" form, the model is completely empty, null, etc... WHY???? // i NEED the model data here to perform processing in this action method. 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');

//todo - 2nd parm SelectedValue of dept
List<sSRPTFundexDocCodes1_Test_Result> sSRPTFundexDocCodes1 = db.GetFormValues(Convert.ToInt32(model.LoanId), (model.SelectedDeptValue));

if (sSRPTFundexDocCodes1 != null)
{
foreach (PDFTemplate template in model.Templates) {
if (template.IsChecked == true) {

发布后模板名称未显示在模型中。这工作正常......值(复选框和相应的名称显示在表单上。

但是,当发布 GeneratePDF 按钮时,我在模型中看到的只是复选框是否被选中(这很好)。在尝试了以下许多语句之后:(ValueFor、DisplayFor、LabelFor、EditorFor 等),模板名称返回的值为空白。我需要与复选框相对应的模板名称。

@Html.ValueFor(model => Model.Templates[i].TemplateName)

我怎样才能做到这一点?提前谢谢...下面是我更新的代码。

ViewModel public partial class ViewModelTemplate_Guarantors 
{
public ViewModelTemplate_Guarantors()
{
Templates = new List<PDFTemplate>();
Guarantors = new List<tGuarantor>();
}

public int SelectedTemplateId { get; set; }
public List<PDFTemplate> Templates { get; set; }

public int SelectedGuarantorId { get; set; }
public List<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 string Error { get; set; }
public string ErrorT { get; set; }
public string ErrorG { get; set; }
public bool ShowGeneratePDFBtn { get; set; }
}

View 的相关部分:

if (Model.ShowGeneratePDFBtn == true) 
{
if (Model.ErrorT == string.Empty)
{
<tr>
<td colspan="5">
<u><b>@Html.Label("Templates:")</b></u>
</td>
</tr>
<tr>
@for (int i = 0; i < Model.Templates.Count; i++)
{
<td>
@Html.CheckBoxFor(model => Model.Templates[i].IsChecked)
@Html.ValueFor(model => Model.Templates[i].TemplateName) </td>
}

</tr>
}
else
{
<tr>
<td>
<b>@Html.DisplayFor(model => Model.ErrorT)</b>
</td>
</tr>
}

if (Model.ErrorG == string.Empty)
{
<tr>
<td colspan="5">
<u><b>@Html.Label("Guarantors:")</b></u>
</td>
</tr>
<tr>
@for (int i = 0; i < Model.Guarantors.Count; i++)
{
<td>
@Html.CheckBoxFor(model => Model.Guarantors[i].isChecked)
@Html.ValueFor(model => Model.Guarantors[i].GuarantorFirstName)&nbsp;@Html.ValueFor(model => Model.Guarantors[i].GuarantorLastName) </td>
}

</tr>
}
else
{
<tr>
<td>
<b>@Html.DisplayFor(model => Model.ErrorG)</b>
</td>
</tr>
}
}
<tr>
<td colspan="3">
<input type="submit" name="submitbutton" id="btnRefresh" value='Refresh' />
</td>
@if (Model.ShowGeneratePDFBtn == true)
{
<td>
<input type="submit" name="submitbutton" id="btnGeneratePDF" value='Generate PDF' />
</td>
}
</tr>
<tr>
<td colspan="5">
@Model.Error
</td>
</tr>

Controller :

public ActionResult ProcessForm(string submitbutton, ViewModelTemplate_Guarantors model, FormCollection collection)

基本上,它再次运行良好。当表单使用生成 PDF 按钮发布时,我得到每个复选框的选中值,但不是模型中模板的名称。

我是不是漏掉了什么???

我提交之前的表格基本上是这样的。这是我在进入 ActionResult 后在我的模型中作为 TemplateID 缺少的复选框 (Form4) 的名称。

public ActionResult ProcessForm(string submitbutton, ViewModelTemplate_Guarantors model, FormCollection collection)

复选框(选中)Form4

@for (int i = 0; i < Model.Templates.Count; i++)
{
<td>
@Html.CheckBoxFor(model => Model.Templates[i].IsChecked)
@Html.DisplayFor(model => Model.Templates[i].TemplateName)
</td>
}

最佳答案

正如我在评论中提到的。模型绑定(bind)器无法绑定(bind)到 IEnumerable。

您的模型应如下所示:

public partial class ViewModelTemplate_Guarantors 
{
public ViewModelTemplate_Guarantors() {
Templates = new List<PDFTemplate>(); // These are important, the model binder
Guarantors = new List<tGuarantor>(); // will not instantiate nested classes
}
public int SelectedTemplateId { get; set; }
public List<PDFTemplate> Templates { get; set; }

public int SelectedGuarantorId { get; set; }
public List<tGuarantor> Guarantors { get; set; }

...
}

此外,您的 View 应如下所示:

...

@for(int i = 0; i < Model.Templates.Count; i++) // should really use label, not display
{
<td>
@Html.CheckBoxFor(model => Model.Templates[i].IsChecked)
@Html.DisplayFor(model => Model.Templates[i].TemplateName)
</td>
}

...

@for(int i = 0; i < Model.Guarantors.Count; i++)
{
<td>
@Html.CheckBoxFor(model => Model.Guarantors[i].isChecked)
@Html.DisplayFor(model => Model.Gurantors[i].GuarantorFirstName)&nbsp;@Html.DisplayFor(model => Model.Guarantors[i].GuarantorLastName)
</td>
}

...

虽然更好的选择是使用 EditorTemplate 而不是这样做:

...
@Html.EditorFor(m => m.Templates)
...
@Html.EditorFor(m => m.Guarantors)
...

然后在 ~/Views/Shared 中创建一个名为 EditorTemplates 的文件夹,然后创建两个名为 Templates.cshtmlGuarantors.cshtml 的文件。

在这些文件中你会这样做:

@model PDFTemplate
<td>
@Html.CheckBoxFor(model => model.IsChecked)
@Html.DisplayFor(model => model.TemplateName)
</td>

@model Guarantors
<td>
@Html.CheckBoxFor(model => model.isChecked)
@Html.DisplayFor(model => model.GuarantorFirstName)&nbsp;@Html.DisplayFor(model => model.GuarantorLastName)
</td>

编辑器模板将自动遍历集合,并考虑正确的命名格式,以使模型绑定(bind)器理解它是一个集合。

关于asp.net-mvc - 当我尝试提交表单时,MVC 4 模型为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12770785/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com