2

我在一个视图上有两个表单,在一个控制器中执行两个单独的操作方法。

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

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

提交第二个表单时,我意识到除非我将它们放在隐藏字段中(因为它们在单独的表单中),否则将无法使用 loanid 和 ddl --- 这很好。我很难理解的是,当我提交第二个表单(frmProcess)时,为什么模型视图活页夹不从表单中获取数据,将其放入模型中并将其提交给我的 GeneratePDF 操作方法。?

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

请帮忙,因为我在工作中完全被困在这个问题上,他们依赖于我。我只是不明白为什么模型绑定器不获取表单上的值并将其提交给操作方法进行处理。看来我遗漏了一些东西以允许数据在提交时返回模型。

以下是我的相关代码: ViedwModel

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>

控制器

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

视图的相关部分:

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>  

控制器:

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

基本上,它再次工作正常。当表单使用 Generate 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> 
                }
4

1 回答 1

8

正如我在评论中提到的。模型绑定器无法绑定到 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; } 

    ...
}

此外,您的视图应如下所示:

...

@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> 

编辑器模板将自动迭代集合,并说明正确的命名格式,以使模型绑定器理解它是一个集合。

于 2012-10-07T19:29:21.237 回答