0

I have a DropDownListFor that is on my Index page and one in my Create page. Both dropdownlists serve the same purpose.

What I want is when the user selects an item in the Index dropdownlist in the index page, it saves that selected item's value which is a GUID to the session and when the Create page loads, I want the dropdownlist in there to select the item based on the GUID in the session.

At the moment when the user clicks on "Create" and goes to the create page, I am merely setting up an object and sending that object to the Create View.

Edit:

I am sending the user over to the Create page by doing this:

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

How do I send the GUID of the selecteditem over to the view?

Subby
  • 5,370
  • 15
  • 70
  • 125

2 Answers2

1

If you want to use Session, what I think you need is to use a form to post to an ActionResult to save the dropdownlist's value and then redirect to the Create page.

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

Then in your Create ActionResult, pass the Session value to the Create View's Model.

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

In your view you can set the selected option on the SelectList passed to your DropDownListFor...

@Html.DropDownListFor(
    x => x.SelectedGuid, 
    new SelectList(Model.ListOfStuff, "Key", "Value", Model.SelectedGuid)
)
Paul Welbourne
  • 1,063
  • 10
  • 16
  • Hi. I wrapped a BeginForm around the code but it is firing every time I change select something in the dropdown and therefore can't click on the "Create" button. – Subby Apr 28 '13 at 11:26
  • Ok I have moved the button outside of the BeginForm and made the form go to SaveGuid method. All is looking good so far, I just need to re-populate the Model in the SaveGuid method and make the user go back to the Index view. – Subby Apr 28 '13 at 11:34
1

I guess you have a situation like this. Here is the Index view:

@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();" });
}

Here is the Index model:

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

The Index and SaveGuid Action look like this:

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");
}

The Create View looks like this...

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

Using a CreateViewModel like this...

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

    // include other model properties
}

The Create and CreateSave ActionResults look like this...

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");
}

I used two forms to allow both the change of selected Guid and to postback all the Create properties.

Paul Welbourne
  • 1,063
  • 10
  • 16
  • Thank you very much Wellers. I managed to get it working. You've been unbelievably helpful – Subby Apr 28 '13 at 12:49