列表对象ID值绑定对话框模态属性

时间:2016-01-29 07:23:24

标签: javascript c# jquery asp.net-mvc razor

我有以下列表视图

enter image description here

点击Reject后,我刚配置为打开模式对话框,如下面的

enter image description here

所以一旦我提交了这个模态对话框,我想向方法提交模态值,以便将其保存在数据库表中。

这是相关的模型类

    public class ProductViewModel
    {
        public AB_Product Products { get; set; }
        public IEnumerable<AB_Product> LProducts { get; set; }
    }

    public partial class AB_Product
    {
        public string ProductID { get; set; }
        public string ApprovalStatus { get; set; }
        public string ReasonEn { get; set; }
        public string ReasonAr { get; set; }
    }

这是查看页面代码

@model project_name.Models.ProductViewModel

<table class="table">

    <tr>
        <th>
            Name
        </th>
        <th>
            Action
        </th>
    </tr>

    @foreach (var obj in Model.LProducts)
    {
        <tr>
            @Html.HiddenFor(modelItem => obj.ProductID)

            <td>
                @Html.DisplayFor(modelItem => obj.ProductTitleEn)
            </td>

            <td>

                <div class="btn-group btn-group-sm" id="CreateButton">
                    <button type="button" class="btn btn-default" onclick="location.href='@Url.Action("Create")';return false;">View Product</button>
                </div>

                @if (obj.ApprovalStatus == "Pending")
                {
                    <div class="btn-group btn-group-sm" id="ApproveButton">
                        <button type="button" class="btn btn-success" onclick="location.href='@Url.Action("Change_Product_State","Home", new { productid = obj.ProductID, value = "Approved" },null)';return false;">Approve</button>
                    </div>

                    <div class="btn-group btn-group-sm" id="RejectButton">
                        <button type="button" id="modal-opener" class="btn btn-danger" return false;">Reject</button>
                    </div>

                }

                <div class="btn-group btn-group-sm" id="CreateButton">
                    <button type="button" class="btn btn-primary" onclick="location.href='@Url.Action("Create_ProductComments","Home", new { productid = obj.ProductID }, null)';return false;">Add Comments</button>
                </div>

            </td>

        </tr>
    }

</table>


<div id="dialog-modal" title="Basic modal dialog">
    @using (Ajax.BeginForm("Change_Product_State", "Home", new { value = "Rejected" }, new AjaxOptions { UpdateTargetId = "ID", OnSuccess = "onSuccess" }))
    {
        <div>
            <fieldset>
                <legend>Account Information</legend>

                <div class="editor-label">
                    @Html.LabelFor(m => m.Products.ReasonEn)
                </div>

                <div class="editor-field">
                    @Html.TextAreaFor(m => m.Products.ReasonEn)
                    @Html.ValidationMessageFor(m => m.Products.ReasonEn)
                </div>

                <div class="editor-label">
                    @Html.LabelFor(m => m.Products.ReasonAr)
                </div>

                <div class="editor-field">
                    @Html.TextAreaFor(m => m.Products.ReasonAr)
                    @Html.ValidationMessageFor(m => m.Products.ReasonAr)
                </div>

                <p>
                    <input type="submit" value="Submit" />
                </p>

            </fieldset>
        </div>
    }
</div>


@section Scripts
{

<script>
        $(function () {
            $("#dialog-modal").dialog({
                autoOpen: false,
                width: 400,
                height: 400,
                show: {
                    effect: "blind",
                    duration: 1000
                },
                hide: {
                    effect: "explode",
                    duration: 1000
                }
            });

            $("#modal-opener").click(function () {
                 $("#dialog-modal").dialog("open");
            });
        });

        function onSuccess() {
            $("#dialog-modal").dialog("close");
        }
</script>

}

如何绑定列表对象我单击ProductIDm.Products.ProductID

1 个答案:

答案 0 :(得分:2)

@Html.HiddenFor(modelItem => obj.ProductID)循环和foreach添加

中删除Ajax.BeginForm()
@Html.HiddenFor(m => m.Products.ProductID)

所以它的价值可以发布。然后将ProductID的值添加为按钮的data-属性(并将id更改为class - 请参阅下面的注释)

<button type="button" class="modal-opener btn btn-danger" data-id="@obj.ProductID">Reject</button>

并将脚本修改为

$(".modal-opener").click(function () { // use class name
    $('#Products_ProductID').val($(this).attr("data-id")); // set the value of the hidden input
    $("#dialog-modal").dialog("open");
})

请注意,由于id循环生成了重复的foreach属性,您有很多无效的html。请改用类名。你的HTML应该是

@foreach (var obj in Model.LProducts)
{
    <tr>
        <td>@Html.DisplayFor(modelItem => obj.ProductTitleEn)</td>
        <td>
            <div class="btn-group btn-group-sm CreateButton"> // change
                <button type="button" class="btn btn-default" onclick="location.href='@Url.Action("Create")';return false;">View Product</button>
            </div>
            @if (obj.ApprovalStatus == "Pending")
            {
                <div class="btn-group btn-group-sm ApproveButton"> // change
                    <button type="button" class="btn btn-success" onclick="location.href='@Url.Action("Change_Product_State","Home", new { productid = obj.ProductID, value = "Approved" },null)';return false;">Approve</button>
                </div>
                <div class="btn-group btn-group-sm RejectButton"> // change
                    <button type="button" class="modal-opener btn btn-danger" data-id="@obj.ProductID">Reject</button> // as above
                </div>
            }
            <div class="btn-group btn-group-sm CreateButton"> // change
                <button type="button" class="btn btn-primary" onclick="location.href='@Url.Action("Create_ProductComments","Home", new { productid = obj.ProductID }, null)';return false;">Add Comments</button>
            </div>
        </td>
    </tr>
}