为什么这个actionlink没有通过我的模型?

时间:2014-04-01 10:56:10

标签: c# asp.net-mvc actionlink

我有一个页面,我想在集合中添加/删除项目,然后使用POST提交数据。我正在使用actionlinks来添加和删除。我可以传递我想要修改的项目的ID和修改类型的字符串,即删除或添加。但我的模型参数始终为空。

@model Models.Claims.ClaimAll



@for (int i = 0; i < Model.Claims.Count; i++)
{
    using (Html.BeginForm())
    {
        int ClaimNum = i + 1;
    <div style="border: solid; border-width: thin; padding: 5px;">
        <div style="background-color: #c3bebe">
            @Html.Label("Claim: " + ClaimNum.ToString())
            <hr />
        </div>
        @Html.HiddenFor(m => m.Claims[i].ClaimID)
        @Html.HiddenFor(m => m.Person.PersonID)

        @Html.LabelFor(m => m.Claims[i].ClaimRef)
        @Html.TextBoxFor(m => m.Claims[i].ClaimRef)


        @{List<SelectListItem> list = new List<SelectListItem>{
        new SelectListItem {Text="Legal", Value = "Legal" },
        new SelectListItem {Text="Immigration", Value = "Immigration"},
        new SelectListItem {Text="Housing", Value = "Housing"}
        };}

        @Html.Label("Claim type:")
        @Html.DropDownListFor(m => m.Claims[i].ClaimType, new SelectList(list, "Text", "Value", Model.Claims[i].ClaimType))

        <br />

        @{
          for (int ii = 0; ii < Model.Claims[i].ClaimActions.Count; ii++)
          {
              int ActionNum = ii + 1;
            @Html.Label("Claim action: " + ActionNum.ToString())
            <hr />
            @Html.HiddenFor(m => m.Claims[i].ClaimActions[ii].ClaimActionID)
            @Html.HiddenFor(m => m.Claims[i].ClaimActions[ii].ClaimID)
            @Html.HiddenFor(m => m.Claims[i].ClaimActions[ii].ClaimStatusID)
            @Html.HiddenFor(m => m.Claims[i].ClaimActions[ii].ClaimStatus)

            @Html.Label("Claim status:")
            @Html.DropDownListFor(m => m.ClaimActions[i].ClaimStatusID, new SelectList(Model.ClaimStatuses, "ClaimStatusID", "ClaimStatusText", Model.Claims[i].ClaimActions[ii].ClaimStatusID))

            @Html.LabelFor(m => m.Claims[i].ClaimActions[ii].Notes)
            @Html.TextBoxFor(m => m.Claims[i].ClaimActions[ii].Notes)

            @Html.LabelFor(m => m.Claims[i].ClaimActions[ii].StartDate)
            @Html.TextBoxFor(m => m.Claims[i].ClaimActions[ii].StartDate, new { @class = "datepicker" })

            @Html.LabelFor(m => m.Claims[i].ClaimActions[ii].EndDate)
            @Html.TextBoxFor(m => m.Claims[i].ClaimActions[ii].EndDate, new { @class = "datepicker" }) 
          }

            <table border="1">
                <tr>
                    <td>
                        @*<input type="submit" value="Add claim action for this claim" name="btnSubmit" />*@
                        @Html.ActionLink("Add claim action for this claim", "AddRemoveClaimActions", new {claimID = Model.Claims[i].ClaimID, text = "Add claim action for this claim",  model = Model  })
                    </td>
                    <td>
                        @*<input type="submit" value="Remove claim action for this claim" name="btnSubmit" />*@
                       @Html.ActionLink("Remove claim action for this claim", "AddRemoveClaimActions", new {claimID = Model.Claims[i].ClaimID, text = "Remove claim action for this claim",  model = Model})
                    </td>
                </tr>
            </table> 
        }
    </div>
    <br />
    }
}





<hr />
<input type="submit" value="Update claims data" name="btnSubmit" />
<hr />

<script type="text/javascript">
    $(function () {
        $(".datepicker").datepicker({
            dateFormat: "dd/mm/yy",
            showStatus: true,
            showWeeks: true,
            currentText: 'Now',
            autoSize: true,
            gotoCurrent: true,
            showAnim: 'drop',
            highlightWeek: true,
            changeMonth: true,
            changeYear: true
        });
        $("#anim").change(function () {
            $(".datepicker").datepicker("option", "showAnim", $(this).val());
        });
    });
</script>

<script type="text/javascript">
    function hideGenericLbl() {
        $("#lblGenericMessage").css("color", "red");
        $("#lblGenericMessage").fadeOut(5000);
    }
</script>

<script type="text/javascript">
    $(document).ready(function () {

        hideGenericLbl();

        @if (Model.strMessage != null)
        {
            if (Model.isValidModel == false &&
            Model.strMessage.Contains("Must be at least one claim status if making a claim.") ||
            Model.strMessage.Contains("New claim status available") ||
            Model.strMessage.Contains("One claim status removed."))
            {
                @:$("#modelClaim_ClaimMade").prop('checked', true);
                @:$("#modelPerson_HasDependants").prop('checked', true);
            }
        }

        // Claim made logic
        $("#modelClaim_ClaimMade").click(function () {
            if ($('#modelClaim_ClaimMade').is(':checked')) {

                $("#NewClaim").show();
                //$("#divClaimStatus").show();
            }
            else {
                $("#NewClaim").hide();
                //$("#divClaimStatus").hide();
            }
        });

        if ($('#modelClaim_ClaimMade').is(':checked')) {

            $("#NewClaim").show();
            //$("#divClaimStatus").show();
        }
        else {
            $("#NewClaim").hide();
            //$("#divClaimStatus").hide();
        }

    });
</script>

这些是我的行动链接

<table border="1">
                <tr>
                    <td>
                        @*<input type="submit" value="Add claim action for this claim" name="btnSubmit" />*@
                        @Html.ActionLink("Add claim action for this claim", "AddRemoveClaimActions", new {claimID = Model.Claims[i].ClaimID, text = "Add claim action for this claim",  model = Model  })
                    </td>
                    <td>
                        @*<input type="submit" value="Remove claim action for this claim" name="btnSubmit" />*@
                       @Html.ActionLink("Remove claim action for this claim", "AddRemoveClaimActions", new {claimID = Model.Claims[i].ClaimID, text = "Remove claim action for this claim",  model = Model})
                    </td>
                </tr>
            </table> 

控制器方法

public ActionResult AddRemoveClaimActions(string claimID, string text, ClaimAll model)
        {
            switch (text)
            {
                case "Add claim action for this claim":
                    #region AddClaimStatus
                    //Add new claim to list
                    model.ClaimActions.Insert(model.ClaimActions.Count, new ClaimAction());
                    // SET to false as Model is not ready for DB
                    model.isValidModel = false;
                    // SET message for the user
                    model.strMessage = "New claim status available";
                    break;
                    #endregion
                case "Remove claim action for this claim":
                    #region RemoveClaimStatus
                    // Can't remove IF only 1 
                    model.isValidModel = false;
                    if (model.ClaimActions.Count == 1)
                    {
                        model.strMessage = "Must be at least one claim status if making a claim.";
                    }
                    else
                    {
                        model.ClaimActions.RemoveAt(model.ClaimActions.Count - 1);
                        model.strMessage = "One claim status removed.";

                    }
                    break;
                    #endregion
            }
            return View("~/Views/Claim/EditClaims.cshtml", model);
        }

我最初使用按钮来执行此操作,但执行POST修改视图模型没有意义。但是现在我无法在控制器中访问我的模型,因为它是null。我检查了参数名称是否正确以及对象类型。

0 个答案:

没有答案