0

I have a .NET Core 2 MVC project, and I am very new to this. I have a Details View that contains data for a selected user. I am using a ViewModel to display that selected user's data and it is all working fine. What I am trying to ultimately do is allow for the cloning of one user's Identity Roles to the current selected user. So I have in my View a panel that contains a SelectList to where you can select some other user (I'm calling them the source user) to get their currently assigned Identity Roles. I then want to have a button that says "Clone" and on click remove all Roles for the current user and then add them to the Roles that the selected source user is assigned to.

Here is what I am using for my drop-down list:

View:

<select asp-for="EmployeeList" class="form-control"
    asp-items="@Model.EmployeeList"                                 
    onchange="this.form.submit()">
    <option disabled selected>-</option>
</select>

Controller:

var employeeList = _employeeRepository.GetEmployeeList().Select(e => new
{
    Id = e.EmployeeId,
    Value = e.PreferredFirstName != null ? e.LastName + ", " + e.PreferredFirstName : e.LastName + ", " + e.FirstName                            
});

ViewModel:

public SelectList EmployeeList { get; set; }

In my View I have a form for the post:

@using (Html.BeginForm("Details", "ApplicationUser", FormMethod.Post))

So I need to pass the selected id (string) of my source user drop-down list to the [HttpPost] Details action in my controller. I also want to maintain the selected option in my drop-down list in the View. I also want to maintain all of the data that is being displayed in my ViewModel.

Your help is greatly appreciated.

4
  • Your view code does not make sense. You cannot use the same property for the property your binding to and the SelectList - your view model needs a property (say) int SelectedEmployee and then its <select asp-for="EmployeeList" asp-items="@Model.EmployeeList" .... (and your EmployeeList property should be IEnumerable<SelectListItem>, not SelectList Commented Dec 14, 2017 at 23:14
  • The the worst practice you have is submitting the form in the onchange event. DO NOT do that! Commented Dec 14, 2017 at 23:15
  • I added int SelectedEmployee to my ViewModel class. I don't see how I would set that value and maintain that value as selected? Commented Dec 15, 2017 at 16:16
  • You set the vale of SelectedEmployee in the GET method before you pass the model to the view (if it matches the value of one of you options, then that option will be selected). And you submit your form using a submit button. But we have no idea what your 'Clone' is or why your doing that (it makes no sense) and you have not shown any code relating to that. Commented Dec 15, 2017 at 23:13

1 Answer 1

2

As said before, your view is really not making sense. However we can fix that.

Viewmodel:

public List<SelectListItem> EmployeeList { get; set; }

View:

<select id="employee-select" asp-for="EmployeeId" class="form-control" onchange="postEmployee()" asp-items="@Model.EmployeeList">
    <option disabled selected>-</option>
</select>

<script>
    function postEmployee() {
        var id = $('#employee-select').val();
        if (id !== '' && id != null) {
            var url = '../CONTROLLERNAME/UpdateUser/' + id;
            var settings = {
                "url": url,
                "method": 'POST'
            };
            $.ajax(settings);
        }
    }
</script>

Controller:

[HttpPost("UpdateUser/{id}")]
public IActionResult UpdateUser([FromRoute] int id)
{
    // Do things with id
}
Sign up to request clarification or add additional context in comments.

5 Comments

I am confused on this. How is my SelectList posting back if I don't use onchange?
As you say, you need the select-list to stay the same. You should use ajax to submit only the value you need to your controller.
Can you give me an example, please?
I updated my answer! Check it out, you need to include CONTROLLERNAME in the js-function. You also need jQuery for this to work
Many thanks for your help, Lennart. I greatly appreciate it!

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.