Return Integer from DropDown List in form

Hi:

I have an MVC form in which I want want to create a new Topic and assign that topic to a Category from a select list in the form.   The two classes are:

public class Topic
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public int CategoryId { get; set; }
        public Category Category { get; set; }
    }

    public class Category
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }

Open in new window


If I pass in  ViewBag.CategoryId = new SelectList(_context.Categories, "Id", "Name");  and use the scaffoled razor code, the new topic is created magically with the category I select in the form being associated with the new topic.

However, I cannot seem to get my tired head around around how to do this while passing in a ViewModel to the form and posting my results back.  Any help doing this where I  create a CreateTopicViewModel - somehow include the dropdown list of categories in this viewmodel  - pass that into the form and then be able to create the new topic would be greatly appreciated.

RBS
RBSAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Miguel OzSoftware EngineerCommented:
The dropdown is missing the selected value. Please replace ViewBag.CategoryId line in your controller GET and POST actions with the code below: (I assume you have an instance called topic that is passed to the View)
ViewBag.ArtistId = new SelectList(_context.Categories, "Id",  "Name", topic.CategoryId );

Open in new window

Note: If more help is needed please post your controller methods (GET and POST actions)
0
RBSAuthor Commented:
Hi:

As I tried to say in my question, I am able to pass a Viewbag to the view and it works fine.  So I do not need help with that.  What I do need help with is passing a select list to a ViewModel and getting the form to work.

RBS
0
Miguel OzSoftware EngineerCommented:
Please post your controller methods and view relevant to this question as requested in my post.
Notes:
1)The posted view bag in your question does not work because you are not passing the selected value. As posted before you may be missing reading the selected value in your POST action.
2) Review your Topic class declaration, the property Category seems redundant given the fact that it already has CategoryID property to identify which Category the topic belongs to.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.