How can I pass selected values?

Feb 13, 2012 at 3:51 PM
Edited Feb 14, 2012 at 5:02 PM

Been trying all weekend to figure out how to implement dropdownlists and checkboxlists with MVC3. Figured out a rudimentary way to do dropdownlists (category), but I've spent over 20 hours trying to get a checkboxlist to work.

Then I happened on your Grab Bag, and I am impressed (and depressed I didn't find it sooner).

I am getting the hang of how it works, but being new to MVC3, I am not sure how I would pass selected values to another view.

I am trying to avoid any lengthy code in my controllers (I have a few and each has MANY actions), particularly since I don't want to have to repeat, for example, the same code in 5 or 6 actionresults.

I am using MvcSerializer in a Wizard, which is working very well. If I have @Html.EditorFor(m => m.FirstName), the value input persists through the wizard steps to the confirmation page, where I can simply pass @Model.FirstName. Would you be able to give me an idea of how I can, for example, pass info into another view?

Much appreciated.

Feb 14, 2012 at 5:03 PM

I too am having a problem with this. 

As I posted on the author's blog: I am trying to render the selected items from the CheckBoxes.  I can do something simple like  [at]View.Name for anything that has only one selection (dropdown for example).  However, when I try to do that for the checkbox it only spits out "System.Collections.Generic.List`1[MyNamespace.ViewModels.MyModel+EnumName]"

The author mentioned in a reply that I could foreach over them in your view and render each item in a <ul><li>, for example</li></ul>, and I thought about that in terms of output. But forgive me, I am not a programmer by profession and am unable to figure out how to code that (my feeble attempts have been unsuccessful).

Could I impose upon you to post some code (I would imagine it might be useful to others). Thank you in advance.

On a personal note (not trying to brown nose): I've really been struggling with getting these types of controls to work in a project of mine. I have basically created a hodge podge of workarounds/implementations and really spent a lot of time trying to unify everything. Your MvcGrabBag does that (and more) for me, so I hope you get a lot of traffic as this is truly something useful for MVC. Good luck.

Coordinator
Feb 20, 2012 at 2:09 PM

@remseq:

If you open the Sample Web project in the source code, I do show how you can show multiple readonly values, but it requires the [ReadOnly] attribute on your property. You could probably do something like this in your Model:

public class MyModel
{
[MySelector]
public List<string> InputValues { get; set; }

[MySelector]
[ReadOnly(true)]
public List<string> ReadOnlyValues
{
   get { return InputValues; }
}
}


and in your View

This should render the checkboxes
@Html.EditorFor(m => m.InputValues)

This should render a read-only list of the selected values
@Html.EditorFor(m => m.ReadOnlyValues)
Coordinator
Feb 20, 2012 at 2:10 PM

@DinoStar,

I haven't done any Wizard based stuff in a while, but your options are basically to store the values in some kind of state bag. For example, you could store it into Session, Cookies (perhaps), or a <hidden> form field and make sure it gets rendered onto the new view. That way when the user posts the second view, it will contain <hidden> fields for the previous form, including all the additional fields in the new form.

Session will probably be the easiest way to go though

Feb 20, 2012 at 3:11 PM
Edited Feb 20, 2012 at 8:25 PM

UPDATE (deleted prior response).

I messed up in that I kept the original name inside the List<[EnumName]>. Using List<string> worked to post the checkbox values!!!!

Thanks!

Mar 3, 2012 at 3:03 PM
Edited Mar 3, 2012 at 8:18 PM

Deleted - sorry