How to mix parameters within an HTML.Beginform ASP.NET

Asked

Viewed 267 times

1

I got the following Html.BeginForm(Don’t take into account if it’s incorrect is a legacy code and I can’t refactor it now).

using (Html.BeginForm("Confirm", "Download", new { ContributorID = "exemplo"}, FormMethod.Post, new { id = "confirm" })) {
    <div class="form-group">
        <label for="work_select">Selecione uma obra existente:</label>
        <select class="form-control" id="work_select">
            <option value="0" selected="selected">Selecione uma obra</option>
        </select>
    </div>
    <p id="error-message"></p>

    @Html.AntiForgeryToken()
    <div class="inner-addon left-addon">
        <i class="fa fa-download" aria-hidden="true"></i>
        <input type="submit" value="@MainMessages.confirm_your_download" class="button-download" style="width:100%" />
        <br />
    </div>
}

Controller:

[HttpPost]
public ActionResult Confirm(string contributorID,string work_select)

If you noticed there is a parameter being added without using an input new { ContributorID = "exemplo"}, I need it to stay that way, however the <select> is not passing the value of work_select for the controller, it arrives null, how can I fix this?

1 answer

2


The <select> is not being sent by the form because it does not have the attribute name. While id is important for Javascript in the DOM, the controls of <form> shall be attached only to FormData if they indicate the attribute name or if you add them manually via Javascript.

<div class="form-group">
    <label for="work_select">Selecione uma obra existente:</label>
    <select class="form-control" id="work_select" name="work_select">
        <option value="0" selected="selected">Selecione uma obra</option>
    </select>
</div>
  • Perfect Leandro, I ate ball with this... I’m so used to using the Razor Helper that I didn’t remember that part of the HTML itself.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.