【问题标题】:ASP.NET MVC Dropdown Selected ItemASP.NET MVC 下拉选择项
【发布时间】:2013-04-28 10:52:49
【问题描述】:

我的 Index 页面上有一个 DropDownListFor,Create 页面上有一个。两个下拉列表的目的相同。

我想要的是当用户在索引页面的 Index 下拉列表中选择一个项目时,它会将所选项目的值保存为会话的 GUID,并且当 Create 页面加载后,我希望其中的下拉列表根据会话中的 GUID 选择项目。

当用户点击“创建”并进入创建页面时,我只是设置了一个对象并将该对象发送到创建视图。

编辑

我通过这样做将用户发送到创建页面:

Html.ActionLink("Create New Listing", "Create", null, new { @class = "btn btn-primary" }))

如何将所选项目的 GUID 发送到视图?

【问题讨论】:

    标签: asp.net-mvc html.dropdownlistfor


    【解决方案1】:

    我猜你有这样的情况。这是索引视图:

    @model Models.IndexViewModel
    @{
        ViewBag.Title = "Index";
    }
    
    <h2>Index</h2>
    @using (Html.BeginForm("SaveGuid", "Flow"))
    {
        Html.DropDownListFor(x => x.SelectedGuid, Model.Guids, new { onchange = "this.form.submit();" });
    }
    

    这是索引模型:

    public class IndexViewModel
    {
        public Guid SelectedGuid { get; set; }
        public SelectList Guids { get; set; }
    }
    

    Index 和 SaveGuid Action 如下所示:

    private List<Guid> Guids = new List<Guid> { Guid.NewGuid(), Guid.NewGuid() }; // for testing only
    
    public ActionResult Index()
    {
        var model = new IndexViewModel { Guids = new SelectList(Guids, Guids.First()) };
        return View(model);
    }
    
    public ActionResult SaveGuid(IndexViewModel model)
    {
        Session["SelectedGuid"] = model.SelectedGuid;        
        return new RedirectResult("Create");
    }
    

    创建视图如下所示...

    @model MvcBootStrapApp.Models.CreateViewModel
    @{
        ViewBag.Title = "Create";
    }
    
    <h2>Create</h2>
    @using (Html.BeginForm("SaveGuid", "Flow"))
    {
        @Html.DropDownListFor(x => x.SelectedGuid, Model.Guids, new { onchange = "this.form.submit();" });
    }
    
    @using (Html.BeginForm("SaveCreate", "Flow"))
    { 
        // setup other controls
        <input type="submit" value="Submit" />
    }
    

    像这样使用 CreateViewModel...

    public class CreateViewModel
    {
        public Guid SelectedGuid { get; set; }
        public SelectList Guids { get; set; }
    
        // include other model properties
    }
    

    Create 和 CreateSave ActionResult 如下所示...

    public ActionResult Create()
    {
        Guid selectedGuid = Guids.First();
        if (Session["SelectedGuid"] != null)
            selectedGuid = (Guid)Session["SelectedGuid"];
    
        return View(new CreateViewModel
        {
            Guids = new SelectList(Guids, selectedGuid),
            SelectedGuid = selectedGuid
        });
    }
    
    public ActionResult SaveCreate(CreateViewModel model)
    {
        // save properties
    
        return new RedirectResult("Index");
    }
    

    我使用了两种表单来允许更改选定的 Guid 并回发所有 Create 属性。

    【讨论】:

    • 非常感谢 Wellers。我设法让它工作。您提供了令人难以置信的帮助
    【解决方案2】:

    如果你想使用 Session,我认为你需要的是使用表单发布到 ActionResult 以保存下拉列表的值,然后重定向到创建页面。

    public ActionResult SaveGuid(Guid value)
    {
        Session["SelectedGuid"] = value;
        return new RedirectResult("Create");
    }
    

    然后在您的 Create ActionResult 中,将 Session 值传递给 Create View 的 Model。

    public ActionResult Create()
    {
        var selectedGuid = (Guid)Session["SelectedGuid"];
        return View(new CreateViewModel { SelectedGuid = selectedGuid, /* include other properties */ };
    }
    

    在您看来,您可以在传递给您的 DropDownListFor 的 SelectList 上设置选定的选项...

    @Html.DropDownListFor(
        x => x.SelectedGuid, 
        new SelectList(Model.ListOfStuff, "Key", "Value", Model.SelectedGuid)
    )
    

    【讨论】:

    • 嗨。我在代码周围包裹了一个 BeginForm,但每次我更改下拉列表中的选择时它都会触发,因此无法单击“创建”按钮。
    • 好的,我已将按钮移到 BeginForm 之外,并使表单转到 SaveGuid 方法。到目前为止一切都很好,我只需要在 SaveGuid 方法中重新填充模型并让用户返回索引视图。
    猜你喜欢
    • 1970-01-01
    • 2015-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多