使用Partial View,JQuery和Bootstrap Modal编辑记录

问题描述 投票:1回答:1

我有一张桌子,最后一栏的每条记录都有一个Edit按钮。

我的目标是在用户按下Edit按钮的记录的模态上有一个可编辑的表单。

为了实现这一点,我创建了一个部分视图,我希望将其加载到模态上,但经过大量的尝试,我无法使其正常工作。创建JS来组成局部视图URL并加载到模态似乎没有效果,它引发了以下错误:

VM364 ESa31501901:361未捕获的ReferenceError:HTMLAnchorElement.onclick中未定义ESa31501901

注意:ESa31501901是传递给JS函数的第一个参数。

这是我的意图:

a)Edit():此方法将返回所有记录。

b)EditClientFeature(string ClientID, string WorkProcessID):此方法将返回包含特定客户端记录的部分视图。当我们开始编辑客户端记录时调用此方法。客户端记录以模态(弹出)显示。

c)EditClientFeature(ClientFeatureViewModel model):此方法将更新客户端记录。

ClientFeature ViewModel

public class ClientFeatureViewModel
{
    public string ClientID { get; set; }
    public string WorkProcessID { get; set; }
    public int? Certification { get; set; }
    public bool? TrackingActive { get; set; }
    public string ClientCode { get; set; }
    public string ContractNo { get; set; }
    public string ProductCode { get; set; }
}

Edit.cshtml

[...]

<tbody>
@foreach (var feature in Model.ClientFeatures)
{
 <tr>
     <td style="text-align:center"><strong>@feature.WorkProcessId</strong>/td>
     <td style="text-align:center">@feature.Certificate</td>
     <td style="text-align:center">@feature.TrackingActive</td>
     <td style="text-align:center">@feature.ClientCode</td>
     <td style="text-align:center">@feature.ContractNo</td>
     <td style="text-align:center">@feature.ProductCode</td>
     <td>
       <a href="#" class="btn btn-success" onclick="EditCF(@Model.Piva, @feature.WorkProcessId)"><i class="glyphicon glyphicon-pencil"></i></a>                                                                                                                                                                
    </td>
    </tr>
}
</tbody>

[...]

    <div class="modal fade" id="ModalClientFeatures">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <a href="#" class="close" data-dismiss="modal">&times;</a>
                    <h3 class="modal-title">Edit</h3>
                </div>
                <div class="modal-body" id="ModalBodyDiv">

                  <!-- Here's where i want to show the partial view-->

                </div>
            </div>
        </div>
    </div>

[...]

<script>    
        function EditCF(ClientID, WorkProcessID) {

            var url = "/Admin/EditClientFeature?ClientID=" + ClientID + "?WorkProcessID=" + WorkProcessID;

            $("#ModalBodyDiv").load(url, function () {
                $("#ModalClientFeatures").modal("show");

            })

        }
</script>

ClientFeaturepartialView.cshtml

@model Project.Models.ClientFeatureViewModel

<script src="~/Scripts/jquery-3.3.1.js"></script>

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

<form id="myForm">

    <div class="form-horizontal">
        <h4>ClientFeatureViewModel</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        @Html.HiddenFor(model => model.ClientID)

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

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

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

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

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

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

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Save" class="btn btn-default" />
                <a href="#" id="btnSubmit" class="btn btn-success btn-block">
                    <span>Update</span>
                </a>
            </div>
        </div>

    </div>
</form>
}

调节器

    public ActionResult EditClientFeature(string ClientID, string WorkProcessID)
    {
        PMEntities db = new EntityConn().Db;

        ClientFeatures ClientFeature = db.ClientFeatures.Where(cf => cf.ClientId == ClientID && cf.WorkProcessId == WorkProcessID).SingleOrDefault();

        if (ClientFeature != null) {

            ClientFeatureViewModel model = new ClientFeatureViewModel
            {
                ClientID = ClientFeature.ClientId,
                WorkProcessID = ClientFeature.WorkProcessId,
                Certification = ClientFeature.Certificate,
                TrackingActive = ClientFeature.TrackingActive,
                ClientCode = ClientFeature.ClientCode,
                ContractNo = ClientFeature.ContractNo,
                ProductCode = ClientFeature.ProductCode,

            };

            return PartialView("ClientFeaturePartialView", model);
        }
        else { return View("Error"); }      

    }

    [HttpPost]
    public ActionResult EditClientFeature(ClientFeatureViewModel model)
    {
        try
        {
            PMEntities db = new EntityConn().Db;

            if (model.ClientID != null)
            {
                //update
                ClientFeatures ClientFeature = db.ClientFeatures.Where(cf => cf.ClientId == model.ClientID && cf.WorkProcessId == model.WorkProcessID).SingleOrDefault();

                ClientFeature.Certificate = model.Certification;
                ClientFeature.ClientCode = model.ClientCode;
                ClientFeature.ContractNo = model.ContractNo;
                ClientFeature.ProductCode = model.ProductCode;
                ClientFeature.TrackingActive = model.TrackingActive;

                db.SaveChanges();

            }
        }
        catch (Exception ex)
        {
            throw ex;
        }

    }
javascript c# jquery asp.net-mvc partial-views
1个回答
1
投票

我认为你的onclick函数参数值被视为变量,而不是字符串。

解决方案1:

尝试下面传递为字符串:(虽然没有测试!)

onclick="EditCF(\'' + @Model.Piva + '\', \'' + @feature.WorkProcessId+ '\')"

解决方案2:

在为链接添加类之后,最好附加一个click处理程序。并使用HTML5 data attributes存储您的价值客户端。

<a href="#" class="btn btn-success editClient" data-ClientID="@Model.Piva" data-WorkProcessId="@feature.WorkProcessId"><i class="glyphicon glyphicon-pencil"></i></a> 


$('.editClient).on('click', function() {
    var clientID = $(this).data('ClientID');
    var workProcessId = $(this).data('WorkProcessId');

    var url = "/Admin/EditClientFeature?ClientID=" + clientID 
    + "?WorkProcessID=" + workProcessID;

    $("#ModalBodyDiv").load(url, function () {

      $("#ModalClientFeatures").modal("show");

    })

});   

Reference

希望这可以帮助。

© www.soinside.com 2019 - 2024. All rights reserved.