MVC模态引导程序编辑

时间:2016-02-09 12:36:17

标签: c# asp.net-mvc

我正在努力编辑MVC模态引导程序中的记录

请看下面的代码:模型

public partial class CleanSupplierClaim
{
    public int Id { get; set; }
    public string Action { get; set; }
    public string Line_Number { get; set; }
    public string Total_Claim { get; set; }
    public string Currency { get; set; }
    public string ClaimReference { get; set; }
    public string ST_Key { get; set; }
    public string Supplier_Claim { get; set; }
    public string Original_Invoice { get; set; }
    public string System_Cost { get; set; }
    public string Error { get; set; }
    public string Domain_Username { get; set; }
}

我的部分视图

    @model CRMSupplierClaimsUplaod.CleanSupplierClaim

<div class="modal-header">
    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
    <h4 class="modal-title" id="myModalLabel">Edit Claim</h4>
</div>

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()

    <div class="modal-body">
        <div class="form-horizontal">
            @Html.ValidationSummary(true, "", new { @class = "text-danger" })
            @Html.HiddenFor(model => model.Id)

            <div class="form-group">
                @Html.LabelFor(model => model.ST_Key, htmlAttributes: new { @class = "control-label col-md-3" })
                <div class="col-md-9">
                    @Html.EditorFor(model => model.ST_Key, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.ST_Key, "", new { @class = "text-danger" })
                </div>
            </div>

            <div class="form-group">
                @Html.LabelFor(model => model.Total_Claim, htmlAttributes: new { @class = "control-label col-md-3" })
                <div class="col-md-9">
                    @Html.EditorFor(model => model.Total_Claim, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.Total_Claim, "", new { @class = "text-danger" })
                </div>
            </div>

            <div class="form-group">
                @Html.LabelFor(model => model.Currency, htmlAttributes: new { @class = "control-label col-md-3" })
                <div class="col-md-9">
                    @Html.EditorFor(model => model.Currency, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.Currency, "", new { @class = "text-danger" })
                </div>
            </div>
        </div>
    </div>

    <div class="modal-footer">
        <button class="btn" data-dismiss="modal">Cancel</button>
        <input class="btn btn-primary" type="submit" value="Save" />
    </div>
}

这是我的视图我试图将Record从

传递给控制器
    <div id='myModal' class='modal fade in'>
    <div class="modal-dialog">
        <div class="modal-content">
            <div id='myModalContent'></div>
        </div>
    </div>
</div>


<div class="panel panel-primary">
    <div class="panel-heading">Claims</div>

    <div class="panel-body ">
        @Html.ActionLink("Add", "Create", "People", null, new { data_modal = "", id = "btnCreate", @class = "btn btn-small btn-primary pull-right" })
    </div>

    <table class="table table-bordered table-hover">
        @foreach (var item in Model)
        {
            <tr>
                <td style="width:20px">
                    <div class="btn-group">
                        @*<button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown" data-hover="dropdown" data-delay="1000" data-close-others="true">
                            <span class="caret"></span>
                        </button>*@

                        @Html.ActionLink("Edit", "EditClaims", new { @id = item.Id })

                    </div>
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.ST_Key)  @Html.DisplayFor(modelItem => item.Total_Claim)   @Html.DisplayFor(modelItem => item.Supplier_Claim)   @Html.DisplayFor(modelItem => item.System_Cost)
                    <div class="text-muted">
                        <small>
                            @Html.DisplayFor(modelItem => item.Currency)
                        </small>
                    </div>
                </td>

            </tr>
        }

    </table>

    <div class="panel-footer">Count = @Model.Count()</div>


</div>

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
    @Scripts.Render("~/bundles/modalform")
}

这是我的控制器代码:

public async Task<ActionResult> EditClaims(int? id)
    {

        //claimId  = (int)TempData["claimID"];

        if (id == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }

        CleanSupplierClaim claim = await db.CleanSupplierClaims.FindAsync(id);

        if (claim == null)
        {
            return HttpNotFound();
        }


        return PartialView("_EditClaimPartial", claim);
    }


    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> EditClaims([Bind(Include = "Id,Line_Number,Total_Claim,Currency")] CleanSupplierClaim claim)
    {
        if (ModelState.IsValid)
        {
            db.Entry(claim).State = EntityState.Modified;
            await db.SaveChangesAsync();
            return Json(new { success = true });
        }
        return PartialView("_EditClaimPartial", claim);
    }

这是我的模态JS代码,

   $(function () {

    $.ajaxSetup({ cache: false });

    $("a[data-modal]").on("click", function (e) {

        // hide dropdown if any
        //$(e.target).closest('.btn-group').children('.dropdown-toggle').dropdown('toggle');


        $('#myModalContent').load(this.href, function () {


            $('#myModal').modal({
                /*backdrop: 'static',*/
                keyboard: true
            }, 'show');

            bindForm(this);
        });

        return false;
    });


});

function bindForm(dialog) {

    $('form', dialog).submit(function () {
        $.ajax({
            url: this.action,
            type: this.method,
            data: $(this).serialize(),
            success: function (result) {
                if (result.success) {
                    $('#myModal').modal('hide');
                    //Refresh
                    location.reload();
                } else {
                    $('#myModalContent').html(result);
                    bindForm();
                }
            }
        });
        return false;
    });
}

我的问题是编辑声明在操作链接中传递了一个空ID,任何想法在这里有什么问题。请帮忙。

1 个答案:

答案 0 :(得分:0)

您必须添加更多参数,例如

@Html.ActionLink("Edit", "EditClaims", "Villa", new {id = item.Id}, null)

您可能需要编辑@符号的位置。你已经把它添加到那里,不知道为什么。 看到Null?它表示html属性的参数。你需要添加它。