locked
Building an action link with model and selected option RRS feed

  • Question

  • User965740324 posted

    In my TradingPartner model I have a property IsSelected.  Only one trading partner will be selected.  Then I populate the select options with all the trading partners that are not selected.  What I'm trying to do is send the targetId (from the model.IsSelected==true) and the sourceId (from the option list) back to the controller.  How would I go about doing this?

    @model IEnumerable<ETL_Staging_UI.Models.TradingPartner>
    
    @{
        ViewData["Title"] = "Copy Rules";
        Layout = "~/Views/Shared/_Layout.cshtml";
    }
    
    <h4>
        Target trading partner
        @foreach (var item in Model)
    {
        @if(item.IsSelected)
        {
        <label class="control-label">@item.Designation</label>
        }
    }
    </h4>
        <hr />
        <div>
            <form>
                <div class="form-group">
                    <label class="control-label">Copy Rules From</label>
                    <select class="form-control" style="width:auto">
                        @foreach (var item in Model)
                    {
                        @if(!item.IsSelected)
                        {
                        <option value="@item.Id">@item.Designation</option>
                        }
                    }
                    </select>
                    @Html.ActionLink("Copy", "CopyRules", new { sourceId = 3, targetId = 4 }, new {@class = "btn  btn-default"})
    
                </div>
            </form>
        </div>
    
        <div>
            <a asp-action="Index">Back to List</a>
        </div>
    

    https://ibb.co/9NfSkqd

    Wednesday, August 19, 2020 5:43 PM

Answers

  • User711641945 posted

    Hi wild_bill,

    Here is a working dmeo like below:

    @model IEnumerable<TradingPartner>
    @{
        ViewData["Title"] = "Copy Rules";
        Layout = "~/Views/Shared/_Layout.cshtml";
        var sId = 0;
    }
    <h4>
        Target trading partner
        @foreach (var item in Model)
        {
            @if (item.IsSelected)
            {
                sId = item.Id;
                <label class="control-label">@item.Designation</label>
            }
        }
    </h4>
    <hr />
    <div>
        <form>
            <div class="form-group">
                <label class="control-label">Copy Rules From</label>
                <select class="form-control" style="width:auto">
                    @foreach (var item in Model)
                    {
                        @if (!item.IsSelected)
                        {
                            <option value="@item.Id">@item.Designation</option>
                        }
                    }
                </select>
                @Html.ActionLink("Copy", "CopyRules", new { sourceId = sId }, new { @class = "btn  btn-default" })
    
            </div>
        </form>
    </div>
    
    <div>
        <a asp-action="Index">Back to List</a>
    </div>
    @section Scripts{ 
        <script>
            $(function () {
                var href = $("a.btn.btn-default").attr("href");
                localStorage['href'] = href;
            })
            $("select").change(function () {
                var data = $("select").val();
                $("a.btn.btn-default").attr("href", localStorage['href'] + "&targetId=" + data);
            });
        </script>
    }

    Result:

    Best Regards,

    Rena

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 20, 2020 7:37 AM

All replies

  • User-474980206 posted

    put the selected value in a hidden field in the form and change the link to a submit button.

    Wednesday, August 19, 2020 7:32 PM
  • User711641945 posted

    Hi wild_bill,

    Here is a working dmeo like below:

    @model IEnumerable<TradingPartner>
    @{
        ViewData["Title"] = "Copy Rules";
        Layout = "~/Views/Shared/_Layout.cshtml";
        var sId = 0;
    }
    <h4>
        Target trading partner
        @foreach (var item in Model)
        {
            @if (item.IsSelected)
            {
                sId = item.Id;
                <label class="control-label">@item.Designation</label>
            }
        }
    </h4>
    <hr />
    <div>
        <form>
            <div class="form-group">
                <label class="control-label">Copy Rules From</label>
                <select class="form-control" style="width:auto">
                    @foreach (var item in Model)
                    {
                        @if (!item.IsSelected)
                        {
                            <option value="@item.Id">@item.Designation</option>
                        }
                    }
                </select>
                @Html.ActionLink("Copy", "CopyRules", new { sourceId = sId }, new { @class = "btn  btn-default" })
    
            </div>
        </form>
    </div>
    
    <div>
        <a asp-action="Index">Back to List</a>
    </div>
    @section Scripts{ 
        <script>
            $(function () {
                var href = $("a.btn.btn-default").attr("href");
                localStorage['href'] = href;
            })
            $("select").change(function () {
                var data = $("select").val();
                $("a.btn.btn-default").attr("href", localStorage['href'] + "&targetId=" + data);
            });
        </script>
    }

    Result:

    Best Regards,

    Rena

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 20, 2020 7:37 AM
  • User965740324 posted

    You went above and beyond giving me help on this. Thank you!

    Thursday, August 20, 2020 3:11 PM