2013-04-28 64 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