简体   繁体   English

通过JavaScript将CSS值传递给部分视图

[英]Passing CSS Values Through JavaScript to Partial View

I am working with an MVC Entity Framework Webapp. 我正在使用MVC实体框架Webapp。 I have a view that displays other partial views that are updated every 30 seconds. 我有一个视图,该视图显示每30秒更新一次的其他局部视图。 The issue is the dynamic progress bar I am using does not fill up at all and I have tried everything I know. 问题是我正在使用的动态进度栏根本没有填满,我已经尝试了所有我知道的东西。 The problem I think is the css that gets passed to the partial view ("width", current_progress + "%"). 我认为问题是传递给局部视图的CSS(“宽度”,current_progress +“%”)。 Here is a pic and the bar is supposed to be over half full... 这是一张照片,酒吧应该满了一半。

在此处输入图片说明

Controller methods: 控制器方法:

    public ActionResult Dashboard()
    {
        ViewBag.SumofDon = db.tblDonors.Sum(s => s.DonationAmount);
        return View(db.tblDonors.ToList());
    }

    public ActionResult Progress()
    {
        return PartialView("_Progress", db.tblDonors.ToList());
    }

Dashboard.cshtml: Dashboard.cshtml:

@model IEnumerable<bssp.Models.tblDonor>

@{
    ViewBag.Title = "Dashboard";
}

@section Scripts{

<script>
function loadProgressPV() {
    $.ajax({
    url: "@Url.Action("Progress")",
    type: 'GET', // <-- make a async request by GET
    dataType: 'html', // <-- to expect an html response
    success: function(result) {
                $('#progress').html(result);
             }
   });
}

$(function() {
    loadProgressPV(); // first time
    // re-call the functions each 10 seconds
    window.setInterval("loadProgressPV()", 10000);
});
</script>

<script>
$(function () {
    var SumofDon = @ViewBag.SumofDon;
    var current_progress = (SumofDon / 20000) * 100; // 20000 is the goal that can be changed
    $("#dynamic")
        .css("width", current_progress + "%") //This causes the problem?
});
</script>

}

<div class="container-fluid">
    <div class="row" id="progress">
        @Html.Partial("_Progress")
    </div>
</div>

Partial view: 部分视图:

@model IEnumerable<bssp.Models.tblDonor>

<div class="row">
<br /><br /><br />
<div class="col-md-12">
    <div class="well bs-component">
        <h4>@String.Format("{0:C}", @ViewBag.SumofDon) out of $20,000<br /></h4>
        <div class="progress progress-striped active">
            <div class="progress-bar" id="dynamic" style="width: 0%"></div> @*The width is what gets updated because it found the id of dynamic*@
        </div>
    </div>
</div>
</div>

Any help would be awesome and thanks in advance! 任何帮助都将非常棒,在此先感谢您!

I think your issue is that you pull in the html from the partial with a style="width: 0%" but then jQuery that adjusts the css, $("#dynamic").css("width", current_progress + "%") , is only run on page load, not after the partial reloads. 我认为您的问题是,您使用style="width: 0%"从局部中提取html,然后使用jQuery调整了CSS, $("#dynamic").css("width", current_progress + "%") ,仅在页面加载时运行,而不在部分重新加载后运行。 You have two options to fix this, 您有两种方法可以解决此问题,

  1. Run the jQuery function after the razor loads in from the partial, in the success method of the ajax. 在ajax的success方法中,从部分加载剃须刀后运行jQuery函数。

  2. Since you are using razor to create the partial, why not just do the calculation right there? 由于您使用剃刀创建局部,为什么不在那里直接进行计算呢? That way the partial comes in already set up with the width set. 这样部分就已经设置好宽度了。 This is the easier and faster option of the two. 这是两者中更容易,更快捷的选择。

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

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