【问题标题】:Select dropdown value after post发布后选择下拉值
【发布时间】:2018-06-01 08:00:24
【问题描述】:

我希望在发布后(剃刀)后保留下拉列表中的值的问题上获得一些指导

我有一个简单的页面:

    @model testContingency.Models.ListByWardDD

@{
    ViewBag.Title = "TestDropDowns";
}

<h2>TestDropDowns</h2>

<div>

    @Html.DropDownList("HospModel", Model.Hospital,  new { @onchange = "ChangeHospital(this.value)" }) 
    @Html.DropDownList("WardModel", Model.Wards)

    <script type="text/javascript">

        function ChangeHospital(val) {
            window.location.href = "/PatientListByWardDD/TestDropDowns?hospID=" + val;
        }



    </script>

</div>

这是控制器

public ActionResult TestDropDowns(int? hospID)
    {
        PASInpatientRepository pasRepo = new PASInpatientRepository();
        var returnModel = new ListByWardDD();     
        var HospitalData = pasRepo.GetPatientHospitalsEnum();
        returnModel.Hospital = pasRepo.GetHopspitalListItems(HospitalData);
        var WardData = pasRepo .GetPatientWardsEnum(hospID);
        returnModel.Wards = pasRepo.GetWardListItems(WardData);
        ViewBag.HospSearch = hospID;
        return View(returnModel);
    }

在控制器中PASInpatientRepository() 与缓存数据库通信。它传回公共 IEnumerable GetHopspitalListItems。它调用在缓存数据库中编写的存储过程(本质上与 sql 存储过程相同)。这一切都以自己粗鲁的方式正常工作。

我遇到的问题是,当我选择下拉列表 @Html.DropDownList("HospModel", Model.Hospital, new { @onchange = "ChangeHospital(this.value)" }) 并调用控制器刷新病房下拉列表时,我想保留我在医院下拉列表中选择的值。我尝试了几种不同的方法,但我承认,我有点卡住了。我发现的大多数示例都是针对强类型的。

正如我所提到的,我是 MVC 的新手,但是对于如何解决这个问题的任何建议,或者关于改进我的代码的建议,我们都非常感谢。

【问题讨论】:

    标签: asp.net-mvc-4 html-helper


    【解决方案1】:

    所以我不确定 Hospital 属性是什么样的,但我会假设每个属性都有一个唯一的 ID。 此外,要将发布的数据绑定到视图模型,您需要在视图中使用表单。要创建下拉列表,请使用DropDownListFor-Helper。这样在提交表单后数据将被绑定回您的模型。

    所以你的视图可能看起来像这样

    @model testContingency.Models.ListByWardDD
    
    @{
        ViewBag.Title = "TestDropDowns";
    }
    
    <h2>TestDropDowns</h2>
    
    <div>
        @using (Html.BeginForm("TestDropDowns", "YourController", FormMethod.Post))
        {
            @Html.DropDownListFor(x => x.HospitalID, Model.Hospital)
            @Html.DropDownListFor(x => x.WardID, Model.Wards)
            <input type="submit" value="send" />
    }    
    </div>
    

    您的 ViewModel testContigency.Models.ListByWardDD 必须至少具有以下属性

    public class ListByWardDD {
        public int HostpitalID { get;set; }
        // the value of the SelectListItem-objects should be the hospital ID
        public IEnumerable<SelectListItem> Hospital { get;set; }
    
        public int WardID { get;set; }
        // the value of the SelectListItem-objects should be the ward ID
        public IEnumerable<SelectListItem> Wards { get;set; }
    }
    

    一旦您发布了表单(为简单起见,我添加了一个按钮来发送表单并保留了 javascript 部分),您的控制器(您需要填写 BeginForm-Helper)的方法 TestDropDowns 将被调用。该方法需要一个 ListByWardDD 类型的对象作为参数,并且框架会自动为您填充这些值。

    [HttpPost]
    public ActionResult TestDropDowns(ListByWardDD viewModel) {
        // your code here, viewModel.HospitalID should contain the selected value
    }
    

    注意:提交表单后,HospitalWards 属性将为空。如果需要再次显示表单,则需要重新填充这些属性。否则您的下拉列表为空。

    我尽力发布有效代码,但我没有编译或测试它。

    【讨论】:

    • 非常感谢您的反馈。我会采纳你关于使用 ListFor 和 Lambda 的建议。但我真的需要知道如何在回发后重新加载医院列表中的选定值。如果我能提供帮助,我真的不想使用 javascript 和 Ajax。但我开始认为这是不可避免的,除非有人可以提出其他建议?
    猜你喜欢
    • 1970-01-01
    • 2021-12-06
    • 1970-01-01
    • 2020-10-05
    • 2020-06-01
    • 1970-01-01
    • 2013-06-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多