locked
Action link not passing route parameters to controller. RRS feed

  • Question

  • User-183185495 posted

    I am trying to get the values from a form from an action link but there always null for example I have two dropdowns, where are in a for loop I have a edit button at the end of the row to post to the below controller as such.

    @foreach (var item in Model) {
    @if (User.Identity.IsAuthenticated && User.IsInRole("manager") && (item.OIC_2 == null)) {
    <td>
        @if (User.Identity.IsAuthenticated && User.IsInRole("manager")) {
            <select asp-for="@item.OIC_1" id="dditem" asp-items="ViewBag.UserList" class=" form-control" data-style="btn-new">
            </select>
    
        }
    
    </td>
    }
    
                                            
    @if (User.Identity.IsAuthenticated && User.IsInRole("manager") && (item.OIC_2 == null)) {
        <td>
            @if (User.Identity.IsAuthenticated && User.IsInRole("manager")) {
    
                <select asp-for="@item.OIC_2" id="dditem" asp-items="ViewBag.UserList" class=" form-control" data-style="btn-new">
                </select>
    
            }
    
        </td>
    }
    }

    The Edit button is simply a link asp-button But when I pass OIC1 and OIC2 their values are null in the controller action. Also as the model is not saved yet will the model binding have the right values if the user changes the selections in the dropdowns ?

    <a asp-action="AssignCase" class="btn btn-primary" style="background-color:#1e3f5a;color:white" asp-route-OIC1="@item.OIC_1" asp-route-OIC2="@item.OIC_2" asp-route-id="@item.Id">Edit</a> |
    
    [HttpGet]
        
    public async Task<IActionResult> AssignCase(int? id,string? OIC1,string? OIC2) {
            if (id == null) {
                return NotFound();
            }
    
    
        var record = _context.MISobject.Where(w => w.Id == id).FirstOrDefault();
        record.OIC_1 = OIC1;
        record.OIC_2 = OIC2;
    
    
    
        await _context.SaveChangesAsync();
        return RedirectToAction("Index");
    
    
    }

    Tuesday, July 21, 2020 2:54 PM

Answers

  • User-183185495 posted

    I found out that the items in the loop where being named differently ie they would be named 

     name="item.id" they must be the normal name for the binding to work.

    I am trying to get the values from a form from an action link but there always null for example I have two dropdowns, where are in a for loop I have a edit button at the end of the row to post to the below controller as such.

    @foreach (var item in Model) {
    @if (User.Identity.IsAuthenticated && User.IsInRole("manager") && (item.OIC_2 == null)) {
    <td>
        @if (User.Identity.IsAuthenticated && User.IsInRole("manager")) {
            <select asp-for="@item.OIC_1" id="dditem" asp-items="ViewBag.UserList" class=" form-control" data-style="btn-new">
            </select>
    
        }
    
    </td>
    }
    
                                            
    @if (User.Identity.IsAuthenticated && User.IsInRole("manager") && (item.OIC_2 == null)) {
        <td>
            @if (User.Identity.IsAuthenticated && User.IsInRole("manager")) {
    
                <select asp-for="@item.OIC_2" id="dditem" asp-items="ViewBag.UserList" class=" form-control" data-style="btn-new">
                </select>
    
            }
    
        </td>
    }
    }

    The Edit button is simply a link asp-button But when I pass OIC1 and OIC2 their values are null in the controller action. Also as the model is not saved yet will the model binding have the right values if the user changes the selections in the dropdowns ?

    <a asp-action="AssignCase" class="btn btn-primary" style="background-color:#1e3f5a;color:white" asp-route-OIC1="@item.OIC_1" asp-route-OIC2="@item.OIC_2" asp-route-id="@item.Id">Edit</a> |
    [HttpGet]
        
    public async Task<IActionResult> AssignCase(int? id,string? OIC1,string? OIC2) {
            if (id == null) {
                return NotFound();
            }
    
    
        var record = _context.MISobject.Where(w => w.Id == id).FirstOrDefault();
        record.OIC_1 = OIC1;
        record.OIC_2 = OIC2;
    
    
    
        await _context.SaveChangesAsync();
        return RedirectToAction("Index");
    
    
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, July 21, 2020 3:55 PM

All replies

  • User-474980206 posted

    The anchor will have the model value at render, it will not depend on the drop down. You could do a post rather than a like to the drop down values. Otherwise write JavaScript to update the links href on the selects change event.

    Tuesday, July 21, 2020 2:59 PM
  • User-183185495 posted

    How would i have a form inside my loop though is that allow is it legal to do that as it would be a form each row can you provide an example ?

    The anchor will have the model value at render, it will not depend on the drop down. You could do a post rather than a like to the drop down values. Otherwise write JavaScript to update the links href on the selects change event.

    Tuesday, July 21, 2020 3:02 PM
  • User-183185495 posted

    For some reason I tried the following but my properties are comming though null.

    [HttpPost]
    public async Task<IActionResult> AssignCase(int id, [Bind("Id,Title,DateRiased,Subject,Priroty,Category,Type,OIC_1,DateRaised,DateReported,LastUpdatedBy,Description")] MISObject mISObject) {
        if (id != mISObject.Id) {
            return NotFound();
        }
      
    
    
    
        var record = _context.MISobject.Where(w => w.Id == mISObject.Id).FirstOrDefault();
        record.OIC_1 = mISObject.OIC_1;
        record.OIC_2 = mISObject.OIC_2;
    
    
    
        await _context.SaveChangesAsync();
        return RedirectToAction("Index");
    
    
    }

    The form Loop

    <tr>
    <form asp-action="AssignCase" asp-controller="Home">
    
    
        <td>
            @item.DateRaised.ToString("MM/dd/yyyy")
    
        </td>
    
        <td>
            @Html.DisplayFor(modelItem => item.Id)
        </td>
    
        @if (User.Identity.IsAuthenticated && User.IsInRole("manager") && (item.OIC_2 == null)) {
            <td>
                @if (User.Identity.IsAuthenticated && User.IsInRole("manager")) {
                    <select asp-for="@item.OIC_1" asp-items="ViewBag.UserList" class=" form-control" data-style="btn-new">
                    </select>
    
                }
    
            </td>
        }
    
    
        @if (User.Identity.IsAuthenticated && User.IsInRole("manager") && (item.OIC_2 == null)) {
            <td>
                @if (User.Identity.IsAuthenticated && User.IsInRole("manager")) {
    
                    <select asp-for="@item.OIC_2" asp-items="ViewBag.UserList" class=" form-control" data-style="btn-new">
                    </select>
    
                }
    
            </td>
        }
    
        <td>
            @item.CaseNotes
    
    
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Active)
        </td>
    
        @if (User.Identity.IsAuthenticated && User.IsInRole("manager")) {
            <td>
                <button type="submit" class="btn btn-primary">Assign Case </button>
            </td>
                                       
    
    }</form>
    </tr>

    The anchor will have the model value at render, it will not depend on the drop down. You could do a post rather than a like to the drop down values. Otherwise write JavaScript to update the links href on the selects change event.

    Tuesday, July 21, 2020 3:32 PM
  • User-183185495 posted

    I found out that the items in the loop where being named differently ie they would be named 

     name="item.id" they must be the normal name for the binding to work.

    I am trying to get the values from a form from an action link but there always null for example I have two dropdowns, where are in a for loop I have a edit button at the end of the row to post to the below controller as such.

    @foreach (var item in Model) {
    @if (User.Identity.IsAuthenticated && User.IsInRole("manager") && (item.OIC_2 == null)) {
    <td>
        @if (User.Identity.IsAuthenticated && User.IsInRole("manager")) {
            <select asp-for="@item.OIC_1" id="dditem" asp-items="ViewBag.UserList" class=" form-control" data-style="btn-new">
            </select>
    
        }
    
    </td>
    }
    
                                            
    @if (User.Identity.IsAuthenticated && User.IsInRole("manager") && (item.OIC_2 == null)) {
        <td>
            @if (User.Identity.IsAuthenticated && User.IsInRole("manager")) {
    
                <select asp-for="@item.OIC_2" id="dditem" asp-items="ViewBag.UserList" class=" form-control" data-style="btn-new">
                </select>
    
            }
    
        </td>
    }
    }

    The Edit button is simply a link asp-button But when I pass OIC1 and OIC2 their values are null in the controller action. Also as the model is not saved yet will the model binding have the right values if the user changes the selections in the dropdowns ?

    <a asp-action="AssignCase" class="btn btn-primary" style="background-color:#1e3f5a;color:white" asp-route-OIC1="@item.OIC_1" asp-route-OIC2="@item.OIC_2" asp-route-id="@item.Id">Edit</a> |
    [HttpGet]
        
    public async Task<IActionResult> AssignCase(int? id,string? OIC1,string? OIC2) {
            if (id == null) {
                return NotFound();
            }
    
    
        var record = _context.MISobject.Where(w => w.Id == id).FirstOrDefault();
        record.OIC_1 = OIC1;
        record.OIC_2 = OIC2;
    
    
    
        await _context.SaveChangesAsync();
        return RedirectToAction("Index");
    
    
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, July 21, 2020 3:55 PM