简体   繁体   English

将数据从一个视图传递到另一视图-MVC

[英]Pass data from one view to another view -MVC

What would be the best method to transfer data from one view to another view? 将数据从一个视图传输到另一视图的最佳方法是什么? or is this bad practice? 还是这是不好的做法?

The reason is that i want to display @model.count() from one view to the homepage, basically a summary of all the views on the homepage. 原因是我想从一个视图显示@ model.count()到主页,基本上是主页上所有视图的摘要。 There is tempdata, html.helper etc but id like to know the best and most reliable method. 有tempdata,html.helper等,但是id喜欢知道最好和最可靠的方法。 Thus avoiding problems further on down the line 从而避免进一步的问题

here is the code in question 这是有问题的代码

product.cshtml product.cshtml

<p>Total amount of Products = @Model.Count()</p>

i basically want that same value to display on the homepage 我基本上希望在首页上显示相同的值

You'd have to refresh the homepage for the value to actually 'update.' 您必须刷新首页才能使该值实际“更新”。 At that point you may as well pass the value back in as a parameter, but I think that's not what you're hoping for. 那时,您还可以将值作为参数传递回去,但是我认为这不是您想要的。

It probably isn't the cleanest solution, but I would think you'd want your text on the main page to be settable via some javascript/jquery. 它可能不是最干净的解决方案,但我认为您希望可以通过一些javascript / jquery设置主页上的文本。

Main Page has this snippet: 主页包含以下代码段:

<form id="PassBackVals"
<input type="hidden" name="ProductCount" value="0">
</form> 

<script>$( ".ProductCount" ).change(function() {
  $("$CurrentProductCount").text = $(".ProductCount").val;
});
</script>

and the Area displaying the text itself on the main would have something along the lines of 而在主屏幕上显示文本本身的区域将沿着

<p> I'm currently showing <div id="CurrentProductCount">0</div> Products </p>

Then in your product.cshtml, you'd have 然后在您的product.cshtml中,您将拥有

<script>$(".ProductCount").val = @Model.Count()
$( ".ProductCount" ).change();
</script>

Ok i looked into other methods and resolved the issue by moving the code behind the viewbag into a public method below 好的,我研究了其他方法,并通过将视图包后面的代码移到下面的公共方法中解决了该问题

    [OutputCache(NoStore = true, Location = OutputCacheLocation.Client, Duration = 10)]
    public ActionResult UpdateTotals()
    {
        JobController j = new JobController();
        ViewBag.JobCount = j.JobCount();

        ProductController p = new ProductController();
        ViewBag.ProductCount = p.ProductCount();

        return PartialView("UpdateTotals");
    }

then i added the viewbags into a partial view and using setinterval() javascript, 然后我将视图包添加到局部视图中,并使用setinterval()javascript,

    <div id ="UpdateTotals">
        @{ Html.RenderAction("UpdateTotals");}
</div>

<script>
    setInterval("$('#UpdateTotals').load('/home/UpdateTotals')", 10000);
</script>

i was able to refresh the data constantly to my desire without the mess of pulling data through views via javascript. 我能够不断刷新数据以达到我的期望,而不会造成通过javascript通过视图拉取数据的麻烦。

hopefully this will help anyone in future :) 希望这会对以后的任何人有所帮助:)

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

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