3

I need to create a select list, preserving the state, which is not part of the model passed to the view. I suppose I should be using a ViewBag to pass a List to the View ? Any advise on implementation and how to preserve the state of the select list (how do I pass the selected value back to action and to the view again (possible ways of doing this) ?

The Action as of now:

public ActionResult Images(string x, string y)
{
//some code 

ContentPage cp = this.ContentPage;

return View(cp);
} 

//Post to action with same name:
[HttpPost]
public ActionResult Images(string someParameter)
 {

    ContentPage cp = this.ContentPage;

    return View(cp);
 }

The view as of now :

@model ContentPage
@{
ViewBag.Title = "Images";
CmsBaseController controller = (this.ViewContext.Controller as CmsBaseController);
}
@using (Html.BeginForm())
{ 
<div>

//This should go to List<SelectListItem> as I understand
<select name="perpage" id="perpage" onchange='submit();'>
           <option value="1">1</option>
           <option value="2">2</option>
           <option value="3">3</option>

</select>
</div>
}

Thank you!!!

1
  • 3
    Why is it not part of the model? Making a ViewModel with the selectlist is probably the easiest option. Commented Aug 26, 2011 at 18:26

1 Answer 1

11

Have you taken a look at this SO question? The answer there looks like it would do the trick if you wanted to use ViewBag/ViewData to pass in that list.

That said, why not just create a quick viewmodel and store it there? It really is a simple way to go.

I don't know what your ContentPage model is but you could certainly make a ContentPageViewModel that has whatever stuff (including the values for the select list) that is necessary for the page.


Example:

It would easy enough, for example, to have a property on the viewmodel to hold the selection and a property that holds some enumeration of possible values. Something like this:

public class MyViewModel
{
   ...

   public int SelectedId { get; set; }

   ...

   public IEnumerable<Choice> Choices { get; set; }
}

where Choice, in my example, is a class that has two properties, one that holds some identifier and the other the text to display. Something like this:

public class Choice
{
   public int Id { get; set; }
   public string Text { get; set; }
}

And then you could just have, perhaps, a DropDownListFor which handles the display/selection work for you. Something like this:

@model MyViewModel

@Html.DropDownListFor(model => model.SelectedId, new SelectList(Model.Choices, "Id", "Text"), "Choose... ")

Back in your controller's action, the viewmodel's SelectedId will get populated with the corresponding Id for the Choice picked view the dropdown.

Sign up to request clarification or add additional context in comments.

1 Comment

@Html.DropDownListFor(model => model.Size, (SelectList)ViewBag.Size, string.Empty) essentially the same answer as above, but a gentle reminder that you can throw the SelectList into a ViewBag and create in the Controller. Otherwise, thanks for the reminder on constructing the DropDowns this way.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.