How to get the value from the combo box and put it in the controller

Viewed 42

enter code here

    <div class="k-d-flex k-justify-content-center" style="padding-top: 54px;">
        <div class="k-w-300">
            <h4 style="text-align: center;">Customize your Projectship</h4>
            <label for="projectship">ProjectShip</label>
            <kendo-combobox id="combobox" combobox datatextfield="Text" datavaluefield="Value" value="2" placeholder="Select ProjectShip..."
                            suggest="true" filter="FilterType.Contains" name="projectship" style="width:100%;" bind-to="data">
            </kendo-combobox>

            <input datatextfield="Text" datavaluefield="Value" suggest="true" filter="FilterType.Contains" name="projectship" style="width:100%;" bind-to="data" asp-for="PShID" class="form-control" />

        </div>
    </div>
</form>

enter code here

    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> RegisterDischargeTonnage(RegisterDischargeTonnageViewModel model)
    {
        if (ModelState.IsValid)
        { 

          await _projectship.RegisterDischargeTonnage(model);
            return RedirectToAction(nameof(Index));
        }
        return View(model);`enter code here`
    }
1 Answers

Below is one simple example to pass the data from View to controller.

InfoModel.cs

namespace MVCapp.Models
{
    public class InfoModel
    {
        public string Name { get; set; }
    }
}

HomeController.cs

        [HttpPost]
        public ActionResult Index(InfoModel person)
        {
           
            string name = person.Name;
            

            return View();
        }

Index.cshtml

@model MVCapp.Models.InfoModel
@{
    Layout = null;
}
 
<!DOCTYPE html>
 
<html>
<head>
    <meta name="viewport" content="width=device-width"/>
    <title>Index</title>
</head>
<body>
    @using (Html.BeginForm("Index", "Home", FormMethod.Post))
    {
        <table cellpadding="0" cellspacing="0">
           
            <tr>
                <td>Name: </td>
                <td>
                    @Html.DropDownListFor(m => m.Name, new List<SelectListItem>
                   { new SelectListItem{Text="ABC", Value="ABC"},
                     new SelectListItem{Text="XYZ", Value="XYZ"}}, "Please select")
                </td>
            </tr>
           
            <tr>
                <td></td>
                <td><input type="submit" value="Submit"/></td>
            </tr>
        </table>
    }
</body>
</html>

Output:

enter image description here

Further, you could modify the code as per your own requirements.

Related