简体   繁体   English

带有局部视图的MVC模态未预先放置Editor进行模型绑定控制

[英]MVC modal with partial view not prepoulating EditorFor control with model binding

I have an MVC App that displays a popup Modal window to edit some data when a button is clicked. 我有一个MVC应用程序,当单击按钮时,该应用程序会显示一个弹出模态窗口,以编辑一些数据。 All of the mechanics seem to work. 所有的机制似乎都起作用。 It steps through everything 它贯穿一切 在此处输入图片说明 fine. 精细。 Loads the Modal. 加载模态。 The model as shown below is populated in the Controller 控制器中填充了如下所示的模型

and when I step into the view code the model is clearly populated..so it passed fine... 当我进入视图代码时,模型显然已填充..因此它通过得很好...

在此处输入图片说明

So my confusion is why when the model loads..are all of the EditorFor fields empty./ when the model is not and the controls are bound to the model properties. 所以我的困惑是为什么当模型加载时..所有的EditorFor字段为空./当模型未加载且控件绑定到模型属性时。

Clearly I'm missing something..I'm just not sure what... 显然我缺少了一些东西。我只是不确定...

Here is the code in my Partial View... 这是我的局部视图中的代码...

 @model Hybridinator.Domain.Entities.Database
<br />
<br />

<div class="modal-header">
    <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
    <h4 class="modal-title" id="myModalLabel">Edit Database Info</h4>
</div>
<div class="modal-body">
    @using (Html.BeginForm())
{


            <div class="form-group">
                <div id="databaselabel" >@Html.LabelFor(m => m.database, "Database")</div>
                <div id="databaseedit" >@Html.EditorFor(m => m.database )</div>
            </div>
            <div class="form-group">
                <div id="databaseserverlabel" >@Html.LabelFor(m => m.database_server, "Database Server")</div>
                <div id="databaseserveredit" >@Html.EditorFor(m => m.database_server )</div>
            </div>
            <div class="form-group">
                <div id="databaseusernamelabel" >@Html.LabelFor(m => m.database_username, "Database Username")</div>
                <div id="databaseusernameedit" >@Html.EditorFor(m => m.database_username)</div>
            </div>
            <div class="form-group">
                <div id="databasepasswordlabel" >@Html.LabelFor(m => m.database_password, "Database Password")</div>
                <div id="databasepasswordedit" >@Html.EditorFor(m => m.database_password)</div>
            </div>
            <div class="form-group">
                <div id="histdatabaselabel"> @Html.LabelFor(m => m.hist_database, "History Database")</div>
                <div id="histdatabaseedit" >@Html.EditorFor(m => m.hist_database)</div>
            </div>
            <div class="form-group">
                <div id="histdatabaseserverlabel"> @Html.LabelFor(m => m.hist_database_server, "History Database Server")</div>
                <div id="histdatabaseserveredit">@Html.EditorFor(m => m.hist_database_server)</div>
            </div>
            <div class="form-group">
                <div id="histdatabaseusernamelabel" >@Html.LabelFor(m => m.hist_database_username, "History Database Username")</div>
                <div id="histdatabaseusernameedit" >@Html.EditorFor(m => m.hist_database_username)</div>
            </div>
            <div class="form-group">
                <div id="histdatabasepasswordlabel" >@Html.LabelFor(m => m.hist_database_password, "History Database Password")</div>
                <div id="histdatabasepasswordedit" >@Html.EditorFor(m => m.hist_database_password)</div>
            </div>

}

    </div>
<div class="modal-footer">
    <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
    <button type="button" class="btn btn-primary">Submit</button>
</div>

And here is the code in the index where the modal loads 这是索引中模态加载的代码

<div class="modal fade" id="modalEditDBInfo" role="application" aria-labelledby="modalEditDBInfoLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modalEditDBInfoContent" style="background-color:white; border-radius:10px; box-shadow:10px;">
            @Html.Partial("_EditDatabaseInfo")
        </div>
    </div>
</div>

The Code for the button that calls the JQuery script 调用JQuery脚本的按钮的代码

<a href="#EditDatabaseButton" class="btn btn-primary" onclick="editDatabaseInfo()">Edit</a>

and the code of the script 和脚本代码

<script type="text/javascript">
    function editDatabaseInfo() {
        var dbid = $('#database_pk').val();
        alert('function values are ' + dbid);
        $.ajax({
            url: '@Url.Action("EditDatabaseInfo", "Database")',
            type: 'GET',
            cache: false,
            data: { database_pk: dbid }
        }).done(function (result) {
            $('#modalEditDBInfoContent').html(result);
        });
        $('#modalEditDBInfo').modal('show');
    }
</script>

And LASTLY an image of the Modal after it Load..you will not none of the edit fields are prepopulated 最后,加载后模态的图像..您将不会预先填充任何编辑字段

在此处输入图片说明

Your modalEditDBInfoContent doesn't have an id = modalEditDBInfoContent. 您的modalEditDBInfoContent没有id = modalEditDBInfoContent。 Its' the class. 这是课程。

$('.modalEditDBInfoContent').html(result);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM