MVC返回到控制器的字段

问题描述 投票:0回答:1

[我有一个控制器来检索和返回下拉菜单中的值,然后,当选择了下拉菜单中的一个选项时,它将使用API​​请求中的值(标题和ID)。

控制器

public ActionResult GetEpics()
        {
            //Code to retrieve list
            Epics = new GetEpicsViewModel();
            Epics.Epics = epicsList;

            return View(Epics);
        }



 [HttpPost]
            public ActionResult Build(GetEpicsViewModel epic)

            {
                GetEpicsViewModel epicTest = epic;
                //API Request
                return View();
            }

这显示在我的下拉列表中,如下所示:

查看

 @using (Html.BeginForm("Build", "GetEpics", FormMethod.Post))
                {
                    <label for="input_OutputType"> Process: @Html.DropDownListFor(model => model.Id, new SelectList(Model.Epics, "Id", "Title")) </label>                    
                    <input type="submit" value="Submit" />
                }

这很好,但是我该如何将标题和ID都传递给我的控制器?我可以很好地传递ID,但无法弄清楚如何传递标题。

Screenshot

型号

public class DevOpsEpic
    {

        public string Id { get; set; }            
        public string Title { get; set; }            
        public string Description { get; set; }  

    }

 public class GetEpicsViewModel
    {
        public string Id { get; set; }
        public string Title { get; set; }
        public List<DevOpsEpic> Epics { get; set; }
    }

意识到这可能是一个非常简单的答案,但是无法弄清楚!

asp.net-mvc model-view-controller html.dropdownlistfor
1个回答
0
投票

您可以使用jQuery,因此,当下拉列表更改时,请在隐藏文件中设置标题值。

@using (Html.BeginForm("Build", "GetEpics", FormMethod.Post))
  {
       <label for="input_OutputType"> Process: @Html.DropDownListFor(model => model.Id, new SelectList(Model.Epics, "Id", "Title"),new { name = "Id" }) </label>
       <input type="hidden" id="Title" name="Title" />
       <input type="submit" value="Submit" />
  }

$('#dropdownId').change(function(){

   $('#Title').val($('#dropdownId option:selected').text());

});
© www.soinside.com 2019 - 2024. All rights reserved.