0

Should be a relatively simple question but currently I'm not able to pass model values updated in the view back to the controller.

As you can see below we have a very simple dropdown list in the view. The 'tenant' string in the Model should be getting set with whichever value is selected in the dropdown list.

However, when clicking on the submit button the 'tenant' string returns null in the controller...

What am I missing?

Thanks!

Model:

public class TenantModel
    {
        public List<string> tenantList { get; set; }

        public string tenant { get; set; }
    }

View:

@model TenantModel

@{
    ViewData["Title"] = "Manage";
}

<h1>Manage Databases</h1>



@Html.DropDownListFor(m => m.tenant, new SelectList(Model.tenantList), "Select Tenant", new { @class = "dropdown", @style = "width: 220px" })

<br />


<button type="submit" onclick="location.href='@Url.Action("DropDbs", "Environments", Model)'" class="btn btn-danger">Destroy</button>

Controller

    public class EnvironmentsController : Controller
    {

        public IActionResult Manage()
        {
            var tenantModel = new TenantModel();
            
            tenantModel.tenantList = Utils.TenantList();
            
            return View(tenantModel);
        }
        
        
        public IActionResult DropDbs(TenantModel tenantModel)
        {
            
            return RedirectToAction("Manage");
        }
    }
1
  • where is your form tag or Html.BeginForm? Commented Nov 12, 2020 at 19:25

2 Answers 2

1

I want to have multiple buttons on the form each targeting a different action inside my controller.

In Asp.net core MVC, you can use tag helper asp- to implement it.

<form method="post">
    @Html.DropDownListFor(m => m.tenant, new SelectList(Model.tenantList), "Select Tenant", new { @class = "dropdown", @style = "width: 220px" })

    <br />

    <button type="submit" asp-action="Action1" asp-controller="Controller" class="btn btn-danger">Operate1</button>
    <button type="submit" asp-action="Action2" asp-controller="Controller" class="btn btn-danger">Operate2</button>
    <button type="submit" asp-action="Action3" asp-controller="Controller" class="btn btn-danger">Operate3</button>

</form>
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I did try this but wasn't able to get it to work initially, I think I was missing the form tags around the buttons, adding these in seems to have done the trick.
0

You're all doing wrong. You're redirecting to action and passing a Model but problem is that not going to fill by your dropdown that why it's null.

You need to wrap your dropdown in form tag and post form to the action.

2 Comments

Thanks - if I wrap it in @using (Html.BeginForm("DropDbs", "Environments", FormMethod.Post) it does work and passes the 'tenant' string through BUT I can only target a single action this way, I want to have multiple buttons on the form each targeting a different action inside my controller.
Use hidden field as tag to identify operation and change field on button click and then submit Form.

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.