2

I have the following code and I am trying to add the selected value to the model.SelectedPayCodes list everytime the user makes a selection from the dropdown and clicks on "Add"

CONTROLLER

[HttpPost]
public PartialViewResult AddPayCode(ReferralModel model, string SelectedPayCode)
{
    var payCode = _employeeService.GetPayCodeById(Convert.ToInt32(SelectedPayCode));

    model.Add(payCode); 
    return PartialView("_PayCodesPartial",model);
}

MODEL

public class ReferralModel
    {
        public Customer Customer { get; set; }
        public Employee Employee { get; set; }
        public List<PayCode> PayCodes { get; set; } // List of paycodes
        public List<PayCode> PayCodesList { get; set; } // List of 'selected' paycodes
        public SelectListItem SelectedPayCode { get; set; } // current selected paycode

        public Referral Referral { get; set; }

        public ReferralModel()
        {
            PayCodesList = new List<PayCode>();
        }

        // Step 3 - Add Paycode is done here.
        public void Add(PayCode payCode)
        {
            PayCodesList.Add(payCode);
        }
    }

VIEW - Create.cshtml

    @using (Ajax.BeginForm("AddPayCode", "Referral",
    new AjaxOptions()
    {
        HttpMethod = "POST",
        InsertionMode = InsertionMode.InsertAfter,
        UpdateTargetId = "PayCodes",
    }))
    {
        @Html.AntiForgeryToken()
        @Html.ValidationSummary()
        <!-- payCodes input-->
        <div class="control-group col-lg-6">
            <label class="control-label">Product</label>
            <div class="controls">
                <!-- Step 1 + Choose PayCode Here -->
                @Html.DropDownListFor(model => model.SelectedPayCode, new SelectList(Model.PayCodes.ToList(), "Id", "Description"), "- Select -")
                <input type="submit" value="Add" />

            </div>
        </div>

        <!-- pay codes list -->
        <div id="PayCodes">
        </div>
    }

VIEW - _PayCodesPartial.cshtml

@model Zenwire.Models.ReferralModel

@if (Model.PayCodesList != null)
{
    foreach (var item in Model.PayCodesList)
    {
        string.Format("{0} - {1}", item.Code, item.Description);
    }
}

The issue seems to be whenever a new item is added the list is empty again and does not retain it's previous selection.

3
  • You are not adding the the values of the current list from the view to the model. You are just adding the latest value. Commented Nov 4, 2013 at 4:36
  • You could save the values in the database. Commented Nov 4, 2013 at 4:45
  • I am trying to build a list first because the main form on the page posts to the database and I need the referral ID for the row inserted to link to the list of paycodes... If I store the paycode to the database I need a way to link them all as they are added to the referral that will get created. Commented Nov 4, 2013 at 4:54

1 Answer 1

2

Try this. I used Session State

 public PartialViewResult AddPayCode(ReferralModel model, string SelectedPayCode)
    {
        var payCode = _employeeService.GetPayCodeById(Convert.ToInt32(SelectedPayCode));

        model.PayCodesList = GetPayCodes();
        model.Add(payCode);
        Session["paycodes"] = model.PayCodesList; 
        return PartialView("_PayCodesPartial",model);
    }

    private List<PayCode> GetPayCodes()
    {
        List<PayCode> payCodes = (List<PayCode>)Session["paycodes"];
        if (payCodes == null )
        {
           payCodes = new List<PayCode>();
           Session["paycodes"] = paycode;
        }
        return payCodes;
     }
Sign up to request clarification or add additional context in comments.

1 Comment

Try referencing System.Web

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.