Refreshing MVC PartialView with new Model on DropDownList Change

10,958

You can use ajax to do partial update to your page. when razor render your page, it will generate a SELECT element with the id "Budget_SelectedBills". So listen to the change event on this dropdown, get the selected value and send that to your server(an action method) and let it return the partial view for the markup you want below. You may use jQuery load method to update the DOM with the new markup coming from server.

@section Scripts
{
  <script>
    $(function(){
       $("#Budget_SelectedBills").change(function(e){
         var val=$(this).val();
         $("#billBudgetPartial").load("/Budgeting/BillDetails/"+val);
       });
    });    
  </script>
}

Assuming you have BillDetails action method in BudgetingController which accpets the billId an return the partial view for the bottom portion of screen.

public ActionResult BillDetails(int id)
{
    var model = ReplaceYourModelForBillTotalAmountViewHere();
    return PartialView("Budgeting/_BillTotalAmount", model);
} 

EDIT: As per the comment

How can I pass 2 parameters in this? like not just the id from the drop but something else the list the @Model.BudgetId

If your javascript code is in the same razor view, you can simply use Model.BudgetId as the second querystring param value.

Assuming BudgetId is an int type

@secion Scripts
{
  <script>
    $(function(){
       $("#Budget_SelectedBills").change(function(e){
         var val=$(this).val();
         $("#billBudgetPartial").load("/Budgeting/BillDetails/"+val
                                                            +"?budgetId="[email protected]);
       });
    });    
  </script>
}

Now make sure that your action method has this second parameter

public ActionResult BillDetails(int id,int budgetId)
{
    var model = ReplaceYourModelForBillTotalAmountViewHere();
    return PartialView("Budgeting/_BillTotalAmount", model);
} 

If your javascript code is in an external js file, you may keep Model.BudgetId to somewhere in the DOM and read that. Either a hidden field or keep it in html 5 data attributes of the select element.

Share:
10,958
cxwilson
Author by

cxwilson

Updated on June 27, 2022

Comments

  • cxwilson
    cxwilson almost 2 years

    I have a budgeting application, I have 3 models I am pulling into 1 view.

    • Budget - get the users budgeting information details (i.e, name of budget, date, etc.)
    • BillBudgetTotal - Allows the user to add a cumulative total for that budget (i.d., budgetid, total amount)
    • BudgetTotalBreakdown - Allows the user to break their budget down into futher details (i.e., break the total amount down by bill name (water, gas, electric, misc, etc.)

    The UI will give the user the option to select a budget (via dropdown) they want to work in and then allow them to enter in their dollar amounts based on which bill they selected.

    Problem: I am trying to figure out a way to allow the partial view (the area under the dropdown) to refresh based on the dropdown selection. I can't seem to get the partial to refresh with the updated model (it needs to be reset based on the value of the dropdownlist selection). I have exhausted multiple options on stack overflow.

    Something like this:enter image description here

    Model:

    public class MyBudgets : Financials
        {
            public Budgets Budget{ get; set; }
            public BillBudgetTotal BudgetTotals { get; set; }
            public BillBudgetTotalBreakdown BudgetTotalBreakdown { get; set; }
        }
    

    Html:

         <div class="col-md-3"></div>
         <div class="row col-md-6">
              @Html.DropDownListFor(model => model.Budget.SelectedBills, Model.Budget.SelectedBills.Select(b => new SelectListItem() { Value = b.Bill_Id.ToString(), Text = b.Bill}), "Select A Bill...",  new { @class = "form-control"})
         </div>
         <div class="col-md-3"></div>
         <br /><br />
         <hr />
         <div id="billBudgetPartial">                 
              @Html.Partial("Budgeting/_BillTotalAmount", Model);
         </div>
    

    Controller:

    [HttpGet]
        public ActionResult Budgets(int budgetId)
        {
            MyBudgets model = new MyBudgets
            {
                Budgets = _executionRepository.RetrieveBudgets(budgetId)
            };
    
            model.Budget.SelectedBills = _executionRepository.SetSelectedBudgets(budgetId);
    
            return View(model);
        }
    
        [HttpPost]
        public ActionResult Budgets()
        {
    
    
            return Json(new { success = "false" });
        }
    
        public ActionResult BillTotalAmount(int id)
        {
            var model = new MyBudgets
            {
                Budgets = _executionRepository.RetrieveBudgetsByBillBudget(id),
                BillBudgetTotal = _executionRepository.RetrieveBillBudgetByBillId(id),
                BillBudgetTotalBreakdown = _executionRepository.RetrieveBillBudgetTotalBreakdown (id)
            };
    
            return PartialView("Execution/_BillTotalAmount", model);
        }
    
    • Dushan Perera
      Dushan Perera almost 8 years
      And what is the question/problem you are facing ?
    • cxwilson
      cxwilson almost 8 years
      @Shyju updated above
  • cxwilson
    cxwilson almost 8 years
    how can I pass 2 parameters in this? like not just the id from the drop but something else the list the @Model.BudgetId also