简体   繁体   English

在 ASP.MVC 中请求后刷新模型

[英]Refresh model after post-request in ASP.MVC

I'm developping a webApp with MVC.我正在用 MVC 开发一个 webApp。 I have a view with cirkles displaying a value and a slider, when you slide the cirkles need to display the new value.我有一个带有显示值和滑块的 cirkles 视图,当您滑动时,cirkles 需要显示新值。 I send the new value with a POST from my AJAX call to the controller, which does a minor calculation with the value and give it back to the view so the cirkles can display the updated value.我从我的 AJAX 调用中通过 POST 将新值发送到控制器,控制器对值进行少量计算并将其返回给视图,以便 cirkles 可以显示更新后的值。 However my view still keeps using the startvalue.但是我的观点仍然继续使用 startvalue。

   @model UGT.UI.Web.MVC.Models.BelastingViewModel 
<script language="JavaScript">
var config1 = liquidFillGaugeDefaultSettings();

@{
        teller = 1;
        string naam_var = null;

        foreach (KeyValuePair<UGT.BL.Domain.BegrotingPackage.Categorie, double> cat in Model.Belasting)
        {
            naam = "fillgauge" + teller;
            naam_var = "gauge" + teller;
            @: var @naam_var = loadLiquidFillGauge("@naam", "@Html.DisplayFor(modelItem => cat.Value)", config1);

                teller++;
        }
    }

function toonCirkels() {

    @{

        teller = 1;
        naam = "fillgauge" + teller;
        string naam_var2 = null;
        foreach (KeyValuePair<UGT.BL.Domain.BegrotingPackage.Categorie, double> cat in Model.Belasting)
        {
            naam_var2 = "gauge" + teller;
            // @: gauge1.update("@Html.DisplayFor(modelItem => cat.Value)");
                 // @: var @naam_var = loadLiquidFillGauge("@naam", "@Html.DisplayFor(modelItem => cat.Value)", config1);
                   // @: @naam_var2.update("@Html.DisplayFor(modelItem => cat.Value)");
             teller++;
         }

        //@:gauge1.update("500");

     }
}

public class BelastingsController : Controller
  {
    private BegrotingsManager begrotingsManager = new BegrotingsManager();
    private int gemeenteId = 54;
    private double loon = 200;
    private BelastingViewModel belastingen = new BelastingViewModel();

    // GET: Belastings
    public ActionResult Index()
    {
            var belasting = begrotingsManager.GetBelastingGebruiker(this.loon, gemeenteId);
      belastingen.Belasting = belasting;
      UpdateModel(belastingen);
      return View(belastingen);

    }

    [HttpPost]
    public ActionResult Index(String loon)
    {
      this.loon = Double.Parse(loon);
      var belasting = begrotingsManager.GetBelastingGebruiker(this.loon, gemeenteId);
            belastingen.Belasting = belasting;
            UpdateModel(belastingen);
            return new HttpStatusCodeResult(HttpStatusCode.OK);
           // return RedirectToAction("Index");
    }

namespace UGT.UI.Web.MVC.Models
{
  public class BelastingViewModel
  {
    public IDictionary<Categorie, double> Belasting { get; set; }
  }


}

     d3.selectAll('.range').on('change', function () {
        this.value = parseInt(this.value);
        if (this.value < 0) this.value = 0;
        else if (this.value > 5000) this.value = 5000;


        var loon = this.value;
        var loonString = "€" + loon;
        d3.select('.range_value').html(loonString);

        sendLoon(loon, loonString);
    });
}

function sendLoon(loon, loonString) {
    $.ajax({
        contentType: "application/json; charset=utf-8",
        url: "/Belastings",
        type: "POST",
        data: JSON.stringify({ "loon": loon }),
        success: function () {
          // window.location.reload();
            toonCirkels();
        },
        error: function () { }

    });
}

The success of your Ajax call calls 'toonCirkels' which only contains razor generated code which is filled on page load.您的 Ajax 调用成功调用“toonCirkels”,它只包含在页面加载时填充的 razor 生成的代码。 The content of this method never changes as it contains ONLY razor generated code and thus will always have the same logic with the same values.此方法的内容永远不会改变,因为它只包含剃刀生成的代码,因此将始终具有相同的逻辑和相同的值。

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

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