使用Modal中的部分视图更新数据库

时间:2017-08-08 14:36:01

标签: asp.net-mvc razor

我有一个强类型的部分视图名为“ChangePassword.cshtml”,如下所示:

@model WebApplication1.Models.ViewModel.ChangeUserPassword

@{
    Layout = null;
}

<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>


@using (Html.BeginForm("ChangePassword", "Account"))
{
    @Html.AntiForgeryToken()
    <div class="form-horizontal">
        <div class="col_full">
            @Html.LabelFor(model => model.OldPassword, htmlAttributes: new { @class = "capitalize t600" })
            @Html.TextBoxFor(model => model.OldPassword, null, new { @class = "sm-form-control", id = "txtOldPassword" })
            @Html.ValidationMessageFor(model => model.OldPassword)
        </div>

        <div class="col_full">
            @Html.LabelFor(model => model.ChangedPassword, htmlAttributes: new { @class = "capitalize t600" })
            @Html.TextBoxFor(model => model.ChangedPassword, null, new { @class = "sm-form-control", id = "txtChangedPassword" })
            @Html.ValidationMessageFor(model => model.ChangedPassword)
        </div>

        <div class="col_full">
            @Html.LabelFor(model => model.ConfirmPassword, htmlAttributes: new { @class = "capitalize t600" })
            @Html.TextBoxFor(model => model.ConfirmPassword, null, new { @class = "sm-form-control", id = "txtConfirmPassword" })
            @Html.ValidationMessageFor(model => model.ConfirmPassword)
        </div>

        <div class="modal-footer">
            <button type="button" class="btn btn-warning" data-dismiss="modal">Cancel</button>
            <input type="submit" value="Save Changes" class="btn btn-primary" id="updatePassword()"/>

        </div>
    </div>
}

我有一个cshtml,它在一个模态中调用这个局部视图。

@model WebApplication1.Models.DB.SysUser
<div class="modal fade" id="modalPassword" tabindex="-1" role="dialog" aria-

labelledby="myModalLabel" aria-hidden="true">
                            <div class="modal-dialog">
                                <div class="modal-body">
                                    <div class="modal-content">
                                        <div id="message"></div>
                                        <div class="modal-header">
                                            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                                            <h4 class="modal-title" id="myModalLabel">Change Password</h4>
                                        </div>
                                        <div class="modal-body">
                                            @*<p class="nobottommargin">@Html.Action("InvokeChangePassword", "Account")</p>*@
                                            <p class="nobottommargin">
                                                @{
                                                    @Html.RenderPartial("ChangePassword", ); <----- HOW TO USE RENDER PARTIAL
                                                }
                                            </p>

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

我将使用模态更新密码。由于它不允许我使用@Html.RenderPartial("ChangePassword"),如果需要@Html.RenderPartial()的对象作为其模型,我如何使用ChangeUserPassword?谢谢。

1 个答案:

答案 0 :(得分:0)

如果要使用renderpartial,请将要传递的模型添加到主模型内的局部视图中。

例如: 如果您需要SysUserChangeUserPassword,请添加包含此类的主模型。

Public class MainModel{
public SysUser suser {get;set;}
public ChangeUserPassword cup {get;set;}
}

然后在主视图中传递主模型并传递ChangeUserPassword,如下所示:

@model WebApplication1.Models.MainModel
<div class="modal fade" id="modalPassword" tabindex="-1" role="dialog" aria-

labelledby="myModalLabel" aria-hidden="true">
                            <div class="modal-dialog">
                                <div class="modal-body">
                                    <div class="modal-content">
                                        <div id="message"></div>
                                        <div class="modal-header">
                                            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                                            <h4 class="modal-title" id="myModalLabel">Change Password</h4>
                                        </div>
                                        <div class="modal-body">
                                            @*<p class="nobottommargin">@Html.Action("InvokeChangePassword", "Account")</p>*@
                                            <p class="nobottommargin">
                                                @{
                  Html.RenderPartial("~/Views/Users/_ChangePassword.cshtml", Model.cup);
                                                }
                                            </p>

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

注意:根据需要更改路径和名称。