MVC - 查看不将模型列表值传递回控制器

时间:2016-02-05 11:48:17

标签: c# visual-studio model-view-controller razor mvvm

我在从mvc的简单视图中将值发送回控制器时遇到问题。

在视图中,我有一个简单的表,可以在用户选择下拉列表值时填充。以下是该视图的摘录:

@model IList<RFQFreight.Models.TargetTransitTotal>

@using (Ajax.BeginForm("InputTimev2", "Target",
            new AjaxOptions
            {UpdateTargetId = "result", HttpMethod = "POST",
                InsertionMode = InsertionMode.Replace}))
{

<table class="tblTarTime">
            <thead>
                <tr><td>Select RFQ</td></tr>
            </thead>
            <tr>
                <td>
                    <select id="ddlTarTimRFQs" name="ddlTarTimRFQs">
                        @{
                            string getRfqUrlId =         HttpContext.Current.Request.QueryString["rfqid"];
                            if (getRfqUrlId == null){getRfqUrlId = "";}
                            List<string> rfqLst = ViewBag.rfqsLst;
                            rfqLst.Sort();
                       }
                        <option value=""></option>  
                        @foreach (var item in ViewBag.rfqsLst)
                        {
                            if (getRfqUrlId == item)
                            {<option value="@item" selected>@item</option>}
                            else{<option value="@item">@item</option>}
                        }
                    </select>
                </td>
            </tr>
        </table>

<table id="tblTarTime" class="tblTarTime">
            @if (ViewBag.rfqSelected == true)
        {

                <thead>
                    <tr>
                        <td>Route</td>
                        <td>Transit Target Time</td>
                    </tr>
                </thead>
                foreach (var item in Model)
                {
                    <tr>                               
                        @Html.HiddenFor(model => item.rfqId)                         
                    </tr>
                    <tr>
                        <td>
                            @Html.DisplayFor(model => item.serviceId)
                            @Html.HiddenFor(model => item.serviceId)
                        </td>
                        <td>
                            @Html.TextBoxFor(model => item.amount)
                            @Html.HiddenFor(model => item.amount)
                            @*<input type="text" value="@item.amount" />*@
                        </td>
                    </tr>
                }
                <tr><td colspan="2"></td></tr>
                <tr>
                    <td colspan="2">
                        @*<a href="#" class="linkSubmit" id="save">Save</a>*@
                        <input type="submit" value="Save" id="saveTarget" onclick="checkInputsTime(event)" />
                    </td>
                </tr>
            }
            else
            {
                <tr><th width="400">No Routes Available</th></tr>
            }
        </table>

以下是[Get]控制器功能的摘录:

public ActionResult InputTimev2(string rfqid = "")
    {
if (rfqid == "")
{
}else{
//page load after rfqid dropdown selection  
            var getLstTargetTransit = new List<TargetTransitTotal>();
            getLstTargetTransit = (from targetRows in db.TargetTransitTotals
                                    join rfqs in db.RFQMasters on      targetRows.rfqId equals rfqs.RfqId
                                    where rfqs.Status != "COMPLETED"   
                                    select targetRows).ToList();
            dynamic rfqLst = new List<string>();
            rfqLst = (from rfq in getLstTargetTransit orderby rfq.rfqId descending select rfq.rfqId).Distinct().ToList();
            ViewBag.rfqsLst = rfqLst;
            ViewBag.rfqSelected = true;

            var lstTargetTransit = new List<TargetTransitTotal>();
            lstTargetTransit = (from targetRows in getLstTargetTransit where targetRows.rfqId == rfqid
                                select targetRows).ToList();

            lstTargetTransit.RemoveRange(1, lstTargetTransit.Count - 1);
            return View(lstTargetTransit);
}

以下是[POST]控制器功能的摘录:

 [HttpPost]
    public ActionResult InputTimev2(List<TargetTransitTotal> model)
 {
    if (model != null)  //This value is showing as null
    {
    }
}

有人可以向我解释为什么在视图中正确发送和显示模型,但是当将模型带回控制器(post)时,模型中什么都没有?

由于

1 个答案:

答案 0 :(得分:1)

我认为问题在于,您将值渲染两次:

<td>
  @Html.DisplayFor(model => item.serviceId)
  @Html.HiddenFor(model => item.serviceId)
</td>

所以物品也会被送回控制器两次。 ModelBinder无法决定使用哪一个。 尝试删除HiddenFor()。