2013-04-28 82 views
0

我有一个DropDownListFor,这是我的索引页面和一个在我的创建页面。这两个dropdownlists服务于相同的目的。ASP.NET MVC Dropdown Selected Item

我想,当用户选择在索引页的指数下拉列表的产品,这样可以节省这是一个GUID的会话和所选项目的值当创建页面加载,我想在那里的下拉列表中选择基于会话中的GUID的项目。

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

编辑

我做这个发送用户到创建页面:

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

我怎么送过来的GUID的selectedItem属性来看待?

回答

1

我想你有这样的情况。下面是Index视图:

@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; } 
} 

指数和SaveGuid操作是这样的:

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" /> 
} 

使用Cr eateViewModel像这样...

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

    // include other model properties 
} 

的创建和CreateSave ActionResults这个样子......

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的两个变化和回传的所有创建属性。

+0

非常感谢Wellers。我设法让它工作。你一直难以置信的帮助 – Subby 2013-04-28 12:49:31

+1

没问题Subby:D – wellers 2013-04-28 12:50:13

1

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

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选择列表的选择选项...

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

嗨。我在代码中包装了一个BeginForm,但每次更改下拉列表中的某些内容时它都会触发,因此无法单击“创建”按钮。 – Subby 2013-04-28 11:26:46

+0

好吧,我已经将BeginForm之外的按钮移到了SaveGuid方法中。所有目前看起来都不错,我只需要在SaveGuid方法中重新填充模型,并让用户返回到索引视图。 – Subby 2013-04-28 11:34:13