【问题标题】:Can not pass model from partial view to controller?不能将模型从局部视图传递给控制器​​?
【发布时间】:2016-09-09 10:29:50
【问题描述】:

我有一个如下所述的视图:

@model Hello.Web.Models.ClaimDataViewModel
@{
    ViewBag.Title = "Index";
}

 @using (Ajax.BeginForm("Index", "Search", new AjaxOptions { UpdateTargetId = "divWebGrid", InsertionMode = InsertionMode.Replace, LoadingElementId = "imgLoad" }))
{ 

//some code

}
@if (Model != null && Model.ClaimDataViewModelList != null)
{
    <div class="panel panel-default" id="divWebGrid">
        <div class="panel-heading panel-heading-sm" style="padding:5px 0px 0px 5px;" >
            <h5 class="panel-title"><b>Search Results:</b></h5>
        </div>
        <div class="panel-body">
            @{Html.RenderPartial("_Index", Model.ClaimDataViewModelList);}
        </div>
    </div>
}

然后我有一个局部视图:

    @model IEnumerable<Hello.Web.Models.ClaimDataViewModel>

@if (Model.Count() > 0)
{

    <table id="tblSearchRecords" class="table table-hover table-condensed">
        <thead>
            <tr>                
                <th style="visibility:hidden;"></th>
                <th>Claim#</th>
                <th>Status</th>
                <th>Due Date</th>
                <th>Claimant</th>                
                <th>Insured Name</th>
                <th>Claim Handler</th>
                <th style="text-align:center;">Full Value</th>
                <th style="text-align:center;">Opt Out</th>
                <th style="text-align:center;">Reassign</th>
                <th></th>
            </tr>
        </thead>
        <tbody>
            @for (int i = 0; i < Model.Count(); i++ )
            {
                <tr>                    
                    <td>@Html.RadioButton("rbCheck", new { ClaimDataId = Model.ElementAt(i).ClaimDataId })</td>
                    <td>@Html.ActionLink(Model.ElementAt(i).ClaimNum + "-" + Model.ElementAt(i).LineSuffix, "")</td>
                    <td>                        
                        @if (Model.ElementAt(i).MRPLineStatus == "PENDG")
                        { <p>Pending</p> }
                        else if (Model.ElementAt(i).MRPLineStatus == "INPRG")
                        { <p>In Progress</p> }
                        else if (Model.ElementAt(i).MRPLineStatus == "COMPL")
                        { <p>Complete</p> }
                        else if (Model.ElementAt(i).MRPLineStatus == "OPOUT")
                        { <p>Opt Out</p> }
                    </td>
                    <td>@Html.DisplayFor(m => m.ElementAt(i).DueDate)</td>
                    <td>@Html.DisplayFor(m => m.ElementAt(i).ClaimantName)</td>                    
                    <td>@Html.DisplayFor(m => m.ElementAt(i).InsuredName)</td>
                    <td>@Html.DisplayFor(m => m.ElementAt(i).ClaimHandlerName)</td>
                    <td style="text-align:right;">@Html.DisplayFor(m => m.ElementAt(i).FullValueAmt)</td>
                    <td style="text-align:center;">@Html.CheckBoxFor(m => m.ElementAt(i).IsOptOut)</td>
                    <td style="text-align:center;">@Html.CheckBoxFor(m => m.ElementAt(i).IsReassigned)</td>
                    <td>
                        @if (Model.ElementAt(i).DueDate < DateTime.Now)
                        {
                        <span class="glyphicon glyphicon-alert" style="color:red"></span>
                        }
                    </td>
                </tr>
            }
        </tbody>
    </table>

    <hr />
    <div class="row">
        <div class="col-sm-3">
            <div class="input-group input-group-sm" style="max-width: 250px;">
                <span class="input-group-addon">Reassign To:</span>
                @Html.DropDownList("ddlReassignTo", (IEnumerable<SelectListItem>)ViewBag.ReviewerList, new { @class = "form-control" })
            </div>
        </div>
        <div class="col-sm-5">
            <div class="input-group input-group-sm" style="width: 200%;">
                <span class="input-group-addon">Opt Out Reason:</span>
                <input type="text" class="form-control" />
            </div>
        </div>        
    </div>

    <div class="row">
                <div class="col-sm-offset-10 col-sm-1">
            @*<button type="submit" class="btn btn-sm btn-primary pull-right"><span class="glyphicon glyphicon-floppy-save">&nbsp</span>Save</button>*@

            @Html.ActionLink("Save", "SaveUpdatedClaims", "Search", new { }, new { @id = "btnTEST", @class = "btn btn-sm btn-primary pull-right" })
        </div>
        <div class="col-sm-1">
            @*<button type="submit" class="btn btn-sm btn-primary" id="btnOpenMr"><span class="glyphicon glyphicon-eye-open">&nbsp</span>Open MR</button>*@

            @Html.ActionLink("Open MR", "RedirectToReview", "Search", new { }, new { @id = "btnOpenMr", @class = "btn btn-sm btn-primary pull-right" })
        </div>
    </div>

}
else
{
    <div class="alert alert-warning"><b>No records found !</b></div>
}

现在,当我单击局部视图上的“保存”按钮时,我的操作方法模型中的值为空。

谁能告诉我这里出了什么问题?

更新:
下面是动作方法:

    [HttpPost]
    public ActionResult SaveUpdatedClaims(IEnumerable<ClaimDataViewModel> model)
    {
        return View();
    }

【问题讨论】:

  • 您没有“保存”按钮。只是一个重定向到 GET 方法的链接。你甚至没有表格。如果你两者都有,它仍然无法工作,因为你不能使用.ElementAt() 来生成你的表单控件
  • 用表单标签装饰你的代码
  • @StephenMuecke:我使用了一个按钮来提交。我什至用“表单”包裹了部分视图。此外,我正在使用 foreach 循环进行迭代。但没有任何效果。
  • 因为你不能使用foreach 循环。而且你不能使用.ElementAt()。参考this answer。
  • 成功了!!!....现在我正在使用 IList。谢谢@StephenMuecke

标签: asp.net-mvc razor


【解决方案1】:

因为你的模型是 list type @model IEnumerable&lt;Hello.Web.Models.ClaimDataViewModel&gt; 而你是 accepting object type Hello.Web.Models.ClaimDataViewModel 在行动中。

还有form标签丢失

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-01
    • 2013-12-20
    • 1970-01-01
    • 2014-06-28
    • 1970-01-01
    相关资源
    最近更新 更多