简体   繁体   English

覆盖模态形式MVC / C#的宽度

[英]Override the width of a modal form MVC/C#

I've got a modal form that is being shared across the application, now I'd like to reuse the same definition for this particular modal but just to override the width. 我已经在整个应用程序之间共享了一个模式形式,现在我想对这个特定模式重复使用相同的定义,但是只是覆盖宽度。 Is there any way of doing this? 有什么办法吗?

<div id="modal-container" class="modal fade">
<div class="modal-dialog" style="width: 90%;"> -- this style attribute should change whenever gets called on a specific view
    <div class="modal-content">
        <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
            <h2 class="modal-title"></h2>
        </div>
        <div class="modal-body partialContainer" style="max-height: 600px;">
        </div>

        <div class="modal-footer">
            <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
            <button type="button" class="btn btn-primary" id="modal-submit">Save</button>
        </div>
    </div>
</div>

Place your model definition in a partial view ( _MyModel.cshtml ) and replace the width from the dynamic ViewDate and call the partial view by passing the width. 将模型定义放在局部视图( _MyModel.cshtml )中,并从动态ViewDate替换宽度,然后通过传递宽度来调用局部视图。

Partial View Code: 部分查看代码:

<div id="modal-container" class="modal fade">
    <div class="modal-dialog" style='@ViewData["width"].ToString()'> -- this style attribute                  should change whenever gets called on a specific view
<div class="modal-content">
    <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
        <h2 class="modal-title"></h2>
    </div>
    <div class="modal-body partialContainer" style="max-height: 600px;">
    </div>

    <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        <button type="button" class="btn btn-primary" id="modal-submit">Save</button>
    </div>
</div>

Call the above partial view from any view or partial view by passing the width as following 通过传递宽度,从任何视图或局部视图调用以上局部视图

  @Html.Partial("_MyModel", new ViewDataDictionary { { "width", "500px" } })

We can also user Model or viewBag to pass data. 我们还可以使用Model或viewBag传递数据。

Add you own class in you model for which you want to change width 在您要更改宽度的模型中添加自己的类

<div id="modal-container" class="modal fade customClass"> // added custom class
<div class="modal-dialog" style="width: 90%;"> -- this style attribute should change whenever gets called on a specific view
    <div class="modal-content">
        <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
            <h2 class="modal-title"></h2>
        </div>
        <div class="modal-body partialContainer" style="max-height: 600px;">
        </div>

        <div class="modal-footer">
            <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
            <button type="button" class="btn btn-primary" id="modal-submit">Save</button>
        </div>
    </div>
</div>

Then add css for that class only 然后仅为该类添加CSS

.customClass{
  width:400px; // custom width 
}

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

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